Skip to content

Commit

Permalink
Merge pull request #15 from courseload/button-spaces
Browse files Browse the repository at this point in the history
Ensure buttons have onKeyDown event listeners that trigger a click event for Spacebar and Enter keys
  • Loading branch information
kloots committed May 13, 2015
2 parents 3af4d51 + 19f7c40 commit b6f1d81
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('props', () => {
<div onClick={k}><img src="#" alt="Foo"/></div>;
});
});

it('warns if there is an image with an empty alt attribute', () => {
expectWarning(assertions.props.onClick.NO_LABEL.msg, () => {
<div onClick={k}><img src="#" alt=""/></div>;
Expand All @@ -99,13 +99,13 @@ describe('props', () => {

describe('when role="button"', () => {
it('requires onKeyUp', () => {
expectWarning(assertions.props.onClick.BUTTON_ROLE_NO_KEYUP.msg, () => {
expectWarning(assertions.props.onClick.BUTTON_ROLE_SPACE.msg, () => {
<span onClick={k} role="button"/>;
});
});

it('requires onKeyDown', () => {
expectWarning(assertions.props.onClick.BUTTON_ROLE_NO_KEYDOWN.msg, () => {
expectWarning(assertions.props.onClick.BUTTON_ROLE_ENTER.msg, () => {
<span onClick={k} role="button"/>;
});
});
Expand Down
12 changes: 7 additions & 5 deletions lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,19 @@ exports.props = {
}
},

BUTTON_ROLE_NO_KEYUP: {
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.',
// onKeyUp is too late to cancel space's default behavior of scrolling the
// page.
BUTTON_ROLE_SPACE: {
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.',
test (tagName, props, children) {
return !(props.role === 'button' && !props.onKeyUp);
return !(props.role === 'button' && !props.onKeyDown);
}
},

BUTTON_ROLE_NO_KEYDOWN: {
BUTTON_ROLE_ENTER: {
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.',
test (tagName, props, children) {
return !(props.role === 'button' && !props.onKeyUp);
return !(props.role === 'button' && props.onKeyDown);
}
}

Expand Down

0 comments on commit b6f1d81

Please sign in to comment.