reactjs - jsx-a11y 当有 htmlFor 时返回 Form 标签必须有关联的控件

标签 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,我也会收到此错误消息:

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:

最佳答案

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

祝你好运!

关于reactjs - jsx-a11y 当有 htmlFor 时返回 Form 标签必须有关联的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44951806/

相关文章:

javascript - react 前端连接到 flask 后端 Howto

iphone - 虚拟乐器 iPhone 应用程序中的 VoiceOver 辅助功能?

javascript - 浏览器 Zoom 上的 ScaleX() 问题

reactjs - 如何使 TypeScript 界面 Prop 依赖于同一界面中的另一个 Prop ?

reactjs - 未捕获的类型错误 : Cannot read property 'forEach' of undefined

javascript - 将三个组件变成两个组件

c - 将程序直接重定向到 eclipse 控制台

ios - 如何设置可访问性容器以使手势正常工作?

swift - 如何在 Swift 中使用 AXObserverAddNotification?

javascript - React 组件 props 在应该改变的时候没有改变?