I need make image tag clickable, but I get a warning by jsx-a11y, how do I fixed it, I already read no-noninteractive-element-interactions, but I do not wanna wrap other div tag around img tag, because it will make a redundancy tag, so does any other way to fix this warning?
The warning is [eslint] Non-interactive elements should not be assigned interactive roles.
and my code is
<img
styleName="pic-id-code"
src={picCodeImgSrc}
alt="pic id code"
onClick={this.refreshPicCodeImg}
onKeyPress={this.handleKeyPress}
role="button"
tabIndex="0"
/>
You should use <input type="image" /> for your use case, it is semantically correct and you won't need to add role or tabindex or any other ARIA tags to make it work and accessible.
There is a caveat as this doesn't actually return to normal state after click and remains focused so you need to call blur after your onClick logic. Here's the simple demo I made few days back to remove focus on click.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With