Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customized Form Control Switch with warning #5368

Closed
shaozj opened this issue Mar 17, 2017 · 2 comments
Closed

Customized Form Control Switch with warning #5368

shaozj opened this issue Mar 17, 2017 · 2 comments
Assignees
Labels
🐛 Bug Ant Design Team had proved that this is a bug.

Comments

@shaozj
Copy link

shaozj commented Mar 17, 2017

Environment(required)

  • antd version: 2.8.1
  • OS and its version: osx ei capitan
  • Browser and its version: google chrome 56.0.2924.87

What did you do? Please provide steps to re-produce your problem.

I created a customized Form control Switch based on antd Switch. And I got a warning.

code:

import {Switch as SwitchAntd} from 'antd';
import { Form, Button } from 'antd';
const FormItem = Form.Item;

class Switch extends React.Component {
  constructor(props) {
    super(props);
    const checked = (this.props.value == 1) ? true : false;
    let defaultProps = {
      checkedChildren: '开',
      unCheckedChildren: '关'
    } 
    console.log('this.props: ', this.props);
    this.state = {
      ...defaultProps,
      ...this.props,
      checked
    };
  }

  handleChange(checked) {
    this.setState({checked});
    const value  = checked ? 1 : 0;
    const onChange = this.props.onChange;
    onChange && onChange(value);
  }

  render() {
    return (
      <SwitchAntd {...this.state} onChange={::this.handleChange} />
    );
  }
}

class MyForm 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;
    const props = {
      checkedChildren: '上线',
      unCheckedChildren: '下线'
    };

    // Only show error after a field is touched.
    return (
      <Form onSubmit={this.handleSubmit}>
        <FormItem>
          {getFieldDecorator('open', {
            initialValue: 1
          })(
            <Switch {...props} />
          )}
        </FormItem>
        <FormItem>
          <Button
            type="primary"
            htmlType="submit"
          >
            查看
          </Button>
        </FormItem>
      </Form>
    );
  }
}

const WrappedMyForm = Form.create()(MyForm);

// write your demo code here, below is an example 

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

image

What do you expected?

No warning.

What happen?

Got a warning.

Re-producible online demo

Please paste my code on riddle or some online code running platform.

@afc163 afc163 added the 🐛 Bug Ant Design Team had proved that this is a bug. label Mar 17, 2017
@benjycui
Copy link
Contributor

Form.Item will add size=large to its children automatically.

@lock
Copy link

lock bot commented May 1, 2018

This thread has been automatically locked because it has not had recent activity. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked as resolved and limited conversation to collaborators May 1, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🐛 Bug Ant Design Team had proved that this is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants