jsx-a11y返回表单标签必须具有关联控件才有htmlFor

时间:2017-07-06 14:29:43

标签: reactjs accessibility

我有这个组件:

// @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';

当我运行eslint时,即使有htmlFor

,我也会收到此错误消息
  

错误:表单标签必须具有关联控件   (jsx-a11y / label-has-for)at   包/ D​​S-组件库/ SRC /组件/原子/标签/ Label.js:19:3:

1 个答案:

答案 0 :(得分:12)

在.eslintrc中,尝试以下操作:

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

这来自:https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/label-has-for.md

祝你好运!