You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.5 KiB

---
order: 2
title:
zh-CN: 受控的 Checkbox
en-US: Controlled Checkbox
---
10 years ago
## zh-CN
10 years ago
联动 checkbox。
## en-US
Communicated with other components.
````jsx
import { Checkbox, Button } from 'antd';
10 years ago
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' }}>
8 years ago
<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={{ marginLeft: '10px' }}
type="primary"
size="small"
onClick={this.toggleDisable}
>
{!this.state.disabled ? 'Disable' : 'Enable'}
</Button>
</p>
</div>
);
}
}
10 years ago
ReactDOM.render(<App />, mountNode);
10 years ago
````