Skip to content

Commit ad10877

Browse files
committed
fix: modify BooleanControl so that screen readers read a more accurate label, and update its interaction tests
1 parent ccffdc3 commit ad10877

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

code/ui/blocks/src/controls/Boolean.stories.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,36 @@ const meta = {
1515
info: 'This is info for the Boolean control stories',
1616
jsx: { useBooleanShorthandSyntax: false },
1717
},
18-
args: { name: 'boolean' },
1918
} as Meta<typeof BooleanControl>;
2019

2120
export default meta;
2221

2322
export const True: StoryObj<typeof BooleanControl> = {
2423
args: {
2524
value: true,
25+
name: 'True',
2626
},
2727
};
2828
export const False: StoryObj<typeof BooleanControl> = {
2929
args: {
3030
value: false,
31+
name: 'False',
3132
},
3233
};
3334

3435
export const Undefined: StoryObj<typeof BooleanControl> = {
3536
args: {
3637
value: undefined,
38+
name: 'Undefined',
3739
},
3840
};
3941

4042
export const Toggling: StoryObj<typeof BooleanControl> = {
4143
args: {
4244
value: undefined,
45+
name: 'Toggling',
4346
},
44-
play: async ({ canvasElement, id }) => {
47+
play: async ({ canvasElement, id, name, ...props }) => {
4548
const channel = addons.getChannel();
4649

4750
channel.emit(RESET_STORY_ARGS, { storyId: id });
@@ -55,23 +58,26 @@ export const Toggling: StoryObj<typeof BooleanControl> = {
5558
const setBooleanControl = canvas.getByText('Set boolean');
5659
await fireEvent.click(setBooleanControl);
5760

58-
let toggle = await canvas.findByTitle('Change to true');
59-
expect(toggle).toBeInTheDocument();
61+
let toggle = await canvas.findByLabelText(name);
62+
expect(toggle).toBeVisible();
6063

6164
// from False to True
6265
await fireEvent.click(toggle);
63-
toggle = await canvas.findByTitle('Change to false');
64-
expect(toggle).toBeInTheDocument();
66+
toggle = await canvas.findByRole('switch');
67+
expect(toggle).not.toBeChecked();
6568

6669
// from True to False
6770
await fireEvent.click(toggle);
68-
toggle = await canvas.findByTitle('Change to true');
71+
toggle = await canvas.findByRole('switch');
6972
expect(toggle).toBeInTheDocument();
7073
},
7174
};
7275

7376
export const TogglingInDocs: StoryObj<typeof BooleanControl> = {
7477
...Toggling,
78+
args: {
79+
name: 'Toggling In Docs',
80+
},
7581
parameters: {
7682
docs: {
7783
autoplay: true,

code/ui/blocks/src/controls/Boolean.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ export type BooleanProps = ControlProps<BooleanValue> & BooleanConfig;
9898
* <BooleanControl name="isTrue" value={value} onChange={handleValueChange}/>
9999
* ```
100100
*/
101-
export const BooleanControl: FC<BooleanProps> = ({ name, value, onChange, onBlur, onFocus }) => {
101+
export const BooleanControl: FC<BooleanProps> = ({
102+
name,
103+
value,
104+
onChange,
105+
onBlur,
106+
onFocus,
107+
...props
108+
}) => {
102109
const onSetFalse = useCallback(() => onChange(false), [onChange]);
103110
if (value === undefined) {
104111
return (
@@ -112,16 +119,17 @@ export const BooleanControl: FC<BooleanProps> = ({ name, value, onChange, onBlur
112119
const parsedValue = typeof value === 'string' ? parse(value) : value;
113120

114121
return (
115-
<Label htmlFor={controlId} title={parsedValue ? 'Change to false' : 'Change to true'}>
122+
<Label htmlFor={controlId} aria-label={name}>
116123
<input
117124
id={controlId}
118125
type="checkbox"
119126
onChange={(e) => onChange(e.target.checked)}
120127
checked={parsedValue}
128+
role="switch"
121129
{...{ name, onBlur, onFocus }}
122130
/>
123-
<span>False</span>
124-
<span>True</span>
131+
<span aria-hidden="true">False</span>
132+
<span aria-hidden="true">True</span>
125133
</Label>
126134
);
127135
};

0 commit comments

Comments
 (0)