Skip to content

Commit ce7f037

Browse files
author
Erin Doyle
committed
Renamed and modified button-role-space to click-events-have-key-events to be more consistent with the rule in the the eslint-plugin-jsx-a11y library
1 parent 3e9e4b5 commit ce7f037

4 files changed

Lines changed: 51 additions & 55 deletions

File tree

docs/rules/button-role-space.md

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# click-events-have-key-events
2+
3+
Enforce onClick is accompanied by at least one of the following: onKeyUp, onKeyDown,
4+
onKeyPress. Coding for the keyboard is important for users with physical disabilities
5+
who cannot use a mouse, AT compatibility, and screenreader users.
6+
7+
8+
## options
9+
10+
*This rule takes no options*
11+
12+
## Passes
13+
14+
```jsx harmony
15+
// passes when there is an `onClick` handler and there is an `onKey*` handler.
16+
<div onClick={fn} onKeyDown={this.handleKeyDown} />
17+
<div onClick={fn} onKeyUp={this.handleKeyUp} />
18+
<div onClick={fn} onKeyPress={this.handleKeyPress} />
19+
20+
// passes when the element is aria-hidden
21+
<div onClick={fn} aria-hidden="true"></div>
22+
```
23+
24+
## Fails
25+
26+
```jsx harmony
27+
// fails when there is an `onClick` handler and no `onKey*` is present
28+
<div onClick={fn}></div>
29+
```
30+
31+
## See also
32+
33+
- [This document](https://www.w3.org/WAI/GL/wiki/Making_actions_keyboard_accessible_by_using_keyboard_event_handlers_with_WAI-ARIA_controls) from w3.org
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,53 @@ import {
66
} from '../util';
77

88
export default {
9-
msg: 'You have `role="button"` but did not define an `onKeyDown` or `onKeyPress` handler. '
9+
msg: 'You have an `onClick` handler but did not define an `onKeyDown`, `onKeyUp` or `onKeyPress` handler. '
1010
+ 'Add it, and have the "Space" key do the same thing as an `onClick` handler.',
1111
url: 'https://www.w3.org/WAI/GL/wiki/Making_actions_keyboard_accessible_by_using_keyboard_event_handlers_with_WAI-ARIA_controls',
1212
affects: [
1313
devices.keyboardOnly
1414
],
1515
test(tagName, props) {
1616
const hidden = hiddenFromAT(props);
17-
const button = props.role === 'button';
17+
const onClick = listensTo(props, 'onClick');
1818
const onKeyDown = listensTo(props, 'onKeyDown');
19+
const onKeyUp = listensTo(props, 'onKeyUp');
1920
const onKeyPress = listensTo(props, 'onKeyPress');
2021

2122
// rule passes when element is hidden,
22-
// has role='button' and has an onKeyDown or onKeyPress prop
23-
return hidden || !button || onKeyDown || onKeyPress;
23+
// has onClick and has an onKeyDown, onKeyUp or onKeyPress prop
24+
return hidden || !onClick || onKeyDown || onKeyUp || onKeyPress;
2425
}
2526
};
2627

2728
export const pass = [{
28-
when: 'role="button" but there is an onKeyDown handler.',
29+
when: 'there is an onClick handler and there is an onKeyDown handler.',
2930
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
30-
render: React => <div role="button" onKeyDown={fn} />
31+
render: React => <div onClick={fn} onKeyDown={fn} />
3132
}, {
32-
when: 'role="button" but there is an onKeyPress handler.',
33+
when: 'there is an onClick handler and there is an onKeyUp handler.',
3334
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
34-
render: React => <div role="button" onKeyPress={fn} />
35+
render: React => <div onClick={fn} onKeyUp={fn} />
3536
}, {
36-
when: 'there is no role',
37-
render: React => <div >derp</div>
37+
when: 'there is an onClick handler and there is an onKeyPress handler.',
38+
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
39+
render: React => <div onClick={fn} onKeyPress={fn} />
3840
}, {
39-
when: 'there the role is not button',
40-
// eslint-disable-next-line jsx-a11y/aria-role
41-
render: React => <div role="foo" />
41+
when: 'there is no onClick',
42+
render: React => <div >derp</div>
4243
}, {
4344
when: 'the element is aria-hidden',
4445
render: React => <div aria-hidden role="button" />
4546
}];
4647

4748
export const fail = [{
48-
when: 'role="button" and no `onKeyDown` or `onKeyPress` is present',
49-
render: React => <div role="button" />
49+
when: 'there is an onClick handler but no `onKeyDown`, `onKeyUp` or `onKeyPress` is present',
50+
render: React => <div onClick={fn} />
5051
}];
5152

5253
export const description = `
5354
Enforce that elements which have the \`role="button"\`
5455
also have an \`onKeyDown\` or \`onKeyPress\` handler that handles Space or Enter
55-
(this is isn't actually checked) for poeple that are using a
56+
(this is isn't actually checked) for people that are using a
5657
keyboard-only device.
5758
`;

src/rules/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable global-require */
22
export default {
3-
'button-role-space': require('./button-role-space').default,
3+
'click-events-have-key-events': require('./click-events-have-key-events').default,
44
'hidden-uses-tabindex': require('./hidden-uses-tabindex').default,
55
'img-uses-alt': require('./img-uses-alt').default,
66
'label-has-for': require('./label-has-for').default,

0 commit comments

Comments
 (0)