【问题标题】:React js select does not show text as valueReact js select不将文本显示为值
【发布时间】:2021-09-30 16:36:54
【问题描述】:

我正在使用来自“@mui/material/Chip”的芯片并从“@mui/material/Select”中选择;在我的反应 js 应用程序中。

import * as React from "react";
import { useTheme } from "@mui/material/styles";
import Box from "@mui/material/Box";
import OutlinedInput from "@mui/material/OutlinedInput";
import InputLabel from "@mui/material/InputLabel";
import MenuItem from "@mui/material/MenuItem";
import FormControl from "@mui/material/FormControl";
import Select from "@mui/material/Select";
import Chip from "@mui/material/Chip";

const ITEM_HEIGHT = 48;
const ITEM_PADDING_TOP = 8;
const MenuProps = {
  PaperProps: {
    style: {
      maxHeight: ITEM_HEIGHT * 4.5 + ITEM_PADDING_TOP,
      width: 250
    }
  }
};

const names = [
  {
    id: 1,
    name: "Carl"
  },
  {
    id: 2,
    name: "Bill"
  }
];

function getStyles(name, personName, theme) {
  return {
    fontWeight:
      personName.indexOf(name) === -1
        ? theme.typography.fontWeightRegular
        : theme.typography.fontWeightMedium
  };
}

export default function MultipleSelectChip() {
  const theme = useTheme();
  const [personName, setPersonName] = React.useState([1]);

  const handleChange = (event) => {
    const {
      target: { value }
    } = event;
    setPersonName(
      // On autofill we get a the stringified value.
      typeof value === "string" ? value.split(",") : value
    );
  };
  console.log(personName, "get a list of ids");
  return (
    <div>
      <FormControl sx={{ m: 1, width: 300 }}>
        <InputLabel id="demo-multiple-chip-label">Chip</InputLabel>
        <Select
          labelId="demo-multiple-chip-label"
          id="demo-multiple-chip"
          multiple
          value={personName}
          onChange={handleChange}
          input={<OutlinedInput id="select-multiple-chip" label="Chip" />}
          renderValue={(selected) => (
            <Box sx={{ display: "flex", flexWrap: "wrap", gap: 0.5 }}>
              {selected.map((value) => (
                <Chip key={value} label={value} />
              ))}
            </Box>
          )}
          MenuProps={MenuProps}
        >
          {names.map(({ id, name }) => (
            <MenuItem
              key={name}
              value={id}
              style={getStyles(name, personName, theme)}
            >
              {name}
            </MenuItem>
          ))}
        </Select>
      </FormControl>
    </div>
  );
}

我现在如果我添加一个 id 作为初始值,在下拉列表中它也显示为数字,但我需要从数组中设置用户的 name。另外,当我更改console.log(personName, "get a list of ids"); 中的输入数据时,我想查看一个ID 列表,就像现在一样。问题是下一个:现在下拉初始值为1,但它应该是Carl,因为他的id为1,当我选择Bill时,出现nr2,但我需要文本而不是数字.
谁能帮忙解决这个问题?
演示:https://codesandbox.io/s/multipleselectchip-material-demo-forked-lkhzb?file=/demo.js:1264-1309

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    尝试以这种方式渲染Chips:

    &lt;Chip key={value} label={names.find(element =&gt; element.id === value)?.name} /&gt;

    personName 将保留 ID 列表,但您将呈现相关名称。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 2022-01-23
    • 2019-05-12
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多