简体   繁体   English

MUI 5 自动完成选择属性 object 作为值

[英]MUI 5 Autocomplete choose attribute of object as value

My options looks like this我的选择看起来像这样

const options = [
  {
    "VaccinationType": "Sample"
  },
  {
    "VaccinationType": "Another Sample"
  }
]

Code:代码:

const [vacType, setVacType] = useState('');

<Autocomplete
    value={vacType}
    onChange={(e, value) => {
        console.log(value);
        setVacType(value);
    }}
    options={options}
    getOptionLabel={(option) => option.VaccinationType}
    isOptionEqualToValue={(option, value) => option.VaccinationType === value.VaccinationType}
    renderInput={(params) => (
            <TextField
                {...params}
                variant="outlined"
                size="small"
            />
    )}
/>

I tried logging my value it still outputs the object { VaccinationType: "Sample" } .我尝试记录我的值,它仍然输出 object { VaccinationType: "Sample" } I want it to only output "Sample"我只想要 output "Sample"

In MUI 4 I'm using this getOptionSelected={(option, value) => option?.VaccinationType === value?.VaccinationType}在 MUI 4 中,我使用这个getOptionSelected={(option, value) => option?.VaccinationType === value?.VaccinationType}

Autocomplete options care about the item of the array which you put.自动完成选项关心您放置的数组项目。 According to your code, each item has all object values.根据您的代码,每个项目都有所有对象值。 I searched to find a way but I couldn't.我试图找到一种方法,但我找不到。

There is a way probably you already thought;您可能已经想到了一种方法;

You can edit options parameter like options={options.map(o => o.VaccinationType)} and remove VaccinationType key on getOptionLabel and isOptionEqualToValue您可以编辑 options ={options.map(o => o.VaccinationType)}等选项参数并删除 getOptionLabel 和 isOptionEqualToValue 上的VaccinationType

<Autocomplete
    value={vacType}
    onChange={(e, value) => {
      console.log(value);
      setVacType(value);
  }}
    options={options.map(o => o.VaccinationType)}
    getOptionLabel={(option) => option}
    isOptionEqualToValue={(option, value) =>
      option === value
    }
    renderInput={(params) => (
      <TextField {...params} variant="outlined" size="small" />
    )}
  />

So I think u forgot to use onInputChange and InputValue properties for MUI Autocomplete, according to https://mui.com/material-ui/react-autocomplete/ these properties are needed in this case here u can read more https://mui.com/material-ui/api/autocomplete/ so please check this solution which works for me:所以我认为你忘记为 MUI 自动完成使用 onInputChange 和 InputValue 属性,根据https://mui.com/material-ui/react-autocomplete/在这种情况下需要这些属性在这里你可以阅读更多https://mui .com/material-ui/api/autocomplete/所以请检查这个对我有用的解决方案:

const [vacType, setVacType] = useState<any>('Sample')
const [inputValue, setInputValue] = useState('Sample')
const options: any[] = [
    {
        VaccinationType: 'Sample'
    },
    {
        VaccinationType: 'Another Sample'
    }
]

<Autocomplete
    value={vacType} //here u set default Value and change whenever it changes a list choice
    inputValue={inputValue} //here u set default Input and change whenever it changes by typing
    onChange={(event: any, newValue: any | null) => {
        setVacType(newValue)
    }}
    onInputChange={(event, newInputValue) => {
        setInputValue(newInputValue)
    }} // handles typing
    options={options}
    getOptionLabel={(option: { VaccinationType: string }) => {
        return option?.VaccinationType
    }}
    isOptionEqualToValue={(option, value) =>
        option?.VaccinationType === value?.VaccinationType
    }
    renderInput={params => (
        <TextField {...params} variant="outlined" size="small" />
    )}
/>

Hope it helps.希望能帮助到你。 Also to answer your problem with console.log还要用 console.log 回答你的问题

your code:你的代码:

onChange={(e, value) => {
    console.log(value);
    setVacType(value);
}}

exchanged to:交换到:

onChange={(event: any, newValue: any | null) => {
    console.log('newValue')
    console.log(newValue)
    setVacType(newValue)
}}

gives object but you on this Autocomplete provide objects then seting values is in objects so u just need to add console.log(newValue.VaccinationType) but not for setVacType(newValue) and Autocomplete will remember last choice给出 object 但你在此自动完成提供对象然后设置值在对象中所以你只需要添加 console.log(newValue.VaccinationType) 而不是 setVacType(newValue) 并且自动完成将记住最后的选择

I don't know why you want to do that!,我不知道你为什么要这样做!,
You can simply use value object wherever you want, and use any key of the object, take the value object as it's then use value.VaccinationType .您可以在任何需要的地方简单地使用value对象,并使用对象的任何键,然后使用value对象,然后使用value.VaccinationType

But, if you want to do this:但是,如果你想这样做:

<Autocomplete
    {...props}
    value={options.find(op => op.VaccinationType === vacType)}
    onChange={(e, value) => {
        console.log(value);                   //Result: { "VaccinationType": "Sample" }
        setVacType(value.VaccinationType);    // <==== Here, add `.VaccinationType`
    }}
/>

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

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