noLabelWithoutControl
诊断类别:lint/a11y/noLabelWithoutControl
¥Diagnostic Category: lint/a11y/noLabelWithoutControl
自从:v1.8.0
¥Since: v1.8.0
来源:
¥Sources:
强制标签元素或组件具有文本标签和相关输入。
¥Enforce that a label element or component has a text label and an associated input.
“input” 被视为以下元素之一:input
、meter
、output
、progress
、select
或 textarea
。
¥An “input” is considered one of the following elements: input
, meter
, output
, progress
, select
or textarea
.
有两种支持将标签与输入关联的方法:
¥There are two supported ways to associate a label with an input:
-
将输入封装在标签元素中。
¥Wrapping an input in a label element.
-
向标签添加
for
属性(或 React 中的htmlFor
)并为其分配与页面上的输入相关联的 DOM ID 字符串。¥Adding a
for
attribute (orhtmlFor
in React) to a label and assigning it a DOM ID string associated with an input on the page.
此规则检查任何 label
元素(或将输出 label
元素的指定自定义组件)是否满足以下条件之一:
¥This rule checks that any label
element (or an indicated custom component that will output a label
element) meets one of these conditions:
-
封装
input
元素(或将输出input
元素的指定自定义组件)¥Wraps an
input
element (or an indicated custom component that will output aninput
element) -
具有
for
或htmlFor
属性,并且label
元素/组件具有可访问的文本内容。¥Has a
for
orhtmlFor
attribute and that thelabel
element/component has accessible text content.
¥Examples
¥Invalid
code-block.jsx:1:1 lint/a11y/noLabelWithoutControl ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ A form label must be associated with an input.
> 1 │ <label for=“js_id” />;
│ ^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Consider adding an accessible text content to the label element.
code-block.jsx:1:1 lint/a11y/noLabelWithoutControl ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ A form label must be associated with an input.
> 1 │ <label for=“js_id”><input /></label>;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Consider adding an accessible text content to the label element.
code-block.jsx:1:1 lint/a11y/noLabelWithoutControl ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ A form label must be associated with an input.
> 1 │ <label htmlFor=“js_id” />;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Consider adding an accessible text content to the label element.
code-block.jsx:1:1 lint/a11y/noLabelWithoutControl ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ A form label must be associated with an input.
> 1 │ <label htmlFor=“js_id”><input /></label>;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Consider adding an accessible text content to the label element.
code-block.jsx:1:1 lint/a11y/noLabelWithoutControl ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ A form label must be associated with an input.
> 1 │ <label>A label</label>;
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Consider adding a for
or htmlFor
attribute to the label element or moving the input element to inside the label element.
code-block.jsx:1:6 lint/a11y/noLabelWithoutControl ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ A form label must be associated with an input.
> 1 │ <div><label /><input /></div>;
│ ^^^^^^^^^
2 │
ℹ Consider adding an accessible text content to the label element.
ℹ Consider adding a for
or htmlFor
attribute to the label element or moving the input element to inside the label element.
¥Valid
¥Options
该规则支持以下选项:
¥The rule supports the following options:
-
inputComponents
- 应被视为与input
元素相同的组件名称数组。¥
inputComponents
- An array of component names that should be considered the same as aninput
element. -
labelAttributes
- 应被视为label
可访问文本内容的属性数组。¥
labelAttributes
- An array of attributes that should be treated as thelabel
accessible text content. -
labelComponents
- 应被视为与label
元素相同的组件名称数组。¥
labelComponents
- An array of component names that should be considered the same as alabel
element.
选项 inputComponents
和 labelComponents
都不支持命名空间组件(例如 <Control.Input>
)。
¥Both options inputComponents
and labelComponents
don’t have support for namespace components (e.g. <Control.Input>
).
¥Related links