我将onChange
、onFocus
和onBlur
从antd
传递到Form.Item
。
<Form.Item
name={[id]}
rules={[{ type, required, ...rules }]}
onChange={onChange}
onFocus={() => setIsFocused(true)}
onBlur={e => setIsFocused(!!e.target.value)}>
{children}
</Form.Item>
但当运行next build
时,此错误:
Type error: Type '{ children: ReactNode; name: string[]; rules: any[]; onChange: (event: FormEvent<HTMLInputElement>) => void; onFocus: () => void; onBlur: (e: any) => void; }' is not assignable to type 'IntrinsicAttributes & FormItemProps'.
Property 'onChange' does not exist on type 'IntrinsicAttributes & FormItemProps'.
13 | name={[id]}
14 | rules={[{ type, required, ...rules }]}
> 15 | onChange={onChange}
| ^
16 | onFocus={() => setIsFocused(true)}
17 | onBlur={e => setIsFocused(!!e.target.value)}>
18 | {children}
它适用于以上引用的所有3种方法。我该如何解决此问题?
这不起作用,因为onChange、onFocus和onBlur不是Form.Item的道具。Form在内部管理表单的状态,使用方式如下:https://codesandbox.io/s/9bpnu?file=/index.js.如果您希望自己管理表单的状态,我建议您使用Ant Design的Input组件。