【问题标题】:How to disable some options in "Select" component of Material-UI like in "Autocomplete"?如何在“自动完成”中禁用 Material-UI 的“选择”组件中的某些选项?
【发布时间】:2020-10-16 03:19:13
【问题描述】:

有什么方法可以禁用Select 组件中的某些选项,例如Autocomplete

PS:选项在数组中

 <FormControl variant="outlined">
        <InputLabel>States</InputLabel>
        <Select native
          defaultValue=""
          // value={value}
          onChange={inputEvent}
          label="States"
        >
          {fetchedStates.map((states, i) => (
            <option key={states + i} value={states}>
              {states}
            </option>
          ))}
        </Select>
      </FormControl>

【问题讨论】:

  • 您已经打开了文档,您只需要查看实现即可。答案已经在您提供的图片中。您必须添加 getOptionDisabled。
  • 我的选项在一个数组中,所以没有给定的方法来禁用select中的特定选项
  • 您使用的是native Select 吗?您没有在示例中显示 native 属性,但 &lt;option&gt; 标记仅作为 native Select 的子级有效(对于非本地,子级应该是 MenuItem 元素)。
  • 太棒了,你能再检查一下吗

标签: css reactjs material-ui react-material


【解决方案1】:

Select 执行此操作的方法是将disabled 属性添加到MenuItem(在下面的示例中显示为“二十”MenuItem)。

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import InputLabel from "@material-ui/core/InputLabel";
import MenuItem from "@material-ui/core/MenuItem";
import FormControl from "@material-ui/core/FormControl";
import Select from "@material-ui/core/Select";

const useStyles = makeStyles(theme => ({
  formControl: {
    margin: theme.spacing(1),
    minWidth: 120
  },
  selectEmpty: {
    marginTop: theme.spacing(2)
  }
}));

export default function SimpleSelect() {
  const classes = useStyles();
  const [age, setAge] = React.useState("");

  const handleChange = event => {
    setAge(event.target.value);
  };

  return (
    <div>
      <FormControl variant="outlined" className={classes.formControl}>
        <InputLabel id="demo-simple-select-outlined-label">Age</InputLabel>
        <Select
          labelId="demo-simple-select-outlined-label"
          id="demo-simple-select-outlined"
          value={age}
          onChange={handleChange}
          label="Age"
        >
          <MenuItem value="">
            <em>None</em>
          </MenuItem>
          <MenuItem value={10}>Ten</MenuItem>
          <MenuItem disabled value={20}>
            Twenty
          </MenuItem>
          <MenuItem value={30}>Thirty</MenuItem>
        </Select>
      </FormControl>
    </div>
  );
}

对于原生 Select,您可以使用 disabled 属性来代替 &lt;option&gt;

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import InputLabel from "@material-ui/core/InputLabel";
import FormControl from "@material-ui/core/FormControl";
import Select from "@material-ui/core/Select";

const useStyles = makeStyles(theme => ({
  formControl: {
    margin: theme.spacing(1),
    minWidth: 120
  },
  selectEmpty: {
    marginTop: theme.spacing(2)
  }
}));

export default function SimpleSelect() {
  const classes = useStyles();
  const [age, setAge] = React.useState("");

  const handleChange = event => {
    setAge(event.target.value);
  };

  return (
    <div>
      <FormControl variant="outlined" className={classes.formControl}>
        <InputLabel id="demo-simple-select-outlined-label">Age</InputLabel>
        <Select
          labelId="demo-simple-select-outlined-label"
          id="demo-simple-select-outlined"
          value={age}
          onChange={handleChange}
          label="Age"
          native
        >
          <option aria-label="None" value="" />
          <option value={10}>Ten</option>
          <option disabled value={20}>
            Twenty
          </option>
          <option value={30}>Thirty</option>
        </Select>
      </FormControl>
    </div>
  );
}

如果选项在一个数组中,您只需要确定哪些选项应该被禁用。下面的示例显示了执行此操作的一种方法,其中选项数据包含是否应禁用该选项。

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import InputLabel from "@material-ui/core/InputLabel";
import FormControl from "@material-ui/core/FormControl";
import Select from "@material-ui/core/Select";

const useStyles = makeStyles(theme => ({
  formControl: {
    margin: theme.spacing(1),
    minWidth: 120
  },
  selectEmpty: {
    marginTop: theme.spacing(2)
  }
}));

const options = [
  { value: 10, label: "Ten" },
  { value: 20, label: "Twenty", disabled: true },
  { value: 30, label: "Thirty" }
];

export default function SimpleSelect() {
  const classes = useStyles();
  const [age, setAge] = React.useState("");

  const handleChange = event => {
    setAge(event.target.value);
  };

  return (
    <div>
      <FormControl variant="outlined" className={classes.formControl}>
        <InputLabel id="demo-simple-select-outlined-label">Age</InputLabel>
        <Select
          labelId="demo-simple-select-outlined-label"
          id="demo-simple-select-outlined"
          value={age}
          onChange={handleChange}
          label="Age"
          native
        >
          <option aria-label="None" value="" />
          {options.map(option => (
            <option value={option.value} disabled={option.disabled}>
              {option.label}
            </option>
          ))}
        </Select>
      </FormControl>
    </div>
  );
}

【讨论】:

  • 对不起,我没有添加,但选项是一个数组?你能再检查一下上面的问题吗
  • @KALITA 我添加了更多示例。
  • @RyanCogswell 请注意,在 MenuItem 中添加 disabled 不会将 disabled 属性添加到 DOM 元素,这会在您测试元素的禁用状态时导致测试失败 (.toBeDisabled() )。
  • @NearHuscarl 在MenuItem 上,它将aria-disabled="true" 添加到DOM 以及Mui-disabled CSS 类。 DOM 元素是 &lt;li&gt;,而 disabled 不是 &lt;li&gt; 元素支持的属性。
猜你喜欢
  • 2020-04-16
  • 2021-12-20
  • 2020-03-04
  • 2020-09-23
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 2012-08-28
  • 2021-07-12
相关资源
最近更新 更多