question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Label for inputs should be associated via wrapping, not "for" attribute

See original GitHub issue

Not sure if this has been asked before (I wasn’t able to find an issue for it). HTML5 allows two ways to associate a label with an input field. The first one is via the “for” attribute and an id, like

<div>
 <label for="myInput">My Input</label>
 <input type="text" id="myInput" />
</div>

The other is to use the label as a wrapper, like

<label>
 My Input
 <input type="text" />
</label>

When using the first method if no id is set the browser does not know about the relation between the label and the input. This isn’t ideal for accessibility reasons. It’s also not ideal when using “Testing Library” with the getByLabelText method to get the input - which simply won’t return the correct input as there is no obvious relation.

Current versions of material ui input components use the first method. I propose to switch to the label wrapping method. I see two major benefits from this:

  1. Using hierarchy alone, the browser knows which labels refer to which input, which improves accessibility.
  2. Given the first reason there is no reason to set Ids anymore. This is great because the dom tree should never contain the same id twice. Since react components are reusable this is not guaranteed when using the same component (one that has a TextField with an Id set) twice.

Possible disadvantages:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:10
  • Comments:16 (12 by maintainers)

github_iconTop GitHub Comments

4reactions
eps1loncommented, Oct 17, 2019

Having dived more into how accessible names are computed I can only see a benefit with not having to provide ids which would be quite nice since it gives less opportunity to make mistakes.

If somebody wants to work on this I would suggest making this work for

<FormControl>
  <InputLabel>
    label
    <Input variant="outlined" />
  </InputLabel>
</FormControl>

https://material-ui.com/components/text-fields/#components

Then we’ll see how we can incorporate this approach into TextField and how backwards compatibility looks.

2reactions
diegohazcommented, Jul 5, 2019

Reakit supports wrapping inputs with the native <label> (not <FormLabel>) just like one would do with HTML. The documentation uses this method specifically for checkboxes and radio buttons, but nothing prevents someone from using this for other input types.

FormLabel just exists so you can use the separate label approach without worrying about generating IDs. Reakit does this already.

But, as you noted, this is all experimental right now and may change in the future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Label for inputs should be associated via wrapping ... - GitHub
Current versions of material ui input components use the first method. I propose to switch to the label wrapping method. I see two...
Read more >
If an element is wrapped by a label, does the label require the ...
I'm not a caveman, so I know I need to associate <label> with those <input> s. I'm fond of wrapping the radio buttons...
Read more >
Labeling Controls | Web Accessibility Initiative (WAI) - W3C
Provide labels to identify all form controls, including text fields, checkboxes, radio buttons, and drop-down menus. In most cases, this is done by...
Read more >
HTML Inputs and Labels: A Love Story | CSS-Tricks
One is by wrapping the input in a label (implicit), and the other is by adding a for attribute to the label and...
Read more >
Form <input> elements must have labels | Axe Rules
Form elements that should have labels​​ Buttons - buttons are self-labeling. Hidden inputs - Inputs with the type attribute value of hidden (e.g.,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found