gpt4 book ai didi

javascript - 当数据可用时,Material UI 组件不会自动完成

转载 作者:行者123 更新时间:2023-12-03 13:29:53 25 4
gpt4 key购买 nike

我无法让我的组件显示我的自动建议。在控制台中观察到我的数据可用,我使用 suggest 属性将其发送到此组件,使用 Material UI AutoComplete 组件功能 here我正在尝试设置我的选项,这些选项随着我的输入而变化,因为它是在父组件中处理的,但设置值似乎并没有反射(reflect)也没有提出我的建议。我很困扰。我的代码如下。

import React, { FunctionComponent, FormEvent, ChangeEvent } from "react";
import { Grid, TextField, Typography } from "@material-ui/core";
import { CreateProjectModel, JobModel } from "~/Models/Projects";
import ErrorModel from "~/Models/ErrorModel";
import Autocomplete from "@material-ui/lab/Autocomplete";

type CreateProjectFormProps = {
model: CreateProjectModel;
errors: ErrorModel<CreateProjectModel>;
onChange: (changes: Partial<CreateProjectModel>) => void;
onSubmit?: () => Promise<void>;
suggestions: JobModel[];
};

const CreateProjectForm: FunctionComponent<CreateProjectFormProps> = ({
model,
errors,
onChange,
onSubmit,
suggestions,
}) => {
const [open, setOpen] = React.useState(false);
const [options, setOptions] = React.useState<JobModel[]>([]);
const loading = open && options.length === 0;
const [inputValue, setInputValue] = React.useState('');
React.useEffect(() => {
let active = true;

if (!loading) {
return undefined;
}

(async () => {
if (active) {
setOptions(suggestions);
}
})();

return () => {
active = false;
};
}, [loading]);

React.useEffect(() => {
if (!open) {
setOptions([]);
}
}, [open]);

const submit = async (event: FormEvent) => {
event.preventDefault();
event.stopPropagation();

await onSubmit();
};

const change = (name: string) => (event: ChangeEvent<HTMLInputElement>) => {
setInputValue(event.target.value);

onChange({
[name]: event.target.value,
});
};

const getFieldProps = (id: string, label: string) => {
return {
id,
label,
helperText: errors[id],
error: Boolean(errors[id]),
value: model[id],
onChange: change(id),
};
};

return (
<Autocomplete
{...getFieldProps}
open={open}
onOpen={() => {
setOpen(true);
}}
onClose={() => {
setOpen(false);
}}
getOptionSelected={(option, value) => option.id === value.id}
getOptionLabel={(option) => option.id}
options={options}
loading={loading}
autoComplete
includeInputInList
renderInput={(params) => (
<TextField
{...getFieldProps("jobNumber", "Job number")}
required
fullWidth
autoFocus
margin="normal"
/>
)}
renderOption={(option) => {
return (
<Grid container alignItems="center">

<Grid item xs>
{options.map((part, index) => (
<span key={index}>
{part.id}
</span>
))}
<Typography variant="body2" color="textSecondary">
{option.name}
</Typography>
</Grid>
</Grid>
);
}}
/>
);
};

export default CreateProjectForm;

建议中我的数据示例如下所示:

[{"id":"BR00001","name":"Aircrew - Standby at home base"},{"id":"BR00695","name":"National Waste"},{"id":"BR00777B","name":"Airly Monitor Site 2018"},{"id":"BR00852A","name":"Cracow Mine"},{"id":"BR00972","name":"Toowoomba Updated"},{"id":"BR01023A","name":"TMRGT Mackay Bee Creek"},{"id":"BR01081","name":"Newman Pilot Job (WA)"},{"id":"BR01147","name":"Lake Vermont Monthly 2019"},{"id":"BR01158","name":"Callide Mine Monthly Survey 2019"},{"id":"BR01182","name":"Lake Vermont Quarterly 2019 April"}]

最佳答案

代码中的问题是您使用的 useEffects。

在下面的 useEffect 中,您实际上最初将选项设置为空数组。这是因为您的自动完成功能未打开,并且效果也在初始安装时运行。此外,由于您在另一个 useEffect 中设置选项,因此您的代码应该工作的唯一时间是在加载状态更新并且您尚未打开自动完成下拉列表时。

只要您关闭一次,状态就会更新回空,并且您将不会再看到建议。

React.useEffect(() => {
if (!open) {
setOptions([]);
}
}, [open]);

解决办法很简单。您不需要保留选项的本地状态,而是使用来自 props 的值,即 suggestions

您只需保持状态为打开即可

const CreateProjectForm: FunctionComponent<CreateProjectFormProps> = ({
model,
errors,
onChange,
onSubmit,
suggestions,
}) => {
const [open, setOpen] = React.useState(false);
const loading = open && suggestions.length === 0;
const [inputValue, setInputValue] = React.useState('');

const submit = async (event: FormEvent) => {
event.preventDefault();
event.stopPropagation();

await onSubmit();
};

const change = (name: string) => (event: ChangeEvent<HTMLInputElement>) => {
setInputValue(event.target.value);

onChange({
[name]: event.target.value,
});
};

const getFieldProps = (id: string, label: string) => {
return {
id,
label,
helperText: errors[id],
error: Boolean(errors[id]),
value: model[id],
onChange: change(id),
};
};

return (
<Autocomplete
{...getFieldProps}
open={open}
onOpen={() => {
setOpen(true);
}}
onClose={() => {
setOpen(false);
}}
getOptionSelected={(option, value) => option.id === value.id}
getOptionLabel={(option) => option.id}
options={suggestions}
loading={loading}
autoComplete
includeInputInList
renderInput={(params) => (
<TextField
{...getFieldProps("jobNumber", "Job number")}
required
fullWidth
autoFocus
margin="normal"
/>
)}
renderOption={(option) => {
return (
<Grid container alignItems="center">

<Grid item xs>
{options.map((part, index) => (
<span key={index}>
{part.id}
</span>
))}
<Typography variant="body2" color="textSecondary">
{option.name}
</Typography>
</Grid>
</Grid>
);
}}
/>
);
};

export default CreateProjectForm;

关于javascript - 当数据可用时,Material UI 组件不会自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61383771/

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