2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 2
|
|
|
|
title: 和外部组件通信
|
|
|
|
---
|
2015-07-15 15:19:24 +08:00
|
|
|
|
|
|
|
联动 checkbox。
|
|
|
|
|
2016-03-31 09:40:55 +08:00
|
|
|
|
2015-07-15 15:19:24 +08:00
|
|
|
|
|
|
|
````jsx
|
2015-10-28 20:55:49 +08:00
|
|
|
import { Checkbox, Button } from 'antd';
|
2015-07-15 15:19:24 +08:00
|
|
|
|
2015-10-28 20:55:49 +08:00
|
|
|
const App = React.createClass({
|
2015-07-16 14:34:42 +08:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
checked: true,
|
|
|
|
disabled: false
|
2015-10-28 20:55:49 +08:00
|
|
|
};
|
2015-07-16 14:34:42 +08:00
|
|
|
},
|
|
|
|
render() {
|
2016-02-17 18:04:42 +08:00
|
|
|
const label = `${this.state.checked ? '选中' : '取消'}-${this.state.disabled ? '不可用' : '可用'}`;
|
2016-01-07 16:29:12 +08:00
|
|
|
return (
|
|
|
|
<div>
|
2016-01-27 16:44:50 +08:00
|
|
|
<p style={{ marginBottom: '20px' }}>
|
2016-02-17 18:02:33 +08:00
|
|
|
<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 ? '选中' : '取消'}
|
|
|
|
</Button>
|
|
|
|
<Button style={{ marginLeft: '10px' }}
|
|
|
|
type="primary" size="small"
|
|
|
|
onClick={this.toggleDisable}>
|
|
|
|
{!this.state.disabled ? '不可用' : '可用'}
|
|
|
|
</Button>
|
|
|
|
</p>
|
|
|
|
</div>
|
2016-01-07 16:29:12 +08:00
|
|
|
);
|
2015-07-16 14:34:42 +08:00
|
|
|
},
|
2015-12-27 16:20:59 +08:00
|
|
|
toggleChecked() {
|
2016-01-27 16:44:50 +08:00
|
|
|
this.setState({ checked: !this.state.checked });
|
2015-07-16 14:34:42 +08:00
|
|
|
},
|
2015-12-27 16:20:59 +08:00
|
|
|
toggleDisable() {
|
2016-01-27 16:44:50 +08:00
|
|
|
this.setState({ disabled: !this.state.disabled });
|
2015-07-16 14:34:42 +08:00
|
|
|
},
|
|
|
|
onChange(e) {
|
|
|
|
console.log('checked = ', e.target.checked);
|
2015-07-16 14:48:57 +08:00
|
|
|
this.setState({
|
2015-12-27 16:20:59 +08:00
|
|
|
checked: e.target.checked,
|
2015-07-16 14:48:57 +08:00
|
|
|
});
|
2015-07-16 14:34:42 +08:00
|
|
|
}
|
2015-07-15 15:19:24 +08:00
|
|
|
});
|
|
|
|
|
2015-12-29 12:08:58 +08:00
|
|
|
ReactDOM.render(<App />, mountNode);
|
2015-07-15 15:19:24 +08:00
|
|
|
````
|