【问题标题】:Set a placeholder value to Select component Material UI v1.0.0-beta.24设置占位符值以选择组件 Material UI v1.0.0-beta.24
【发布时间】:2017-12-18 22:05:00
【问题描述】:

我在一个测试项目中使用 Material UI 中的 v1.0.0-beta.24,“下拉”菜单的工作方式与以前的版本不同,所以我想做的是在 Select 组件中设置一个占位符.

在我以前版本的示例中,我有这个:

<DropDownMenu
  value={this.state.DivisionState}
  onChange={this.handleChangeDivision}>
  <MenuItem value={''} primaryText={'Select division'} />
  {this.renderDivisionOptions()}
</DropDownMenu>

所以新版本不支持primaryText属性,这是我的代码:

import React from 'react';
import Select from 'material-ui/Select';
import {MenuItem, MenuIcon} from 'material-ui/Menu';
import {DatePicker} from 'material-ui-pickers';
import { FormControl } from 'material-ui/Form';
import { InputLabel } from 'material-ui/Input';


import cr from '../styles/general.css';

export default class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = {

      DivisionData: [],
      DivisionState: '',

    };
    this.renderDivisionOptions = this.renderDivisionOptions.bind(this);
    this.handleChangeDivision = this.handleChangeDivision.bind(this);

  }
  componentDidMount() {
    const divisionWS = 'http://localhost:8080/services/Divisions/getAll';

    fetch(divisionWS)
      .then(Response => Response.json())
      .then(findResponse => {
        console.log(findResponse);

        this.setState({
          DivisionData: findResponse.divisions,
        });
      });

  }
  handleChangeDivision(event, index, value) {
    this.setState({ DivisionState: event.target.value});

  }
  renderDivisionOptions() {
    return this.state.DivisionData.map((dt, i) => {
      return (
        <MenuItem
          key={i}
          value={dt.divDeptShrtDesc}>
          {dt.divDeptShrtDesc}

        </MenuItem>
      );
    });
  }
  render() {
    return (

      <div className={cr.container}>
        <div className={cr.containerOfContainers}>
          <div className={cr.inputContainer}>
            <div className={cr.rows}>
              <div>
                <div>
                  <FormControl>
                    <InputLabel htmlFor="name-multiple">Division</InputLabel>
                      <Select
                        value={this.state.DivisionState}
                        onChange={this.handleChangeDivision}
                        autoWidth={false}
                      >
                        {this.renderDivisionOptions()}
                      </Select>
                  </FormControl>
                </div>
                </div>
              </div>
            </div>
          </div>
        </div>
    );
  }
}

所以在这方面的任何帮助都会很好。

问候。

【问题讨论】:

  • 拥有 this.state.divisionState = '你的占位符文本'。或者创建一个自定义的this.state.placeholderText:'example input',如果DivisionData为空有条件使用
  • 您好,感谢您的宝贵时间。问题是它不是空的,选择填充了我运行应用程序时加载的 Web 服务......
  • 在您的 .map 之上,但仍在该函数内部硬编码单个菜单项以用作占位符... '占位符文本'

标签: javascript reactjs material-ui


【解决方案1】:

只需在 Select 中提供以下属性

displayEmpty
renderValue={value !== "" ? undefined : () => "placeholder text"}

用你的变量替换值。

【讨论】:

    【解决方案2】:

    您正在使用带有htmlFor="name-multiple" 的 InputLabel,但没有使用该名称的输入。您需要通过在 Select 上设置 input 属性来提供一个:

      <FormControl>
        <InputLabel htmlFor="name-multiple">Division</InputLabel>
          <Select
            value={this.state.DivisionState}
            onChange={this.handleChangeDivision}
            autoWidth={false}
            input={<Input id="name-multiple" />}
          >
            {this.renderDivisionOptions()}
          </Select>
      </FormControl>
    

    您可以在 Simple Selects demoSelect 组件上看到这一点。

    【讨论】:

      【解决方案3】:

      我不知道如何将代码放入 cmets...

      renderDivisionOptions() {
        <MenuItem
          key={1}
          value={this.state.placeholder}>
            'placeholdertext'
        </MenuItem>
      
        return this.state.DivisionData.map((dt, i) => {
          return (
            <MenuItem
              key={i}
              value={dt.divDeptShrtDesc}>
                {dt.divDeptShrtDesc}
            </MenuItem>
          );
        });
      }
      

      这行得通吗?它超级 hacky,但我不敢相信他们不再支持占位符文本了。

      【讨论】:

      • 感谢您的宝贵时间,但没有成功!!我也是,不知道为什么不包含稳定版中的这个属性...
      猜你喜欢
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      • 2022-01-19
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      相关资源
      最近更新 更多