【问题标题】:Wrapping an ant-design form component包装一个 ant-design 表单组件
【发布时间】:2017-04-07 20:06:28
【问题描述】:

我一直在试图弄清楚如何包装一个 ant-design 表单组件。我有几个Selects 会有相同的选项,所以我想创建一个SelectWrapper(见下面的sn-p)。不幸的是,antd 的表单似乎不喜欢这样,并且会出错

createBaseForm.js:98 Uncaught TypeError: Cannot read property 'onChange' of undefined

尽管我通过表单道具传递给Select

function ReusableCountrySelect({countries, ...props}) {
  return (
    <Select
      {...props}
    >
      {
        countries.map(c => (
          <Select.Option
            value={c.id}
            key={c.id}
          >{c.name}</Select.Option>
        ))
      }
    </Select>
  );
}

完整示例(传播需要 babel)

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
const mountNode = document.getElementById('root');

import {
  Form, Select, Button
} from 'antd';
const FormItem = Form.Item;
const Option = Select.Option;

function ReusableCountrySelect({countries, ...props}) {
  return (
    <Select
      {...props}
    >
      {
        countries.map(c => (
          <Select.Option
            value={c.id}
            key={c.id}
          >{c.name}</Select.Option>
        ))
      }
    </Select>
  );
}

class Demo 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}>

        <FormItem
          label="Select Country"
          hasFeedback
        >
          {getFieldDecorator('select', {
            rules: [
              { required: true, message: 'Please select your country!' },
            ],
          })(
            <ReusableCountrySelect
              countries={[
                {name: 'china', id: 'china'},
                {name: 'india', id: 'india'},
                {name: 'britain', id: 'britain'}
              ]}
              placeholder="Please select a country"
            />
          )}
        </FormItem>

        <FormItem
          wrapperCol={{ span: 12, offset: 6 }}
        >
          <Button type="primary" htmlType="submit">Submit</Button>
        </FormItem>
      </Form>
    );
  }
}

const WrappedDemo = Form.create()(Demo);

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

克隆 https://github.com/megawac/antd-form-issuenpm start 以查看问题

【问题讨论】:

    标签: javascript antd


    【解决方案1】:

    如何封装antd组件?传播爱和道具!!!

    import { Select as AntSelect} from 'antd'; 
    
    const Select = (props) => {
        return (<AntSelect {...props} >{props.children}</AntSelect>)
    }
    

    【讨论】:

    • 如果使用 Typescript,您可能会遇到问题。如果是这种情况,请从 Ant 导入 SelectProps,并传递 Ant 的 SelectProps 而不仅仅是“Props”。然后你基本上可以做同样的事情并传播。还将获得自动完成和 IDE 支持。
    【解决方案2】:

    解决于https://github.com/ant-design/ant-design/issues/5700

    表单需要控件的引用,而功能组件没有引用。

    解决方案是使用基于类的包装器组件代替基于功能的包装器组件。

    【讨论】:

    • 这对我有帮助。谢谢!!
    • 我认为这是 ant-design 4 之前的有效解决方案。现在 Makatun 的回应可能是要走的路(至少对我有用)。
    猜你喜欢
    • 2019-03-13
    • 1970-01-01
    • 2020-09-29
    • 2019-10-22
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多