Skip to content

Commit d485215

Browse files
committed
Udated the error message of onclick-has-focus to stress the interactive behavior of the violating element.
1 parent 6d2ffd3 commit d485215

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

__tests__/src/rules/onclick-has-focus-test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ const parserOptions = {
2525
const ruleTester = new RuleTester();
2626

2727
const expectedError = {
28-
message: 'Interactive elements with onClick handlers must be focusable. ' +
29-
'Either set the tabIndex property to a valid value (usually 0), ' +
30-
'or use an element type which is inherently focusable such as `button`.',
28+
message: 'An non-interactive element with an onClick handler and an ' +
29+
'interactive role must be focusable. Either set the tabIndex property to ' +
30+
'a valid value (usually 0) or use an element type which is inherently ' +
31+
'focusable such as `button`.',
3132
type: 'JSXOpeningElement',
3233
};
3334

@@ -54,6 +55,7 @@ ruleTester.run('onclick-has-focus', rule, {
5455
{ code: '<input type="hidden" onClick={() => void 0} tabIndex="-1" />', parserOptions },
5556
{ code: '<input type="hidden" onClick={() => void 0} tabIndex={-1} />', parserOptions },
5657
{ code: '<input onClick={() => void 0} />', parserOptions },
58+
{ code: '<input onClick={() => void 0} role="combobox" />', parserOptions },
5759
{ code: '<button onClick={() => void 0} className="foo" />', parserOptions },
5860
{ code: '<option onClick={() => void 0} className="foo" />', parserOptions },
5961
{ code: '<select onClick={() => void 0} className="foo" />', parserOptions },
@@ -71,6 +73,7 @@ ruleTester.run('onclick-has-focus', rule, {
7173
{ code: '<a onClick={() => void 0} href="http://x.y.z" />', parserOptions },
7274
{ code: '<a onClick={() => void 0} href="http://x.y.z" tabIndex="0" />', parserOptions },
7375
{ code: '<a onClick={() => void 0} href="http://x.y.z" tabIndex={0} />', parserOptions },
76+
{ code: '<a onClick={() => void 0} href="http://x.y.z" role="button" />', parserOptions },
7477
{ code: '<TestComponent onClick={doFoo} />', parserOptions },
7578
{ code: '<input onClick={() => void 0} type="hidden" />;', parserOptions },
7679
{ code: '<span onClick="submitForm();">Submit</span>', errors: [expectedError], parserOptions },

src/rules/onclick-has-focus.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import getTabIndex from '../util/getTabIndex';
1414
// Rule Definition
1515
// ----------------------------------------------------------------------------
1616

17-
const errorMessage = 'Interactive elements with onClick handlers must be ' +
18-
'focusable. Either set the tabIndex property to a valid value (usually 0), ' +
19-
'or use an element type which is inherently focusable such as `button`.';
17+
const errorMessage =
18+
'An non-interactive element with an onClick handler and an ' +
19+
'interactive role must be focusable. Either set the tabIndex property to ' +
20+
'a valid value (usually 0) or use an element type which is inherently ' +
21+
'focusable such as `button`.';
2022

2123
const schema = generateObjSchema();
2224

0 commit comments

Comments
 (0)