【问题标题】:How to override .MuiSelect-nativeInput in Material-UI如何在 Material-UI 中覆盖 .MuiSelect-nativeInput
【发布时间】:2021-10-15 21:02:23
【问题描述】:
const styles = makeStyles((theme) => ({
  root: { margin: "0px 20px" },
  textStyle: {
    fontFamily: "Comfortaa",
  },
  container: {},
  textField: {
    fontFamily: "Comfortaa",
  },
  dropDownFormSize: {
    width: "100%",
    fontFamily: "Comfortaa",
  },
  optionDropdown: {
    color: "black",
  },
  dropDownSelector: {
    color: "black",
    backgroundColor: "tomato",
  },
  nativeInput: {
    opacity: "1",
  },
}));

const MainTable: React.FC = () => {
  const classes = styles();
  <FormControl
    classes={{
      root: classes.dropDownFormSize,
    }}
  >
    <Select
      required
      className={classes.dropDownSelector}
      value={emotion[i]}
      name="emotion"
      onChange={handleChangeEmotion(i)}
      classes={{
        root: classes.optionDropdown,
        select: classes.optionDropdown,
        // using nativeInput here gives me error
        nativeInput: classes.nativeInput,
      }}
      MenuProps={{
        anchorOrigin: {
          vertical: "bottom",
          horizontal: "left",
        },
        getContentAnchorEl: null,
        MenuListProps: {
          className: classes.optionDropdown,
        },
      }}
      placeholder="Select Something"
      native={false}
    >
      <MenuItem
        value=""
        disabled
        // className={
        //     classes.optionItems
        // }
      >
        Select Emotion
      </MenuItem>
      {emotions.map((emotion, i) => {
        return (
          <MenuItem
            key={i}
            // className={
            //     classes.optionItems
            // }
            value={emotion}
          >
            {emotion}
          </MenuItem>
        );
      })}
    </Select>
  </FormControl>;
};

我想从 .MuiSelect-nativeInput 类中删除不透明度。当我尝试使用 nativeInput 规则覆盖此类时,我收到此错误消息:- Object literal may only specify known properties, and 'nativeInput' does not exist in type 'Partial&lt;ClassNameMap&lt;SelectClassKey&gt;&gt;'. 尽管nativeInput 规则在Select API 的文档中给出。我试图在主题文件中覆盖它,但我再次收到 nativeInput 不存在的错误。如何从 MuiSelect-nativeInput 类中删除不透明度。

【问题讨论】:

    标签: css reactjs material-ui css-selectors jsx


    【解决方案1】:

    您可以改为使用呈现为选择输入的TextField

    const useStyles = makeStyles({
      root: {
        "& .MuiSelect-nativeInput": {
          opacity: 1,
        },
      },
    });
    
    <TextField 
      select
      classes = {{ root: classes.root }}
    />
    

    【讨论】:

      猜你喜欢
      • 2020-08-21
      • 1970-01-01
      • 2021-01-17
      • 2019-01-14
      • 2021-03-21
      • 1970-01-01
      • 2018-11-22
      • 2020-01-30
      • 2019-03-07
      相关资源
      最近更新 更多