我有这个组件:
// @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 包/ DS-组件库/ SRC /组件/原子/标签/ Label.js:19:3:
答案 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
祝你好运!