Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsx-a11y returning Form label must have associated control when there is an htmlFor

I have this component:

// @flow
import React, { Element } from 'react';
import styles from './Label.scss';
import cs from 'classnames';

export const Label = ({
  id,
  htmlFor,
  invalid,
  required,
  children
}: {
  id: string,
  htmlFor: string,
  invalid: boolean,
  required: boolean,
  children: Element<*>
}) =>
  <label
    htmlFor={htmlFor}
    id={id}
    required={required}
    className={cs(
      styles.label,
      required ? styles.required : '',
      invalid ? styles.invalid : ''
    )}
  >
    {children}
  </label>;

Label.displayName = 'Label';

When I run eslint I get this error message even though there is an htmlFor:

error: Form label must have associated control (jsx-a11y/label-has-for) at packages/ds-component-library/src/components/atoms/Label/Label.js:19:3:

like image 613
dagda1 Avatar asked Jul 06 '17 14:07

dagda1


1 Answers

In your .eslintrc, try the following:

"rules": {
        "jsx-a11y/label-has-for": [ 2, {
            "components": [ "Label" ],
            "required": {
                "some": [ "nesting", "id" ]
            },
            "allowChildren": false,
        }]
    }

This is coming from: https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/label-has-for.md

Good luck!

like image 101
Joe Avatar answered Nov 14 '22 23:11

Joe