gpt4 book ai didi

reactjs - Material UI Autocomplete 上的 Typescript Equality 问题

转载 作者:行者123 更新时间:2023-12-03 15:21:54 25 4
gpt4 key购买 nike

数据存储为:

 { iso: "gb", label: "United Kingdom", country: "United Kingdom" },
{ iso: "fr", label: "France", country: "France" }

传递给自动完成的值是:
{ iso: "gb", label: "United Kingdom", country: "United Kingdom" }

控制台报错

Material-UI: the value provided to Autocomplete is invalid. None of the options match with {"label":"United Kingdom","iso":"gb","country":"United Kingdom"}.


value={} 上报告的类型错误

Type 'string | ICountry' is not assignable to type 'ICountry | ICountry[] | null | undefined'. Type 'string' is not assignable to type 'ICountry | ICountry[] | null | undefined'.



问题:将数据传递给组件并没有将其设置为相应的选项,我对如何解决此问题一无所知。

问题代码沙箱:
https://codesandbox.io/s/charming-firefly-zl3qd?file=/src/App.tsx
import * as React from "react";
import { Box, Typography, TextField, Button } from "@material-ui/core";
import { Autocomplete } from "@material-ui/lab";
import "./styles.css";
import { countries } from "./countries";
import { investors } from "./investor";
import { Formik } from "formik";

interface ICountry {
iso: string;
country: string;
label: string;
}

const isoToFlag = (isoCode: string) => {
if (isoCode) {
return typeof String.fromCodePoint !== "undefined"
? isoCode
.toUpperCase()
.replace(/./g, char =>
String.fromCodePoint(char.charCodeAt(0) + 127397)
)
: isoCode;
}
return "";
};

const App: React.FC = () => {
const investor = investors.find(element => element.id === "123");


return (
<div className="App">
<Formik
initialValues={{
address: {
country: investor?.legal.address.country ? investor.legal.address.country : '',
}
}}
onSubmit={(values, actions) => {
console.log({ values, actions });
actions.setSubmitting(false);
}}
>
{({ submitForm, isSubmitting, values, setFieldValue, setValues }) => {

return (
<form>

<Autocomplete
id="country"
options={countries}
getOptionLabel={option => option.label}
value={values.address.country}
renderOption={(option: ICountry) => (
<Box display="flex" flexDirection="row" alignItems="center">
<Box mr={1}>{isoToFlag(option.iso)}</Box>
<Box>
<Typography variant="body2">{option.label}</Typography>
</Box>
</Box>
)}
onChange={(e: object, value: any | null) => {
console.log('do the types match?', typeof value === typeof values.address.country);
console.log('do the objects match?', value === values.address.country);
console.log('the objects in question', value, values.address.country);
console.log(" ");
setFieldValue("address.country", value);
}}
renderInput={params => (
<TextField
{...params}
name="address.country"
label="Country"
variant="outlined"
fullWidth
/>
)}
/>
<Button
variant="contained"
size="large"
color="primary"
disabled={isSubmitting}
onClick={submitForm}
>
Submit
</Button>
</form>
);
}}
</Formik>
</div>
);
};

export default App;

国家.ts

import { ICountry } from "./investor";

export const countries: ICountry[] = [
{
iso: "gb",
label: "United Kingdom",
country: "United Kingdom"
},
{
iso: "fr",
label: "France",
country: "France"
}
];

最佳答案

来自 Material-UI 的完整消息是:

Material-UI: the value provided to Autocomplete is invalid. None of the options match with {"label":"United Kingdom","iso":"gb","country":"United Kingdom"}. You can use the getOptionSelected prop to customize the equality test.



默认相等性测试只是 === ,因此如果您的选项是对象,则您的值必须是这些确切对象之一才能匹配。具有相同值的不同对象将不匹配。

但是正如消息告诉您的那样,您可以通过 getOptionSelected Prop 自定义相等性测试。例如,您可以指定:
getOptionSelected={(option, value) => option.iso === value.iso}

或者您可以对所有对象属性进行深度相等性检查。

类型错误可以通过 value={values.address.country as ICountry} 修复。

这是您的沙箱的工作版本: https://codesandbox.io/s/autocomplete-getoptionselected-b6n3s

关于reactjs - Material UI Autocomplete 上的 Typescript Equality 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61504777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com