Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LUNA-1331] [BpkCheckbox] Add ARIA label prop #3544

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/bpk-component-checkbox/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ const MixedExample = () => (
</div>
);

const AriaLabelExample = () => (
<div>
<StatefulCheckbox id="abisko" name="abisko" label="Abisko" isChecked aria-label="checkbox 1"/>
<StatefulCheckbox id="bunol" name="bunol" label="Buñol" aria-label="checkbox 2"/>
</div>
);

export {
DefaultExample,
IndeterminateExample,
Expand All @@ -194,4 +201,5 @@ export {
SmallLabelRequiredExample,
SmallLabelInvalidExample,
MixedExample,
AriaLabelExample,
};
4 changes: 3 additions & 1 deletion examples/bpk-component-checkbox/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
RequiredExample,
SmallLabelExample,
MixedExample,
AriaLabelExample,
} from './examples';

export default {
Expand All @@ -44,9 +45,10 @@ export const White = WhiteExample;
export const Disabled = DisabledExample;
export const Required = RequiredExample;
export const SmallLabel = SmallLabelExample;
export const AriaLabel = AriaLabelExample;

export const VisualTest = MixedExample;
export const VisualTestWithZoom = VisualTest.bind({});
VisualTestWithZoom.args = {
zoomEnabled: true
};
};
5 changes: 4 additions & 1 deletion packages/bpk-component-checkbox/src/BpkCheckbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Props = {
smallLabel: boolean,
valid: ?boolean,
checked: boolean,
ariaLabel: ?string,
/**
* The indeterminate prop is only a visual clue, it does not affect the checked state of the checkbox. If `indeterminate` is flagged then the checkbox will be displayed with a minus sign in the box. This is used when there is a checkbox group and the parent displays this state when not all child checkboxes are selected.
*/
Expand All @@ -45,6 +46,7 @@ type Props = {

const BpkCheckbox = (props: Props) => {
const {
ariaLabel,
checked,
className,
disabled,
Expand Down Expand Up @@ -88,7 +90,7 @@ const BpkCheckbox = (props: Props) => {
className={inputClasses}
name={name}
disabled={disabled}
aria-label={label}
aria-label={ariaLabel || label}
aria-invalid={isInvalid}
data-indeterminate={indeterminate}
ref={(e) => {
Expand All @@ -113,6 +115,7 @@ const BpkCheckbox = (props: Props) => {
BpkCheckbox.propTypes = {
name: PropTypes.string.isRequired,
label: PropTypes.node.isRequired,
ariaLabel: PropTypes.string,
required: PropTypes.bool,
disabled: PropTypes.bool,
white: PropTypes.bool,
Expand Down
16 changes: 13 additions & 3 deletions packages/bpk-component-checkbox/src/accessibility-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
* limitations under the License.
*/

/* @flow strict */

import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import { axe } from 'jest-axe';

import BpkCheckbox from './BpkCheckbox';
Expand All @@ -31,4 +29,16 @@ describe('BpkCheckbox accessibility tests', () => {
const results = await axe(container);
expect(results).toHaveNoViolations();
});

it('should render using label as aria-label if no ariaLabel prop provided', async () => {
render(<BpkCheckbox name="checkbox" label="Prefer directs" />);
const label = screen.getByLabelText('Prefer directs');
expect(label).toBeInTheDocument();
});

it('should render using ariaLabel prop as aria-label if prop provided', async () => {
render(<BpkCheckbox name="checkbox" label="Prefer directs" ariaLabel="aria label test string"/>);
const ariaLabel = screen.getByLabelText('aria label test string');
expect(ariaLabel).toBeInTheDocument();
});
Comment on lines +32 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd maybe add another test that should render using ariaLabel when both aria-label and label are provided [completely optional to add]

});
Loading