18

I have the following code:

Enabled = (id) => {
  let removal = null;
  if (!this.props.disabled) {
    removal = (
      <span
        className="chipRemove"
        onClick={() => this.onDelete(id)}
      >
        x
      </span>)
    ;
  }
  return removal;
}

it works well, but linter is giving me:

jsx-a11y/no-static-element-interactions

How can I solve this error (according to jsx-a11y)?

FacundoGFlores
  • 7,388
  • 10
  • 57
  • 92

3 Answers3

26

From Doc:

Enforce non-interactive DOM elements have no interactive handlers. Static elements such as <div> and <span> should not have mouse/keyboard event listeners. Instead use something more semantic, such as a button or a link.

Valid interactive elements are:

<a> elements with href or tabIndex props
<button> elements
<input> elements that are not hidden
<select> and <option> elements
<textarea> elements
<area> elements

Reference: https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-static-element-interactions.md

Mayank Shukla
  • 92,299
  • 16
  • 152
  • 142
15

resolve this error by providing ...role="button" and tabIndex

<div
  onClick={onClickHandler}
  onKeyPress={onKeyPressHandler}
  role="button"
  tabIndex="0">
 Save
</div>
D V Yogesh
  • 2,553
  • 1
  • 24
  • 36
4

From eslint suggested documentation, it was important to learn about this:

Case: this element is not a button, link, menuitem, etc. It is catching bubbled events from elements that it contains

If your element is catching bubbled click or key events from descendant elements, then the proper role for this element is presentation.

<div
  onClick={onClickHandler}
  role="presentation">
  <button>Save</button>
</div>
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
lucascavalcante
  • 1,166
  • 1
  • 10
  • 28