首页 > 解决方案 > what's the props's type with Typescript when i customize ant-design form control

问题描述

my form component code here:

import * as React from 'react';
import { From } from 'antd';
import { FormComponentProps } from 'antd/es/form';
import CustomControl from './CustomControl'

interface IProps extends FormComponentProps {

}

class MyForm extends React.Component<IProps> {
  constructor(props: IProps) {
    super(props);
  }

  public render(): React.ReactNode {
    const { getFieldDecorator } = this.props.form

    return <Form>
      <Form.Item label="custom-control">
        {
          getFieldDecorator('custom-field', {initialValue: 'custome-value'})
          (<CustomControl />)
        }
      </Form.Item>
    </Form>
  }
}

export default Form.create<IProps>()(MyForm);

my customize form control here:

import * as React from 'react';
import { Input } from 'antd';

const { forwardRef, useState } = React;

function CustomControl(props: any) {
    const [value, setValue] = useState(props.value);
    const handleChange = (value: string) => {
        if (props.onChange) {
            setValue(value)
            props.onChange(value)
        }
    }

    return <div>
        <Input value={value} onChange=((e) => handleChange(e.currentTarget.value)) />
    </div>
}
export default forwardRef(CustomControl)

it's this any:

function CustomComponent(props: any) {}

i can't define this props's type who's passed by antd getFieldDecorator, the code in antd about getFieldDecorator:

getFieldDecorator<T extends Object = {}>(id: keyof T, options?: GetFieldDecoratorOptions): (node: React.ReactNode) => React.ReactNode;

React.ReactNode is obviously not for me.

So, i can only write any here, it's not friendly enough.

Please show me the way to optimize this any.

标签: reactjstypescriptantd

解决方案


推荐阅读