跳转到内容

useKeyWithClickEvents

Diagnostic Category: lint/a11y/useKeyWithClickEvents

Since: v1.0.0

Sources:

Enforce onClick is accompanied by at least one of the following: onKeyUp, onKeyDown, onKeyPress.

Coding for the keyboard is important for users with physical disabilities who cannot use a mouse, AT compatibility, and screenreader users. This does not apply for interactive or hidden elements.

<div onClick={() => {}} />
code-block.jsx:1:1 lint/a11y/useKeyWithClickEvents ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Enforce to have the onClick mouse event with the onKeyUp, the onKeyDown, or the onKeyPress keyboard event.

> 1 │ <div onClick={() => {}} />
^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

Actions triggered using mouse events should have corresponding keyboard events to account for keyboard-only navigation.

<div onClick={() => {}} onKeyDown={handleKeyDown} />
<div onClick={() => {}} onKeyUp={handleKeyUp} />
<div onClick={() => {}} onKeyPress={handleKeyPress} />
// this rule doesn't apply to user created component
<MyComponent onClick={() => {}} />
<div onClick={() => {}} {...spread}></div>
<div {...spread} onClick={() => {}} ></div>
<button onClick={() => console.log("test")}>Submit</button>