Skip to content

Commit b6f1d81

Browse files
committed
Merge pull request #15 from courseload/button-spaces
Ensure buttons have onKeyDown event listeners that trigger a click event for Spacebar and Enter keys
2 parents 3af4d51 + 19f7c40 commit b6f1d81

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

lib/__tests__/index-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe('props', () => {
8989
<div onClick={k}><img src="#" alt="Foo"/></div>;
9090
});
9191
});
92-
92+
9393
it('warns if there is an image with an empty alt attribute', () => {
9494
expectWarning(assertions.props.onClick.NO_LABEL.msg, () => {
9595
<div onClick={k}><img src="#" alt=""/></div>;
@@ -99,13 +99,13 @@ describe('props', () => {
9999

100100
describe('when role="button"', () => {
101101
it('requires onKeyUp', () => {
102-
expectWarning(assertions.props.onClick.BUTTON_ROLE_NO_KEYUP.msg, () => {
102+
expectWarning(assertions.props.onClick.BUTTON_ROLE_SPACE.msg, () => {
103103
<span onClick={k} role="button"/>;
104104
});
105105
});
106106

107107
it('requires onKeyDown', () => {
108-
expectWarning(assertions.props.onClick.BUTTON_ROLE_NO_KEYDOWN.msg, () => {
108+
expectWarning(assertions.props.onClick.BUTTON_ROLE_ENTER.msg, () => {
109109
<span onClick={k} role="button"/>;
110110
});
111111
});

lib/assertions.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,19 @@ exports.props = {
6868
}
6969
},
7070

71-
BUTTON_ROLE_NO_KEYUP: {
72-
msg: 'You have `role="button"` but did not define an `onKeyUp` handler. Add it, and have the "Space" key do the same thing as an `onClick` handler.',
71+
// onKeyUp is too late to cancel space's default behavior of scrolling the
72+
// page.
73+
BUTTON_ROLE_SPACE: {
74+
msg: 'You have `role="button"` but did not define an `onKeyDown` handler. Add it, and have the "Space" key do the same thing as an `onClick` handler.',
7375
test (tagName, props, children) {
74-
return !(props.role === 'button' && !props.onKeyUp);
76+
return !(props.role === 'button' && !props.onKeyDown);
7577
}
7678
},
7779

78-
BUTTON_ROLE_NO_KEYDOWN: {
80+
BUTTON_ROLE_ENTER: {
7981
msg: 'You have `role="button"` but did not define an `onKeyDown` handler. Add it, and have the "Enter" key do the same thing as an `onClick` handler.',
8082
test (tagName, props, children) {
81-
return !(props.role === 'button' && !props.onKeyUp);
83+
return !(props.role === 'button' && props.onKeyDown);
8284
}
8385
}
8486

0 commit comments

Comments
 (0)