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.

How to include placeholder in select component in Material UI@Next

See original GitHub issue
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

Select field should show Placeholder text.

Current Behavior

Placeholder text should be visible when passed to Input tag.

Your Environment

Tech Version
Material-UI v1.0.0-beta.42
React 16.1.0
browser Chrome( 65.0.3325.181)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:21 (2 by maintainers)

github_iconTop GitHub Comments

77reactions
oliviertassinaricommented, Apr 20, 2018

@NayanSrivastav This concern has already been raised in #8875. As far as the discussion went, the conclusion was that the placeholder property for a select component doesn’t make sense. With the given information, it’s not something we plan on addressing. Instead, @sakulstra has raised some great alternatives: https://github.com/mui-org/material-ui/pull/8875#issuecomment-349771804

import React from "react";
import { render } from "react-dom";
import { Select } from "material-ui";
import { withStyles } from "material-ui/styles";
import { MenuItem } from 'material-ui/Menu';
import classNames from "classnames";

class App extends React.Component {
  state = {
      select: 'none'
  }

  handleChange = field => e => {
    this.setState({[field]: e.target.value})
  }

  render() {
    return (
      <div>
        <Select native defaultValue='none'>
          <option value="none" disabled>
            uncontrolled Native placeholder
          </option>
          <option value="1">Option 1</option>
          <option value="2">Option 2</option>
          <option value="3">Option 3</option>
        </Select>
        <br />
        <Select native value={this.state.select} onChange={this.handleChange('select')}>
          <option value="none" disabled>
            controlled Native placeholder
          </option>
          <option value="1">Option 1</option>
          <option value="2">Option 2</option>
          <option value="3">Option 3</option>
        </Select>
        <br />
        <Select native value={this.state.select} onChange={this.handleChange('select')}>
          {this.state.select === 'none' && <option value="none" disabled>
            Autohide after selection
          </option>}
          <option value="1">Option 1</option>
          <option value="2">Option 2</option>
          <option value="3">Option 3</option>
        </Select>
        <br />
        <Select value={this.state.select} onChange={this.handleChange('select')}>
          <MenuItem value="none" disabled>
            controlled Non native placeholder
          </MenuItem>
          <MenuItem value="1">Option 1</MenuItem>
          <MenuItem value="2">Option 2</MenuItem>
          <MenuItem value="3">Option 3</MenuItem>
        </Select>
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

https://codesandbox.io/s/wqq7059oq8

33reactions
slamjscommented, Jul 21, 2021

you can use the following

displayEmpty={true}
renderValue={value => value?.length ? Array.isArray(value) ? value.join(', ') : value : 'placeholder'}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create a select component with placeholder in React
Suppose you want a select component with a placeholder that will not be ... import Select from '@material-ui/core/Select'; import grey from ...
Read more >
javascript - <Select> Placeholder styling
Styling via a className on the MenuItem does not work because the default display of the selected menu item displays its children.
Read more >
React Select component - Material UI
The first step is to style the InputBase component. Once it's styled, you can either use it directly as a text field or...
Read more >
Select displayEmpty placeholder
Select displayEmpty placeholder. https://github.com/mui-org/material-ui/blob/master/docs/src/pages/components/selects/SimpleSelect.js.
Read more >
How to set a Placeholder on a Select tag in React
To set a placeholder on a Select tag in React, set the first option element of the select tag to `disabled` and give...
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