Skip to content

Commit 931c4ac

Browse files
committed
Add docs
1 parent 6a60a06 commit 931c4ac

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Prefer using `onSelect` instead of `onClick` for `ActionListItem` components (`prefer-action-list-item-onselect`)
2+
3+
🔧 The `--fix` option on the [ESLint CLI](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.
4+
5+
## Rule details
6+
7+
When using the `onClick` attribute on `ActionList.Item` components, this callback only fires when a user clicks on the element with a mouse. If the user navigates to the element with a keyboard and presses the `Enter` key, the callback will not fire. This produces an inaccessible experience for keyboard users.
8+
9+
Using `onSelect` will lead to a more accessible experience for keyboard users compared to using `onClick`.
10+
11+
This rule is generally auto-fixable, though you may encounter type checking errors that result from not properly handling keyboard events which are not part of the `onSelect` callback signature.
12+
13+
👎 Examples of **incorrect** code for this rule:
14+
15+
```jsx
16+
<ActionList.Item onClick={handleClick} />
17+
<ActionList.Item
18+
aria-label="Edit item 1"
19+
onClick={(event) => {
20+
event.preventDefault()
21+
handleClick()
22+
}}
23+
/>
24+
```
25+
26+
👍 Examples of **correct** code for this rule:
27+
28+
```jsx
29+
<ActionList.Item onSelect={handleClick} />
30+
<ActionList.Item
31+
aria-label="Edit item 1"
32+
onSelect={(event) => {
33+
event.preventDefault()
34+
handleClick()
35+
}}
36+
/>
37+
```

src/rules/__tests__/prefer-action-list-item-onselect.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ ruleTester.run('prefer-action-list-item-onselect', rule, {
4040
errors: [{messageId: 'prefer-action-list-item-onselect'}],
4141
output: `<ActionList.Item aria-label="Edit item 1" onSelect={onClick} />`,
4242
},
43+
{
44+
code: `<ActionList.Item
45+
aria-label="Edit item 1"
46+
onClick={(event) => {
47+
event.preventDefault()
48+
handleClick()
49+
}}
50+
/>`,
51+
errors: [{messageId: 'prefer-action-list-item-onselect'}],
52+
output: `<ActionList.Item
53+
aria-label="Edit item 1"
54+
onSelect={(event) => {
55+
event.preventDefault()
56+
handleClick()
57+
}}
58+
/>`,
59+
},
4360
// This is invalid, but we can fix it anyway
4461
{
4562
code: `<ActionList.Item aria-label="Edit item 1" onClick />`,

0 commit comments

Comments
 (0)