gpt4 book ai didi

reactjs - React-Admin:如何发送从 API 调用中自动填充的输入值?

转载 作者:行者123 更新时间:2023-12-04 10:04:45 24 4
gpt4 key购买 nike

我有一个输入“A”,它从 API 获取地址数据,并根据该数据自动填充输入“B”、“C”和“D”,但在输入完成后,我尝试将该表单发送到我的后端,这些自动填充的输入都不会发送,只发送输入“A”。此外,如果我手动编辑任何输入(删除字符、添加空格、更改值),我编辑的输入将发送到我的后端。

我正在使用 reducer 来存储状态。我使用的输入都是普通的 react-admin TextInput 组件。

这是代码:

const AutoFill = () => {
const [searching, setSearching] = useState(false);
const [error, setError] = useState(false);
const [stateData, setStateData] = useReducer(
(state, newState) => ({ ...state, ...newState }),
{
cep: ' - ',
address: '',
number: '',
neighborhood: '',
city: '',
state: '',
}
);

const FormControl = (event) => {
const { name, value } = event.target;
setStateData({ [name]: value });
};

const SearchControl = (event) => {
const { name, value } = event.target;
setStateData({ [name]: value });
if (value && !value.includes('_')) {
setSearching(true);
setStateData({ state: '...' });
setStateData({ city: '...' });
setStateData({ neighborhood: '...' });
setStateData({ address: '...' });
cep(value.replace('-', '')).then(
(result) => {
setSearching(false);
setError(false);
setStateData({ state: result.state });
setStateData({ city: result.city });
setStateData({ neighborhood: result.neighborhood });
setStateData({ address: result.street });
},
() => {
setSearching(false);
setError(true);
setStateData({ state: '' });
setStateData({ city: '' });
setStateData({ neighborhood: '' });
setStateData({ address: '' });
}
);
}
};

return (
<>
<TextInput
source="cep"
error={error}
value={stateData.cep}
onChange={SearchControl}
/>
<TextInput
source="address"
disabled={searching}
value={stateData.address}
onChange={FormControl}
/>
<TextInput
source="number"
disabled={searching}
value={stateData.number}
onChange={FormControl}
/>
<TextInput
source="neighborhood"
disabled={searching}
value={stateData.neighborhood}
onChange={FormControl}
/>
<TextInput
source="state"
disabled={searching}
value={stateData.state}
onChange={FormControl}
/>
<TextInput
source="city"
disabled={searching}
value={stateData.city}
onChange={FormControl}
/>
</>
);
};

export const Create = (props) => {
return (
<Create {...props}>
<SimpleForm>
<NumberInput label="Value" source="price" />
<AutoFill />
<RichTextInput label="Description" source="description" />
</SimpleForm>
</Create>
);
};

最佳答案

您将需要使用 React Final Form 的 FormState 和 Form 解决方案。例如,将使用我的代码片段。

1) 获取表单值

    const formState = useFormState();
const form = useForm();

const {
asset_system_parent_id: majorSystem,
classification,
} = formState.values;

2) 设置 useEffect 将观察表单字段的变化:
    useEffect(() => {
const setFluidEnd = async () => {
DO SOMETHING!!!!!
};

if ('Fluid End Maintenance' === classification) {
setFluidEnd();
}
}, [classification, form, notify]);

3) 使用 form.change (+ form.batch 如果需要更新多个输入)
    useEffect(() => {
const setFluidEnd = async () => {
await requestGetList('asset-systems', 'id', 'ASC', 500, {
description: 'Fluid End',
relationship: 'parent',
})
.then(res => {
form.change('asset_system_parent_id', res.data[0].id);
})
.catch(error => {
notify(`System Assets not found`, 'warning');
});
};

if ('Fluid End Maintenance' === classification) {
setFluidEnd();
}
}, [classification, form, notify]);

您可以在此处阅读有关 api 的更多信息: https://final-form.org/docs/final-form/types/FormApi

关于reactjs - React-Admin:如何发送从 API 调用中自动填充的输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61644728/

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