CreateCounterpartyComponent:
const CreateCounterpartyComponent: React.FC<FormComponentProps> = props => {
const GET_ALL = gql`
query {
types {
value: id
name: full_name
}
}
`;
const { getFieldDecorator } = props.form;
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
console.log(e);
};
const changeHandler = (e:number) => {
console.log(e)
}
const { loading, data } = useQuery(GET_ALL);
if (loading) return <Loader />;
return (
<div>
<Form onSubmit={handleSubmit}>
<Form.Item hasFeedback>
{getFieldDecorator('address', {
rules: [{ required: true, message: 'Address?' }],
})(<CustomSeletor items={data.types} width="20%" changeEvent={(e)=>changeHandler(e)}/>)}
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit" className="login-form-button">
Send
</Button>
</Form.Item>
</Form>
</div>
);
};
export const CreateCounterparty = Form.create()(CreateCounterpartyComponent);
CustomSeletor:
const { Option } = Select;
interface IProps {
items: ISelectorProps[];
width: string;
changeEvent(e:number):void;
}
export const CustomSeletor: React.FC<IProps> = ({ items, width, changeEvent }) => {
const mystyles = {
width: width,
};
const handleChange = (e:number) => {
changeEvent(e)
}
const options = items.map(({ value, name }) => <Option key={value} value={value}>{name}</Option>);
return (
<Select style={mystyles} onChange={handleChange}>
{options}
</Select>
);
};
答案 0 :(得分:1)
您正在使用antd。根据{{3}}的规定:
自定义或第三方表单控件也可以在Form中使用。控件必须遵循以下约定:
antd要求CustomSelector
能够接收引用。必须将功能组件包装在React.forwardRef
中才能接收引用,否则您将看到错误。
解决方案是将CustomSelector
包装在forwardRef
中:
export const CustomSelector = React.forwardRef(({ items, width, changeEvent }, ref) => {
const mystyles = {
width: width,
};
const handleChange = (e:number) => {
changeEvent(e)
}
const options = items.map(({ value, name }) => <Option key={value} value={value}>{name}</Option>);
return (
<Select ref={ref} style={mystyles} onChange={handleChange}>
{options}
</Select>
);
});
您应确保Select
符合antd的要求。检查他们的文档,因为我不认识他们。