Eslint-plugin-jsx-a11y: Checkbox false negative with control-has-associated-label

Created on 13 Jun 2020  路  5Comments  路  Source: jsx-eslint/eslint-plugin-jsx-a11y

Based on the docs I think my code should pass the control-has-associated-label checks but I'm currently getting the following warning:

warning  A control must be associated with a text label  jsx-a11y/control-has-associated-label

Here's my code:

<label htmlFor={`check_${item.label.toLowerCase()}`}>
      <input type="checkbox" onClick={(event) => userFilter(item.value, refine)} id={`check_${item.label.toLowerCase()}`} />
      {item.label}
</label>

I've just started getting this warning having upgraded to v6.2.3.

bug help wanted

Most helpful comment

@theAdhocracy, try the latest release. I tried your case as a passing case for control-has-associated-label and it's passing.

Screen Shot 2020-06-20 at 2 50 51 PM

All 5 comments

That is indeed correct; i suspect the dynamic ID in a template literal is being overlooked by the rule.

Out of interest I tried changing both values for htmlFor and id to simply foo but the warning persisted. Obviously that wouldn't be a useable fix anyway, but could that suggest the dynamic ID isn't the issue?

Yes, that would suggest that.

Can you provide the full component, and the full error message?

Sure. I've removed everything unnecessary from the component and still getting the error message here:

import React from "react"
import { connectRefinementList } from "react-instantsearch-dom"

import styles from "./search.module.css"

const CategoryFilter = ({ items }) => {
   return (
      <ul className={styles.category_list}>
         {items.map((item) => (
            <li key={item.label}>
               <label htmlFor={`check_${item.label.toLowerCase()}`}>
                  <input type="checkbox" id={`check_${item.label.toLowerCase()}`} />
                  {item.label}
               </label>
            </li>
         ))}
      </ul>
   )
}

export const CustomCategoryFilter = connectRefinementList(CategoryFilter)

Error message:
12:7 warning A control must be associated with a text label jsx-a11y/control-has-associated-label

@theAdhocracy, try the latest release. I tried your case as a passing case for control-has-associated-label and it's passing.

Screen Shot 2020-06-20 at 2 50 51 PM

Was this page helpful?
0 / 5 - 0 ratings

Related issues

markduan picture markduan  路  5Comments

jessebeach picture jessebeach  路  6Comments

AlmeroSteyn picture AlmeroSteyn  路  5Comments

calinoracation picture calinoracation  路  3Comments

astorije picture astorije  路  3Comments