reactjs - ant design 3.0 版表单上的 Typescript 错误

标签 reactjs typescript antd

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

这是错误: enter image description here

编辑 当我尝试使用这种形式时: enter image description here

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);

我在这里收到 typescript 错误: Form.create()(NormalLoginForm)

最佳答案

Form.create 是一个静态函数,它尝试将您的 NormalLoginForm 组件属性转换为 FormComponentProps。所以要实现它,你可以创建你的 Prop 接口(interface) 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);

关于reactjs - ant design 3.0 版表单上的 Typescript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47679801/

相关文章:

javascript - 更新 Redux 状态然后在同一实例中获取更新后的状态

javascript - 为什么按钮不打开按下按钮的模式?

javascript - 在 Angular 2 中提交发布请求时的奇怪行为

javascript - 日历 Ant 设计 : how to show event only in specific day in month using React JS

antd - Ant 设计。如何动态设置表单域错误消息?

css - 如何在 Tab 组件中更改 Icon 组件的位置?

javascript - 在自定义 View 中调用 super() 时,确保传递与传递组件的构造函数相同的 Prop

reactjs - 资源音频文件无法在 Expo 中播放

typescript - 可以使用不同的约束子类型实例化函数

reactjs - 没有子元素的 JSX 元素必须是自关闭的