如何选择自动完成材料ui中已经选择的值?

时间:2020-04-27 08:34:49

标签: material-ui

我使用的是Material UI自动完成功能,因此遇到了这个问题。在文档here中说:“该值必须与选项具有引用相等性才能被选择”。我完全按照它说的去做,但似乎不起作用。

  • 这是我的选择 enter image description here
  • 这是我的价值 enter image description here
  • 当前 enter image description here
  • 预期 enter image description here

    <Autocomplete
     disableCloseOnSelect={true}
     multiple
     options={techList}
     getOptionLabel={(options) => options.techName}
     value={newValueDialog}
     onChange={(e, tech) => {
      this.handleChangeValueDialog(tech);
     }}
     popupIcon={false}
     closeIcon={false}
     renderTags={(value, getTagProps) =>
     value.map((option, index) => (
      <Chip
        color="primary"
        variant="outlined"
        label={option.techName}
        {...getTagProps({ index })}
        deleteIcon={<ClearIcon />}
      />
     ))
     }
    

    renderInput = {((params)=>( )} />

1 个答案:

答案 0 :(得分:1)

在使用自定义对象时,请确保与自己的实现相同。

https://material-ui.com/api/autocomplete/#autocomplete-api

getOptionSelected

您必须实现“ getOptionSelected”方法

    getOptionSelected={(option, value) =>
      option.techName === value.techName
    }

https://codesandbox.io/s/material-demo-vh5ns

相关问题