Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[material-ui][Select] displayEmpty=true renders label over value #42148

Closed
rgavrilov opened this issue May 6, 2024 · 3 comments
Closed

[material-ui][Select] displayEmpty=true renders label over value #42148

rgavrilov opened this issue May 6, 2024 · 3 comments
Assignees
Labels
component: select This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material support: commercial support: Stack Overflow Please ask the community on Stack Overflow

Comments

@rgavrilov
Copy link

rgavrilov commented May 6, 2024

The problem in depth

When MenuItem value is empty string, see code below, form control label is rendered over the selected item content.

                <FormControl sx={{ minWidth: 300 }} size={'small'}>
                    <InputLabel id={'calculatorLabel'}>Calculator</InputLabel>
                    <Select
                        labelId={'calculatorLabel'}
                        value={calculator}
                        label={'Calculator'}
                        onChange={(e) => setCalculator(e.target.value)}
                        displayEmpty={true}
                    >
                        <MenuItem value={''}>
                            <em>All</em>
                        </MenuItem>
                        {_.map(calculatorsQuery.data, (c) => (
                            <MenuItem key={c.name} value={c.name}>
                                {c.name}
                            </MenuItem>
                        ))}
                    </Select>
                </FormControl>

image

Your environment

`npx @mui/envinfo`
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: Select displayEmpty
Order ID: 52426

Search keywords:

@rgavrilov rgavrilov added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: commercial labels May 6, 2024
@zannager zannager transferred this issue from mui/mui-x May 6, 2024
@zannager zannager added component: menu This is the name of the generic UI component, not the React module! component: FormControl The React component labels May 6, 2024
@danilo-leal danilo-leal changed the title Select with displayEmpty=true renders label over value [material-ui][Select] displayEmpty=true renders label over value May 7, 2024
@danilo-leal danilo-leal added component: select This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material and removed component: FormControl The React component labels May 7, 2024
@yamahmed
Copy link

yamahmed commented May 9, 2024

Hey @rgavrilov i have two suggestions you can try:

1)You can provide a default non-empty value for the Select component.
2)You can conditionally render the InputLabel based on whether the selected value is empty or not.
See whatever works best for you.

@mj12albert mj12albert added status: waiting for author Issue with insufficient information and removed component: menu This is the name of the generic UI component, not the React module! status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels May 13, 2024
@mj12albert
Copy link
Member

@rgavrilov When you use displayEmpty, you must manually control the InputLabels shrunk state: https://mui.com/material-ui/api/select/#select-prop-displayEmpty

You should also use renderValue to ensure something is rendered when your option with empty value is selected: https://mui.com/material-ui/api/select/#select-prop-renderValue

<FormControl>
  <InputLabel shrink={val || val === ''}>
    Calculator
  </InputLabel>
  <Select
    value={val}
    label="Calculator"
    onChange={(e) => setVal(e.target.value)}
    displayEmpty
    renderValue={(val) => {
      if (val === '') {
        return 'All options';
      }
      return val;
    }}
  >
    <MenuItem value="">
      All
    </MenuItem>
    <MenuItem value="B">
      B
    </MenuItem>
    <MenuItem value="C">
      C
    </MenuItem>
  </Select>
</FormControl>

Here's a working demo: https://stackblitz.com/edit/mcve-react-material-ui-rx1lgs?file=src%2FApp.tsx

@mj12albert mj12albert added support: Stack Overflow Please ask the community on Stack Overflow and removed status: waiting for author Issue with insufficient information labels May 13, 2024
Copy link

👋 Thanks for using our open-source projects!

We use GitHub issues exclusively as a bug and feature requests tracker, however,
this issue appears to be a support request.

For support with Material UI please check out https://mui.com/material-ui/getting-started/support/. Thanks!

If you have a question on Stack Overflow, you are welcome to link to it here, it might help others.
If your issue is subsequently confirmed as a bug, and the report follows the issue template, it can be reopened.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: select This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material support: commercial support: Stack Overflow Please ask the community on Stack Overflow
Projects
None yet
Development

No branches or pull requests

5 participants