ant-design/components/checkbox/demo/controller.md
xrkffgg b032344d81
style: add checkout grop RTL (#22186)
* style: add checkout grop RTL

* style: add rtl.less

* fix: add test

* fix: add group test

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: 偏右 <afc163@gmail.com>
2020-03-13 15:09:40 +08:00

1.5 KiB

order title
2
zh-CN en-US
受控的 Checkbox Controlled Checkbox

zh-CN

联动 checkbox。

en-US

Communicated with other components.

import { Checkbox, Button } from 'antd';

class App extends React.Component {
  state = {
    checked: true,
    disabled: false,
  };

  toggleChecked = () => {
    this.setState({ checked: !this.state.checked });
  };

  toggleDisable = () => {
    this.setState({ disabled: !this.state.disabled });
  };

  onChange = e => {
    console.log('checked = ', e.target.checked);
    this.setState({
      checked: e.target.checked,
    });
  };

  render() {
    const label = `${this.state.checked ? 'Checked' : 'Unchecked'}-${
      this.state.disabled ? 'Disabled' : 'Enabled'
    }`;
    return (
      <div>
        <p style={{ marginBottom: '20px' }}>
          <Checkbox
            checked={this.state.checked}
            disabled={this.state.disabled}
            onChange={this.onChange}
          >
            {label}
          </Checkbox>
        </p>
        <p>
          <Button type="primary" size="small" onClick={this.toggleChecked}>
            {!this.state.checked ? 'Check' : 'Uncheck'}
          </Button>
          <Button
            style={{ margin: '0 10px' }}
            type="primary"
            size="small"
            onClick={this.toggleDisable}
          >
            {!this.state.disabled ? 'Disable' : 'Enable'}
          </Button>
        </p>
      </div>
    );
  }
}

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