我有这个组件:
// @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/