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.

Checkbox with label is rendered wrong

See original GitHub issue

Issue

According to the readme it’s possible to just add a standard <label> element after the checkbox.

        <Checkbox
          nativeControlId='my-checkbox'
          checked={this.state.checked}
          indeterminate={this.state.indeterminate}
          onChange={(e) => this.setState({
            checked: e.target.checked,
            indeterminate: e.target.indeterminate})
          }
        />
        <label htmlFor='my-checkbox'>My Checkbox</label>

Will render a checkbox looking like this, with the label wrongly positioned and wrong font: image

Example code here: https://stackblitz.com/edit/react-wpvkxv

Possible solution

It’s missing the Form Field CSS. There is no Form Field React component so this is the best solution I found but it seems wrong to have to do it like this:

  • add form field CSS from package @material/form-field
  • use a wrapping div around the <Checkbox> and <label> with the class “mdc-form-field”

Then it looks as expected: image

Example code here: https://stackblitz.com/edit/react-stbwsz?file=Checkbox.js

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
moog16commented, Jun 13, 2019

@seriema yes, adding the form field component is the correct solution. We never created the form-field component since it is just styles. But if you’d like to create a component for it please feel free 😃

0reactions
lbellcommented, Sep 3, 2019

import "@material/form-field/dist/mdc.form-field.css";

for css – apparently.

(Found this all woefully confusing.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

label not working with checkbox - Stack Overflow
I believe the label element links to the id attribute, not the name attribute. Try this: <form> <input type="checkbox" name="showRatings" id="showRatings" ...
Read more >
HTML is not rendered in checkbox labels [#2920854] - Drupal
Checkbox labels with HTML code are not rendered, because the template outputs {{ input_title }}, but {{ label }} is the correct output....
Read more >
Element: <oj-checkboxset> - Oracle
If user-assistance-density is 'compact', it will show on the label as an icon. In the Alta theme the input's label will render a...
Read more >
<input>: The Input (Form Input) element - HTML
<label for="name">Name (4 to 8 characters):</label> ... checkbox, A check box allowing single values to be selected/deselected.
Read more >
Checkbox | Primer React
<FormControl.Label>Default checkbox</FormControl.Label> ... checkbox state should be used if the input value is neither true nor false.
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