mui / mui/material-ui

[TextField] Notched, outlined label incorrect when using InputLabelProps to override `required` prop

Open
#32,934 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

scope: text field support: question
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

Duplicates
  • I have searched the existing issues
Latest version
  • I have tested the latest version
Current behavior 😯

We have a form with all required fields. We would like to remove the * added to required fields as it is redundant when all fields are required. However, we still want to leverage the native HTML validation on input elements.

In sum, we'd like to set required={true} on TextInput (and others), but not render an asterisk in the label.

This is almost achievable by passing InputLabelProp={{required: false}}. However, for outlined inputs, the notch is still rendered as if the asterisk is present.

Screen Shot 2022-05-27 at 12 11 29 PM

(Repro: https://codesandbox.io/s/elated-lewin-oge4dl?file=/index.js)

Note the extra space next to the label.

Expected behavior 🤔

Ideally, if InputLabelProps={{required: false}} is set, the notched label would not render the extra space where the asterisk would be:

Screen Shot 2022-05-27 at 12 16 31 PM
Steps to reproduce 🕹

Steps:

  1. Create a TextField with props required={true} and InputLabelProps={{required: false}}
  2. Note the extra space in the label

https://codesandbox.io/s/elated-lewin-oge4dl?file=/index.js

Context 🔦

I think a broader ability to customize the asterisk rendered on labels for required inputs is most desirable.

Your environment 🌎
`npx @mui/envinfo`
  System:
    OS: macOS 12.3.1
  Binaries:
    Node: 16.13.2 - ~/.nvm/versions/node/v16.13.2/bin/node
    Yarn: 1.22.15 - ~/.nvm/versions/node/v16.13.2/bin/yarn
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.2/bin/npm
  Browsers:
    Chrome: 101.0.4951.64
    Edge: Not Found
    Firefox: 98.0.1
    Safari: 15.4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the index.js reproduction linked in the issue, then trace how TextField passes InputLabelProps through the outlined label path. Verify that required={true} still enables native validation while required: false prevents the extra notch space; confirm the result against the provided reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.