From e33e56d0965c09aad7cd587a9d322beda094e0ab Mon Sep 17 00:00:00 2001 From: Maxwell Frank Date: Thu, 20 Jul 2023 18:45:59 +0000 Subject: [PATCH] fix: selectable box set props forwarding --- src/SelectableBox/README.md | 51 +++++++++++++++++++ src/SelectableBox/SelectableBoxSet.jsx | 20 ++++++++ .../tests/SelectableBoxSet.test.jsx | 21 ++++++++ 3 files changed, 92 insertions(+) diff --git a/src/SelectableBox/README.md b/src/SelectableBox/README.md index e1bf00d3a49..b5dfa805fe3 100644 --- a/src/SelectableBox/README.md +++ b/src/SelectableBox/README.md @@ -42,6 +42,7 @@ As ``Checkbox`` onChange={handleChange} name="cheeses" columns={isExtraSmall ? 1 : 2} + ariaLabel="cheese selection" >
@@ -83,6 +84,7 @@ As ``Checkbox`` onChange={handleChange} name="colors" columns={isExtraSmall ? 1 : 3} + ariaLabel="color selection" >
@@ -144,6 +146,7 @@ As ``Checkbox`` with ``isIndeterminate`` onChange={handleChange} name="cheeses" columns={isExtraSmall ? 1 : 3} + ariaLabel="cheese selection" >
@@ -162,3 +165,51 @@ As ``Checkbox`` with ``isIndeterminate`` ); } ``` + +As ``Checkbox`` with ``ariaLabelledby`` + +```jsx live +() => { + const type = 'checkbox'; + const allCheeseOptions = ['swiss', 'cheddar', 'pepperjack']; + const [checkedCheeses, { add, remove, set, clear }] = useCheckboxSetValues(['swiss']); + + const handleChange = e => { + e.target.checked ? add(e.target.value) : remove(e.target.value); + }; + + const isExtraSmall = useMediaQuery({ maxWidth: breakpoints.extraSmall.maxWidth }); + + return ( +
+

+ Select your favorite cheeses +

+ + +

+ Swiss +

+
+ +

+ Cheddar +

+
+ +

+ Pepperjack +

+
+
+
+ ); +} +``` diff --git a/src/SelectableBox/SelectableBoxSet.jsx b/src/SelectableBox/SelectableBoxSet.jsx index 29a9d751ca4..f26f3e1f206 100644 --- a/src/SelectableBox/SelectableBoxSet.jsx +++ b/src/SelectableBox/SelectableBoxSet.jsx @@ -2,6 +2,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import { getInputType } from './utils'; +import { requiredWhenNot } from '../utils/propTypes'; const INPUT_TYPES = [ 'radio', @@ -19,6 +20,9 @@ const SelectableBoxSet = React.forwardRef(({ type, columns, className, + ariaLabel, + ariaLabelledby, + ...props }, ref) => { const inputType = getInputType('SelectableBoxSet', type); @@ -35,6 +39,9 @@ const SelectableBoxSet = React.forwardRef(({ `pgn__selectable_box-set--${columns || DEFAULT_COLUMNS_NUMBER}`, className, ), + 'aria-label': ariaLabel, + 'aria-labelledby': ariaLabelledby, + ...props, }, children, ); @@ -62,6 +69,17 @@ SelectableBoxSet.propTypes = { columns: PropTypes.number, /** A class that is be appended to the base element. */ className: PropTypes.string, + /** + * The ID of the label for the `SelectableBoxSet`. + * + * An accessible label must be provided to the `SelectableBoxSet`. + */ + ariaLabelledby: PropTypes.string, + /** + * A label for the `SelectableBoxSet`. + * + * If not using `ariaLabelledby`, then `ariaLabel` must be provided */ + ariaLabel: requiredWhenNot(PropTypes.string, 'ariaLabelledby'), }; SelectableBoxSet.defaultProps = { @@ -72,6 +90,8 @@ SelectableBoxSet.defaultProps = { type: 'radio', columns: DEFAULT_COLUMNS_NUMBER, className: undefined, + ariaLabelledby: undefined, + ariaLabel: undefined, }; export default SelectableBoxSet; diff --git a/src/SelectableBox/tests/SelectableBoxSet.test.jsx b/src/SelectableBox/tests/SelectableBoxSet.test.jsx index b017bd9eeed..ccfb6ae2dda 100644 --- a/src/SelectableBox/tests/SelectableBoxSet.test.jsx +++ b/src/SelectableBox/tests/SelectableBoxSet.test.jsx @@ -1,6 +1,7 @@ import React from 'react'; import { mount } from 'enzyme'; import renderer from 'react-test-renderer'; +import { render, screen } from '@testing-library/react'; import { Form } from '../..'; import SelectableBox from '..'; @@ -37,6 +38,10 @@ describe('', () => { const tree = renderer.create(()).toJSON(); expect(tree).toMatchSnapshot(); }); + it('forwards props', () => { + render(()); + expect(screen.getByTestId('test-radio-set-name')).toBeTruthy(); + }); it('correct render when type prop is changed', () => { const setWrapper = mount(); expect(setWrapper.find(Form.RadioSet).length).toBeGreaterThan(0); @@ -84,5 +89,21 @@ describe('', () => { const selectableBoxSet = wrapper.find(Form.RadioSet); expect(selectableBoxSet.hasClass(`pgn__selectable_box-set--${columns}`)).toBe(true); }); + it('renders with an aria-label attribute', () => { + render(()); + expect(screen.getByLabelText('test-radio-set-label')).toBeTruthy(); + }); + it('renders with an aria-labelledby attribute', () => { + render(( + <> +

Radio Set Label text

+ + + )); + expect(screen.getByLabelText('Radio Set Label text')).toBeTruthy(); + }); }); });