2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 2
|
2016-08-18 11:22:55 +08:00
|
|
|
title:
|
2019-05-07 14:57:32 +08:00
|
|
|
zh-CN: 受控的 Checkbox
|
|
|
|
en-US: Controlled Checkbox
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2015-07-15 15:19:24 +08:00
|
|
|
|
2016-08-18 11:22:55 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2015-07-15 15:19:24 +08:00
|
|
|
联动 checkbox。
|
|
|
|
|
2016-08-18 11:22:55 +08:00
|
|
|
## en-US
|
|
|
|
|
|
|
|
Communicated with other components.
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
2015-10-28 20:55:49 +08:00
|
|
|
import { Checkbox, Button } from 'antd';
|
2015-07-15 15:19:24 +08:00
|
|
|
|
2017-02-20 10:09:37 +08:00
|
|
|
class App extends React.Component {
|
|
|
|
state = {
|
|
|
|
checked: true,
|
|
|
|
disabled: false,
|
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2018-11-28 15:00:03 +08:00
|
|
|
toggleChecked = () => {
|
|
|
|
this.setState({ checked: !this.state.checked });
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-11-28 15:00:03 +08:00
|
|
|
|
|
|
|
toggleDisable = () => {
|
|
|
|
this.setState({ disabled: !this.state.disabled });
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-11-28 15:00:03 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
onChange = e => {
|
2018-11-28 15:00:03 +08:00
|
|
|
console.log('checked = ', e.target.checked);
|
|
|
|
this.setState({
|
|
|
|
checked: e.target.checked,
|
|
|
|
});
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-11-28 15:00:03 +08:00
|
|
|
|
2015-07-16 14:34:42 +08:00
|
|
|
render() {
|
2019-05-07 14:57:32 +08:00
|
|
|
const label = `${this.state.checked ? 'Checked' : 'Unchecked'}-${
|
|
|
|
this.state.disabled ? 'Disabled' : 'Enabled'
|
|
|
|
}`;
|
2016-01-07 16:29:12 +08:00
|
|
|
return (
|
2020-07-07 22:06:00 +08:00
|
|
|
<>
|
2016-01-27 16:44:50 +08:00
|
|
|
<p style={{ marginBottom: '20px' }}>
|
2017-05-15 16:36:07 +08:00
|
|
|
<Checkbox
|
|
|
|
checked={this.state.checked}
|
2016-02-17 18:02:33 +08:00
|
|
|
disabled={this.state.disabled}
|
2016-06-06 13:54:10 +08:00
|
|
|
onChange={this.onChange}
|
|
|
|
>
|
2016-02-17 18:02:33 +08:00
|
|
|
{label}
|
|
|
|
</Checkbox>
|
|
|
|
</p>
|
|
|
|
<p>
|
2019-05-07 14:57:32 +08:00
|
|
|
<Button type="primary" size="small" onClick={this.toggleChecked}>
|
2016-08-18 11:22:55 +08:00
|
|
|
{!this.state.checked ? 'Check' : 'Uncheck'}
|
2016-02-17 18:02:33 +08:00
|
|
|
</Button>
|
2017-05-15 14:37:22 +08:00
|
|
|
<Button
|
2020-03-13 15:09:40 +08:00
|
|
|
style={{ margin: '0 10px' }}
|
2017-05-15 14:37:22 +08:00
|
|
|
type="primary"
|
|
|
|
size="small"
|
2016-06-06 13:54:10 +08:00
|
|
|
onClick={this.toggleDisable}
|
|
|
|
>
|
2016-08-18 11:22:55 +08:00
|
|
|
{!this.state.disabled ? 'Disable' : 'Enable'}
|
2016-02-17 18:02:33 +08:00
|
|
|
</Button>
|
|
|
|
</p>
|
2020-07-07 22:06:00 +08:00
|
|
|
</>
|
2016-01-07 16:29:12 +08:00
|
|
|
);
|
2017-02-20 10:09:37 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-15 15:19:24 +08:00
|
|
|
|
2015-12-29 12:08:58 +08:00
|
|
|
ReactDOM.render(<App />, mountNode);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|