【问题标题】:jsx-a11y returning Form label must have associated control when there is an htmlForjsx-a11y 有htmlFor时返回Form标签必须有关联控件
【发布时间】:2017-07-06 14:29:42
【问题描述】:

我有这个组件:

// @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) 在 packages/ds-component-library/src/components/atoms/Label/Label.js:19:3:

【问题讨论】:

    标签: reactjs accessibility


    【解决方案1】:

    在您的 .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

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-10
      • 2021-11-10
      • 2020-01-18
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 2015-05-16
      相关资源
      最近更新 更多