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.

[TextField] `InputLabelProps={{ required: true }}` renders incorrectly

See original GitHub issue

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

The asterisk used for required TextField components renders differently if using InputLabelProps={{ required: true }} to using required. The former is useful if you want to roll your own validation.

image

It’s subtle but the spacing to the right of the asterisk is missing on the second TextField.

So this is OK:

<TextField required />

This has the missing spacing:

<TextField InputLabelProps={{ required: true }} />

Expected behavior 🤔

Using required or InputLabelProps={{ required: true }} should render the asterisk consistently.

Steps to reproduce 🕹

https://codesandbox.io/s/clever-frost-fy71ov?file=/src/App.tsx

Context 🔦

No response

Your environment 🌎

`npx @mui/envinfo`
  System:
    OS: macOS 10.15.7
  Binaries:
    Node: 16.13.2 - ~/.nvm/versions/node/v16.13.2/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.2/bin/yarn
    npm: 8.5.2 - ~/.nvm/versions/node/v16.13.2/bin/npm
  Browsers:
    Chrome: 99.0.4844.51
    Edge: Not Found
    Firefox: 96.0.2
    Safari: 14.1.2
  npmPackages:
    @emotion/react: ^11.8.2 => 11.8.2 
    @emotion/styled: ^11.8.1 => 11.8.1 
    @mui/base:  5.0.0-alpha.71 
    @mui/material: ^5.5.0 => 5.5.0 
    @mui/private-theming:  5.4.4 
    @mui/styled-engine:  5.4.4 
    @mui/system:  5.5.0 
    @mui/types:  7.1.2 
    @mui/utils:  5.4.4 
    @types/react: ^17.0.33 => 17.0.40 
    react: ^17.0.2 => 17.0.2 
    react-dom: ^17.0.2 => 17.0.2 
    typescript: ^4.5.4 => 4.6.2 

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
robcaldecottcommented, Apr 6, 2022

@michaldudak The use case is custom validation. If you pass required to an input then the built-in browser HTML UI will appear when you try and submit the form - which we do not want. We want to use our own UI for required fields whilst keeping the TextField asterisk.

image

So we want the label to look exactly the same but without the required attribute on the underlying input.

HTH.

0reactions
michaldudakcommented, Apr 6, 2022

You can add the asterisk to the label yourself (<TextField label="Required field *" />) or override the required prop set by the input by setting it to undefined: <TextField label="Required field" inputProps={{ required: undefined }} />. Does this solve your problem?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Material-UI Outlined Input Label Incorrect Alignment
I'm having an issue with TextField labels and placeholder text rendering incorrectly with Material UI. I'm not sure why this is happening as ......
Read more >
React Text Field component - Material UI - MUI
The TextField wrapper component is a complete form control including a label, ... Standard form attributes are supported e.g. required , disabled ,...
Read more >
Input Components - React-admin - Marmelab
An Input component displays an input, or a dropdown list, a list of radio buttons, etc. ... Input components require a source prop....
Read more >
inputlabelprops= shrink true | The AI Search Engine You Control
but the problem is we need to pass it everytime we want to render the autocomplete. ... <TextField label="ML Features" InputLabelProps={{ shrink: true...
Read more >
How to conditionally render error and success messages on ...
render () { const { classes } = this.props; console.log(classes); return ( <div> <div id='notification'/> <FormControl fullWidth="true" ...
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