mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-16 09:39:10 +08:00
5cf579c37a
* docs(badge): replace class component with hooks * docs(button): replace class component with hooks * docs(calendar): replace class component with hooks * docs(card): replace class component with hooks * docs(button): replace class component with hooks * chore(deps): remove webpack devDependencies * docs(cascader): replace class component with hooks * docs(checkbox): replace class component with hooks * docs(collapse): replace class component with hooks * docs(comment): replace class component with hooks * docs(descriptions): replace class component with hooks
58 lines
1.2 KiB
Markdown
58 lines
1.2 KiB
Markdown
---
|
|
order: 2
|
|
title:
|
|
zh-CN: 受控的 Checkbox
|
|
en-US: Controlled Checkbox
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
联动 checkbox。
|
|
|
|
## en-US
|
|
|
|
Communicated with other components.
|
|
|
|
```jsx
|
|
import React, { useState } from 'react';
|
|
import { Checkbox, Button } from 'antd';
|
|
|
|
export default () => {
|
|
const [checked, setChecked] = useState(true);
|
|
const [disabled, setDisabled] = useState(false);
|
|
|
|
const toggleChecked = () => {
|
|
setChecked(!checked);
|
|
};
|
|
|
|
const toggleDisable = () => {
|
|
setDisabled(!disabled);
|
|
};
|
|
|
|
const onChange = e => {
|
|
console.log('checked = ', e.target.checked);
|
|
setChecked(e.target.checked);
|
|
};
|
|
|
|
const label = `${checked ? 'Checked' : 'Unchecked'}-${disabled ? 'Disabled' : 'Enabled'}`;
|
|
|
|
return (
|
|
<>
|
|
<p style={{ marginBottom: '20px' }}>
|
|
<Checkbox checked={checked} disabled={disabled} onChange={onChange}>
|
|
{label}
|
|
</Checkbox>
|
|
</p>
|
|
<p>
|
|
<Button type="primary" size="small" onClick={toggleChecked}>
|
|
{!checked ? 'Check' : 'Uncheck'}
|
|
</Button>
|
|
<Button style={{ margin: '0 10px' }} type="primary" size="small" onClick={toggleDisable}>
|
|
{!disabled ? 'Disable' : 'Enable'}
|
|
</Button>
|
|
</p>
|
|
</>
|
|
);
|
|
};
|
|
```
|