简体   繁体   English

选择芯片输入不显示所选值

[英]Select with chip input not displaying the selected value

I have a Select and the inputs are in Chip Format.我有一个选择,输入是芯片格式。 I tried console log of the value selected and it is getting it fine.我尝试了所选值的控制台日志,结果很好。 But for some reason, it does not get displayed on the select box.但由于某种原因,它不会显示在选择框中。 What am I doing wrong here?我在这里做错了什么?

 handleChange = event => {
    this.setState({ badge : event.target.value });
  };

 const chipOptions = [
      {key: 1, 'text': 'text1',  'value': 'text1'},
      {key: 2, 'text':'text2', 'value':'text2'},
      {key: 3, 'text':'text3', 'value':'text3'}
    ]

<FormControl className={classes.formControl}>
            <Select
              value={this.state.badge}
              onChange={this.handleChange}
              inputProps={{
                name: 'badge',
                id: 'badge-simple',
              }}
            >
            {chipOptions && chipOptions.map(option=> (
              <Chip key={option.value} label={option.value} className={classes.chip} value={option.value} />
            ))}

          </Select>
        </FormControl>

The default manner in which Select renders the selected value is to render its children. Select呈现选定值的默认方式是呈现其子项。 In the Select source code as it is looping through the children of the Select, it does the following:Select 源代码中,当它循环通过 Select 的子项时,它执行以下操作:

        selected = areEqualValues(value, child.props.value);
        if (selected && computeDisplay) {
          displaySingle = child.props.children;
        }

This is based on the assumption of the Select having MenuItem children.这是基于Select具有MenuItem子项的假设。 For instance, in the following example the first MenuItem would be selected and that MenuItem's children would be the text "Item 1":例如,在以下示例中,第一个 MenuItem 将被选中,该 MenuItem 的子项将是文本“Item 1”:

<Select value={1}>
   <MenuItem value={1}>Item 1</MenuItem>
   <MenuItem value={2}>Item 2</MenuItem>
</Select>

Your Chips don't have children, so nothing is displayed.您的 Chips 没有孩子,因此不会显示任何内容。 You can customize this behavior by specifying the renderValue property on Select .您可以通过在Select上指定renderValue属性来自定义此行为。 This is a function that receives the value and can decide what to render.这是一个接收值并可以决定渲染什么的函数。

The following example shows using the renderValue prop to render a Chip :以下示例显示使用renderValue道具渲染Chip

import React, { useState } from "react";
import ReactDOM from "react-dom";

import FormControl from "@material-ui/core/FormControl";
import Chip from "@material-ui/core/Chip";
import Select from "@material-ui/core/Select";
import { withStyles } from "@material-ui/core/styles";

const styles = theme => ({
  formControl: {
    margin: theme.spacing.unit,
    minWidth: 120
  }
});
const chipOptions = [
  { key: 1, text: "text1", value: "text1" },
  { key: 2, text: "text2", value: "text2" },
  { key: 3, text: "text3", value: "text3" }
];

function App({ classes }) {
  const [value, setValue] = useState("text1");
  const renderChip = value => {
    return <Chip label={value} className={classes.chip} />;
  };
  return (
    <>
      <FormControl className={classes.formControl}>
        <Select
          inputProps={{
            name: "badge",
            id: "badge-simple"
          }}
          renderValue={renderChip}
          value={value}
          onChange={event => {
            console.log(event.target.value);
            setValue(event.target.value);
          }}
        >
          {chipOptions &&
            chipOptions.map(option => (
              <Chip
                key={option.value}
                label={option.value}
                className={classes.chip}
                value={option.value}
              />
            ))}
        </Select>
      </FormControl>
    </>
  );
}
const StyledApp = withStyles(styles)(App);
const rootElement = document.getElementById("root");
ReactDOM.render(<StyledApp />, rootElement);

编辑片选

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM