Skip to content

Commit 04b87f3

Browse files
[Switch] Add role="switch" (#46482)
1 parent 79bb2dd commit 04b87f3

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

docs/data/material/components/switches/switches.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ You can change the placement of the label:
7171

7272
## Accessibility
7373

74-
- It will render an element with the `checkbox` role not `switch` role since this
75-
role isn't widely supported yet. Please test first if assistive technology of your
76-
target audience supports this role properly. Then you can change the role with
77-
`<Switch inputProps={{ role: 'switch' }}>`
7874
- All form controls should have labels, and this includes radio buttons, checkboxes, and switches. In most cases, this is done by using the `<label>` element ([FormControlLabel](/material-ui/api/form-control-label/)).
7975
- When a label can't be used, it's necessary to add an attribute directly to the input component.
8076
In this case, you can apply the additional attribute (for example `aria-label`, `aria-labelledby`, `title`) via the `inputProps` prop.

packages/mui-material/src/Switch/Switch.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ const Switch = React.forwardRef(function Switch(inProps, ref) {
296296
? slotProps.switchBase(ownerState)
297297
: slotProps.switchBase,
298298
}),
299+
input: {
300+
role: 'switch',
301+
},
299302
...(slotProps.input && {
300303
input:
301304
typeof slotProps.input === 'function' ? slotProps.input(ownerState) : slotProps.input,

packages/mui-material/src/Switch/Switch.test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,28 @@ describe('<Switch />', () => {
7474
expect(root.childNodes[1]).to.have.class(classes.track);
7575
});
7676

77-
it('renders a `role="checkbox"` with the Unchecked state by default', () => {
77+
it('renders a `role="switch"` with the Unchecked state by default', () => {
7878
const { getByRole } = render(<Switch />);
7979

80-
expect(getByRole('checkbox')).to.have.property('checked', false);
80+
expect(getByRole('switch')).to.have.property('checked', false);
8181
});
8282

83-
it('renders a checkbox with the Checked state when checked', () => {
83+
it('renders a switch with the Checked state when checked', () => {
8484
const { getByRole } = render(<Switch defaultChecked />);
8585

86-
expect(getByRole('checkbox')).to.have.property('checked', true);
86+
expect(getByRole('switch')).to.have.property('checked', true);
8787
});
8888

8989
specify('the switch can be disabled', () => {
9090
const { getByRole } = render(<Switch disabled />);
9191

92-
expect(getByRole('checkbox')).to.have.property('disabled', true);
92+
expect(getByRole('switch')).to.have.property('disabled', true);
9393
});
9494

9595
specify('the switch can be readonly', () => {
9696
const { getByRole } = render(<Switch readOnly />);
9797

98-
expect(getByRole('checkbox')).to.have.property('readOnly', true);
98+
expect(getByRole('switch')).to.have.property('readOnly', true);
9999
});
100100

101101
specify('renders a custom icon when provided', () => {
@@ -117,11 +117,11 @@ describe('<Switch />', () => {
117117

118118
// how a user would trigger it
119119
act(() => {
120-
getByRole('checkbox').click();
121-
fireEvent.change(getByRole('checkbox'), { target: { checked: '' } });
120+
getByRole('switch').click();
121+
fireEvent.change(getByRole('switch'), { target: { checked: '' } });
122122
});
123123

124-
expect(getByRole('checkbox')).to.have.property('checked', false);
124+
expect(getByRole('switch')).to.have.property('checked', false);
125125
});
126126

127127
it('should not show warnings when custom `type` is provided', () => {
@@ -137,7 +137,7 @@ describe('<Switch />', () => {
137137
</FormControl>,
138138
);
139139

140-
expect(getByRole('checkbox')).not.to.have.attribute('disabled');
140+
expect(getByRole('switch')).not.to.have.attribute('disabled');
141141
});
142142

143143
it('should be overridden by props', () => {
@@ -147,7 +147,7 @@ describe('<Switch />', () => {
147147
</FormControl>,
148148
);
149149

150-
expect(getByRole('checkbox')).to.have.attribute('disabled');
150+
expect(getByRole('switch')).to.have.attribute('disabled');
151151
});
152152
});
153153

@@ -159,7 +159,7 @@ describe('<Switch />', () => {
159159
</FormControl>,
160160
);
161161

162-
expect(getByRole('checkbox')).to.have.attribute('disabled');
162+
expect(getByRole('switch')).to.have.attribute('disabled');
163163
});
164164

165165
it('should be overridden by props', () => {
@@ -169,7 +169,7 @@ describe('<Switch />', () => {
169169
</FormControl>,
170170
);
171171

172-
expect(getByRole('checkbox')).not.to.have.attribute('disabled');
172+
expect(getByRole('switch')).not.to.have.attribute('disabled');
173173
});
174174
});
175175
});

0 commit comments

Comments
 (0)