Skip to content

Commit 847b3bb

Browse files
王磊fatelei
authored andcommitted
Support change the aria role of menu or menuitem.
1 parent 5a5d6c8 commit 847b3bb

File tree

5 files changed

+59
-8
lines changed

5 files changed

+59
-8
lines changed

src/Menu.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const Menu = createReactClass({
2828
selectable: PropTypes.bool,
2929
multiple: PropTypes.bool,
3030
children: PropTypes.any,
31+
role: PropTypes.string,
3132
},
3233

3334
mixins: [MenuMixin],

src/MenuItem.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ const MenuItem = createReactClass({
125125
[this.getDisabledClassName()]: props.disabled,
126126
});
127127
const attrs = {
128-
...props.attribute,
129-
title: props.title,
130-
className,
131128
role: 'menuitem',
132129
'aria-selected': selected,
133130
'aria-disabled': props.disabled,
131+
...props.attribute,
132+
title: props.title,
133+
className,
134134
};
135135
let mouseEvent = {};
136136
if (!props.disabled) {

src/MenuMixin.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const MenuMixin = {
6565
defaultOpenKeys: PropTypes.arrayOf(PropTypes.string),
6666
openKeys: PropTypes.arrayOf(PropTypes.string),
6767
children: PropTypes.any,
68+
role: PropTypes.string,
6869
},
6970

7071
getDefaultProps() {
@@ -225,11 +226,12 @@ const MenuMixin = {
225226
props.className,
226227
`${props.prefixCls}-${props.mode}`,
227228
);
229+
228230
const domProps = {
229231
className,
230-
role: 'menu',
231-
'aria-activedescendant': '',
232+
role: props.role || 'menu',
232233
};
234+
233235
if (props.id) {
234236
domProps.id = props.id;
235237
}

tests/Menu.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ describe('Menu', () => {
3939
});
4040
});
4141

42+
describe('render role listbox', () => {
43+
function createMenu() {
44+
return (
45+
<Menu
46+
className="myMenu"
47+
openAnimation="fade"
48+
role="listbox"
49+
>
50+
<MenuItem key="1" attribute={{ role: 'option' }}>1</MenuItem>
51+
<MenuItem key="2" attribute={{ role: 'option' }}>2</MenuItem>
52+
<MenuItem key="3" attribute={{ role: 'option' }}>3</MenuItem>
53+
</Menu>
54+
);
55+
}
56+
57+
it(`renders menu correctly`, () => {
58+
const wrapper = render(createMenu());
59+
expect(renderToJson(wrapper)).toMatchSnapshot();
60+
});
61+
});
62+
4263

4364
it('set activeKey', () => {
4465
const wrapper = mount(

tests/__snapshots__/Menu.spec.js.snap

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
exports[`Menu render renders horizontal menu correctly 1`] = `
44
<ul
5-
aria-activedescendant=""
65
class="rc-menu myMenu rc-menu-root rc-menu-vertical"
76
role="menu"
87
tabindex="0"
@@ -95,7 +94,6 @@ exports[`Menu render renders horizontal menu correctly 1`] = `
9594

9695
exports[`Menu render renders inline menu correctly 1`] = `
9796
<ul
98-
aria-activedescendant=""
9997
class="rc-menu myMenu rc-menu-root rc-menu-vertical"
10098
role="menu"
10199
tabindex="0"
@@ -188,7 +186,6 @@ exports[`Menu render renders inline menu correctly 1`] = `
188186

189187
exports[`Menu render renders vertical menu correctly 1`] = `
190188
<ul
191-
aria-activedescendant=""
192189
class="rc-menu myMenu rc-menu-root rc-menu-vertical"
193190
role="menu"
194191
tabindex="0"
@@ -278,3 +275,33 @@ exports[`Menu render renders vertical menu correctly 1`] = `
278275
</li>
279276
</ul>
280277
`;
278+
279+
exports[`Menu render role listbox renders menu correctly 1`] = `
280+
<ul
281+
class="rc-menu myMenu rc-menu-root rc-menu-vertical"
282+
role="listbox"
283+
tabindex="0"
284+
>
285+
<li
286+
aria-selected="false"
287+
class="rc-menu-item"
288+
role="option"
289+
>
290+
1
291+
</li>
292+
<li
293+
aria-selected="false"
294+
class="rc-menu-item"
295+
role="option"
296+
>
297+
2
298+
</li>
299+
<li
300+
aria-selected="false"
301+
class="rc-menu-item"
302+
role="option"
303+
>
304+
3
305+
</li>
306+
</ul>
307+
`;

0 commit comments

Comments
 (0)