【问题标题】:Typescript error on ant design version 3.0 formant design 3.0 版表单上的打字稿错误
【发布时间】:2018-05-20 15:49:53
【问题描述】:

我在尝试来自 https://ant.design/components/form/Form.create 时遇到打字错误

这是错误:

编辑 当我尝试使用这种形式时:

import { Form, Icon, Input, Button, Checkbox } from 'antd';
const FormItem = Form.Item;

class NormalLoginForm extends React.Component {
  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      if (!err) {
        console.log('Received values of form: ', values);
      }
    });
  }
  render() {
    const { getFieldDecorator } = this.props.form;
    return (
      <Form onSubmit={this.handleSubmit} className="login-form">
        <FormItem>
          {getFieldDecorator('userName', {
            rules: [{ required: true, message: 'Please input your username!' }],
          })(
            <Input prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />} placeholder="Username" />
          )}
        </FormItem>
        <FormItem>
          {getFieldDecorator('password', {
            rules: [{ required: true, message: 'Please input your Password!' }],
          })(
            <Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} type="password" placeholder="Password" />
          )}
        </FormItem>
        <FormItem>
          {getFieldDecorator('remember', {
            valuePropName: 'checked',
            initialValue: true,
          })(
            <Checkbox>Remember me</Checkbox>
          )}
          <a className="login-form-forgot" href="">Forgot password</a>
          <Button type="primary" htmlType="submit" className="login-form-button">
            Log in
          </Button>
          Or <a href="">register now!</a>
        </FormItem>
      </Form>
    );
  }
}

const WrappedNormalLoginForm = Form.create()(NormalLoginForm);

ReactDOM.render(<WrappedNormalLoginForm />, mountNode);

我在这里收到打字稿错误: Form.create()(NormalLoginForm)

【问题讨论】:

    标签: reactjs typescript antd


    【解决方案1】:

    Form.create 是一个静态函数,它尝试将您的 NormalLoginForm 组件道具转换为 FormComponentProps。所以要实现它,你可以创建你的 prop 接口 NormalLoginProps 并直接从 'antd/lib/form/Form' 导入 FormComponentProps

    import { Form, Icon, Input, Button, Checkbox } from 'antd';
    import {FormComponentProps} from 'antd/lib/form/Form';
    const FormItem = Form.Item;
    interface NormalLoginProps{}
    
    class NormalLoginForm extends React.Component<NormalLoginProps & FormComponentProps> {
      handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
          if (!err) {
            console.log('Received values of form: ', values);
          }
        });
      }
      render() {
        const { getFieldDecorator } = this.props.form;
        return (
          <Form onSubmit={this.handleSubmit} className="login-form">
            <FormItem>
              {getFieldDecorator('userName', {
                rules: [{ required: true, message: 'Please input your username!' }],
              })(
                <Input prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />} placeholder="Username" />
              )}
            </FormItem>
            <FormItem>
              {getFieldDecorator('password', {
                rules: [{ required: true, message: 'Please input your Password!' }],
              })(
                <Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} type="password" placeholder="Password" />
              )}
            </FormItem>
            <FormItem>
              {getFieldDecorator('remember', {
                valuePropName: 'checked',
                initialValue: true,
              })(
                <Checkbox>Remember me</Checkbox>
              )}
              <a className="login-form-forgot" href="">Forgot password</a>
              <Button type="primary" htmlType="submit" className="login-form-button">
                Log in
              </Button>
              Or <a href="">register now!</a>
            </FormItem>
          </Form>
        );
      }
    }
    
    const WrappedNormalLoginForm = Form.create<NormalLoginProps>()(NormalLoginForm);
    
    ReactDOM.render(<WrappedNormalLoginForm />, mountNode);
    

    【讨论】:

      猜你喜欢
      • 2020-11-30
      • 2021-01-03
      • 1970-01-01
      • 2021-04-29
      • 2016-09-16
      • 2012-09-27
      • 2015-03-23
      • 1970-01-01
      • 2021-09-19
      相关资源
      最近更新 更多