jsx-a11y/control-has-associated-label

🚫 This rule is disabled in the following configs: ☑️ recommended, 🔒 strict.

Enforce that a control (an interactive element) has a text label.

There are two supported ways to supply a control with a text label:

The rule is permissive in the sense that it will assume that expressions will eventually provide a label. So an element like this will pass.

jsx <button type="button">{maybeSomethingThatContainsALabel}</button>

How do I resolve this error?

Case: I have a simple button that requires a label.

Provide text content in the button element.

jsx <button type="button">Save</button>

Case: I have an icon button and I don't want visible text.

Use the aria-label attribute and provide the text label as the value.

jsx <button type="button" aria-label="Save" class="icon-save" />

Case: The label for my element is already located on the page and I don't want to repeat the text in my source code.

Use the aria-labelledby attribute and point the IDREF value to an element with an accessible label.

```jsx

Comment

```

Case: My label and input components are custom components, but I still want to require that they have an accessible text label.

You can configure the rule to be aware of your custom components. Refer to the Rule Details below.

jsx <CustomInput label="Surname" type="text" value={value} />

Rule options

This rule takes one optional object argument of type object:

json { "rules": { "jsx-a11y/control-has-associated-label": [ 2, { "labelAttributes": ["label"], "controlComponents": ["CustomComponent"], "ignoreElements": [ "audio", "canvas", "embed", "input", "textarea", "tr", "video", ], "ignoreRoles": [ "grid", "listbox", "menu", "menubar", "radiogroup", "row", "tablist", "toolbar", "tree", "treegrid", ], "depth": 3, }], } }

Succeed

jsx <button type="button" aria-label="Save" class="icon-save" />

Fail

jsx <button type="button" class="icon-save" />

Accessibility guidelines