[英]Trigger on change on dynamic value set for react-admin SelectInput field
Can we trigger on change of SelectInput/TextField.我们可以在 SelectInput/TextField 更改时触发吗? For example if we set value for SelectInput field using source property like:
例如,如果我们使用源属性为 SelectInput 字段设置值,例如:
<SelectInput source='domain' onChange={() => {
//... fetch geo list
//... then set geo value
}}/>
then in the same form I want to update 2nd Select input field based on first select input value like:然后以相同的形式,我想根据第一个选择输入值更新第二个选择输入字段,例如:
<SelectInput source='geo' .../>
but if I update source value of 'domain' dynamically then it is not triggering on change.但是如果我动态更新“域”的源值,那么它不会在更改时触发。 Is there any way to make this possible.
有什么办法可以做到这一点。
You have to do it the other way around: it' not the domain that should notify the geo, it's the geo that should react to the domain change.你必须反过来做:不是应该通知地理的域,而是应该对域更改做出反应的地理。 That's the React way!
这就是反应方式!
The react-admin documentation (https://marmelab.com/react-admin/Inputs.html#linking-two-inputs ) explains that you need to use react-final-form's useFormState hook to get the current form state: react-admin 文档(https://marmelab.com/react-admin/Inputs.html#linking-two-inputs )解释说您需要使用 react-final-form 的 useFormState 钩子来获取当前表单状态:
import * as React from 'react';
import { Edit, SimpleForm, SelectInput } from 'react-admin';
import { useFormState } from 'react-final-form';
const countries = ['USA', 'UK', 'France'];
const cities = {
USA: ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'],
UK: ['London', 'Birmingham', 'Glasgow', 'Liverpool', 'Bristol'],
France: ['Paris', 'Marseille', 'Lyon', 'Toulouse', 'Nice'],
};
const toChoices = items => items.map(item => ({ id: item, name: item }));
const CityInput = props => {
const { values } = useFormState();
return (
<SelectInput
choices={values.country ? toChoices(cities[values.country]) : []}
{...props}
/>
);
};
const OrderEdit = props => (
<Edit {...props}>
<SimpleForm>
<SelectInput source="country" choices={toChoices(countries)} />
<CityInput source="cities" />
</SimpleForm>
</Edit>
);
export default OrderEdit;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.