英文:
MUI Autocomplete Customization. How to render and set as value one property, but search by another?
问题
export const countries = [
{ code: '', label: '', phone: '' },
{ code: 'AD', label: 'Andorra', phone: '376' },
{ code: 'AE', label: 'United Arab Emirates', phone: '971' },
{ code: 'AF', label: 'Afghanistan', phone: '93' }
]
<Controller
name="userDTO.phoneCode"
control={control}
render={({ field, fieldState: { error } }) => (
<Autocomplete
{...field}
options={countries.map((country) => country.label)}
getOptionLabel={(option) => option}
onChange={(event, newValue) => setValue("userDTO.phoneCode", newValue, { shouldValidate: true })}
renderInput={(params) => {
const selectedCountry = countries.find((country) => country.label === params.inputProps.value);
const { code, label, phone } = selectedCountry || {
code: "",
label: "",
phone: "",
};
return (
<TextField
{...params}
InputProps={{
...params.InputProps,
startAdornment: (
<InputAdornment position="start">
<Iconify
key={label}
icon={`circle-flags:${code.toLowerCase()}`}
width={28}
sx={{ mr: 1 }}
/>
</InputAdornment>
)
}}
label="Phone Code"
/>
);
}}
renderOption={(props, option) => {
const selectedCountry = countries.find((country) => country.label === option);
if (!selectedCountry) {
return null;
}
return (
<li {...props} key={selectedCountry.label}>
<Iconify
key={selectedCountry.label}
icon={`circle-flags:${selectedCountry.code.toLowerCase()}`}
width={28}
sx={{ mr: 1 }}
/>
{selectedCountry.label} ({selectedCountry.code}) +{selectedCountry.phone}
</li>
);
}}
/>
)}
/>
英文:
export const countries = [
{ code: '', label: '', phone: '' },
{ code: 'AD', label: 'Andorra', phone: '376' },
{
code: 'AE',
label: 'United Arab Emirates',
phone: '971',
},
{ code: 'AF', label: 'Afghanistan', phone: '93' }
]
<Controller
name="userDTO.phoneCode"
control={control}
render={({ field, fieldState: { error } }) => (
<Autocomplete
{...field}
options={countries.map((country) => country.label)}
getOptionLabel={(option) => option}
onChange={(event, newValue) => setValue("userDTO.phoneCode", newValue, { shouldValidate: true })}
renderInput={(params) => {
const selectedCountry = countries.find( (country) => country.label === params.inputProps.value );
const { code, label, phone } = selectedCountry || {
code: "",
label: "",
phone: "",
};
return (
<TextField
{...params}
InputProps={{
...params.InputProps,
startAdornment: (
<InputAdornment position="start">
<Iconify
key={label}
icon={`circle-flags:${code.toLowerCase()}`}
width={28}
sx={{mr: 1}}
/>
</InputAdornment>)
}}
label="Phone Code"
/>);
}}
renderOption={(props, option) => {
const {code, label, phone} = countries.filter( (country) => country.label === option )[0];
if (!label) {
return null;
}
return (
<li {...props} key={label}>
<Iconify
key={label}
icon={`circle-flags:${code.toLowerCase()}`}
width={28}
sx={{mr: 1}}
/>
{label} ({code}) +{phone}
</li>
);
}}
/>
)}
/>
This code will render Autocomplete with search by country.label, country.label as selected value, and country.label will be in submitted data. How to customize this code to have search by country.label, render country.phone and set country.phone as value?
I've read a documentation, but did not found solution.
答案1
得分: 0
你可以使用Autocomplete API的filterOptions属性。使用它,你可以自定义搜索,像这样的方式可能会起作用:
filterOptions={(options,state) => {
return countries.filter(country =>
country.label.includes(state.inputValue)).map(country => country.phone);
}}
在options中,你会收到自动完成选项的数组,在state中,你有用户输入。
英文:
You can use the filterOptions property from the Autocomplete API. With it you can customize the search, in this case something like this I think would work:
filterOptions={(options,state) => {
return countries.filter(country =>
country.label.includes(state.inputValue)).map(country => country.phone);
}}
In options you receive the autocomplete options array and in state you have the user input.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论