我有一个创建表单,其中根据另一个 SelectInput 的选择使另一个 SelectInput 可见。
第一次发生这种情况时,第一个输入被清除。如何阻止第一个选择清除?
这是重新创建的代码:
import {Create, SelectInput, TextInput, SimpleForm} from 'react-admin';
import React from 'react';
class Recreate extends React.Component {
constructor(props) {
super(props);
this.props = props;
this.state = {visible: false}
}
render() {
return (
<Create {...this.props}>
<SimpleForm>
<SelectInput source="device" choices={[{id: 'OR', name: 'Oregon'}, {id: 'WA', name: 'Washington'}]}
onChange={(event, key, payload) => this.setState({visible: key === 'WA'})}
/>
{this.state.visible && (
<SelectInput label={'I am visible from WA'}
source="renderer"
choices={[
{id: 'Seattle', name: 'Seattle'},
{id: 'Olympia', name: 'Olympia'},
]}
defaultValue={'gs'}/>
)}
</SimpleForm>
</Create>
);
}
}
导出默认重新创建;
更新:根据答案尝试修复:
import {Create, SelectInput, SimpleForm, TextInput} from 'react-admin';
import React from 'react';
const CustomSelectInput = ({onChangeCustomHandler, ...rest}) => (
<SelectInput onChange={(event, key, payload) => {
onChangeCustomHandler(key)
}}
{...rest}
/>
);
class Recreate extends React.Component {
constructor(props) {
super(props);
this.props = props;
this.state = {visible: false}
}
render() {
return (
<Create {...this.props}>
<SimpleForm>
<CustomSelectInput source="device"
choices={[{id: 'OR', name: 'Oregon'}, {id: 'WA', name: 'Washington'}]}
onChangeCustomHandler={(key) => this.setState({visible: key === 'WA'})}/>
{this.state.visible && (
<SelectInput label={'I am visible from WA'}
source="renderer"
choices={[
{id: 'Seattle', name: 'Seattle'},
{id: 'Olympia', name: 'Olympia'},
]}
/>
)}
</SimpleForm>
</Create>
);
}
}
export default Recreate;