Switch input labels to use <label> element
See original GitHub issueHeya @hharnisc @alvarezmelissa87 @stevemdixon!
Just a heads up on an axe-core error I’m seeing while adding the tests - it looks like we’re using a div element for the input labels instead of a <label>
const renderLabel = ({ label }) => (
label ? (
<div style={labelStyle}>
<Text>{label}</Text>
</div>
) : null
);
I wonder if instead of using a <Text>
component for this we could use the native <label>
component? That way screen readers have an easier time knowing which label is associated to which input.
Here’s an example of how we might do that:
const renderLabel = ({ label }) => (
label ? (
<label for={id}>{label}</label>
) : null
);
The only thing tricky about this is that I believe we’ll need a unique id for each instance of the input in order to associate the label with a particular <input>
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
The Label element - HTML: HyperText Markup Language | MDN
To explicitly associate a <label> element with an <input> element, you first need to add the id attribute to the <input> element. Next,...
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
There are two ways to pair a label and an input. One is by wrapping the input in a label (implicit), and the...
Read more >Should I put input elements inside a label element?
Checkboxes and radios use are built to support HTML-based form validation and provide concise, accessible labels. As such, our <input> s and <label>...
Read more >Form <input> elements must have labels | Axe Rules
Programmatically associate labels with all form controls. The recommended method for most circumstances is to use the label element and an explicit ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think it’s time we create a
Label
component 😄Seems like a great idea to always have a label (visible or otherwise!). Pretty cool that the label can be associated with the input with nesting too!
I think it will keep things simpler if we use the
for
andid
method since they’re decoupled and give us some freedom around how to show and hide components.