2016-03-31 09:40:55 +08:00
|
|
|
|
---
|
|
|
|
|
order: 3
|
2016-08-26 14:42:41 +08:00
|
|
|
|
title:
|
|
|
|
|
zh-CN: 条件触发
|
|
|
|
|
en-US: Conditional trigger
|
2016-03-31 09:40:55 +08:00
|
|
|
|
---
|
2015-12-04 14:58:19 +08:00
|
|
|
|
|
2016-08-26 14:42:41 +08:00
|
|
|
|
## zh-CN
|
|
|
|
|
|
2015-12-04 14:58:19 +08:00
|
|
|
|
可以判断是否需要弹出。
|
|
|
|
|
|
2016-08-26 14:42:41 +08:00
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
|
|
Make it pop up under some conditions.
|
|
|
|
|
|
2017-02-13 10:55:53 +08:00
|
|
|
|
````jsx
|
2015-12-04 14:58:19 +08:00
|
|
|
|
import { Popconfirm, Switch, message } from 'antd';
|
|
|
|
|
|
2017-02-20 22:13:19 +08:00
|
|
|
|
class App extends React.Component {
|
|
|
|
|
state = {
|
|
|
|
|
visible: false,
|
|
|
|
|
condition: true, // Whether meet the condition, if not show popconfirm.
|
2017-02-20 22:21:14 +08:00
|
|
|
|
}
|
2017-02-20 22:13:19 +08:00
|
|
|
|
changeCondition = (value) => {
|
2015-12-04 14:58:19 +08:00
|
|
|
|
this.setState({ condition: value });
|
2017-02-20 22:13:19 +08:00
|
|
|
|
}
|
|
|
|
|
confirm = () => {
|
2015-12-04 14:58:19 +08:00
|
|
|
|
this.setState({ visible: false });
|
2016-09-29 11:27:59 +08:00
|
|
|
|
message.success('Next step.');
|
2017-02-20 22:13:19 +08:00
|
|
|
|
}
|
|
|
|
|
cancel = () => {
|
2015-12-04 14:58:19 +08:00
|
|
|
|
this.setState({ visible: false });
|
2016-09-29 11:27:59 +08:00
|
|
|
|
message.error('Click on cancel.');
|
2017-02-20 22:13:19 +08:00
|
|
|
|
}
|
|
|
|
|
handleVisibleChange = (visible) => {
|
2015-12-04 14:58:19 +08:00
|
|
|
|
if (!visible) {
|
|
|
|
|
this.setState({ visible });
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-09-29 11:27:59 +08:00
|
|
|
|
// Determining condition before show the popconfirm.
|
2015-12-04 14:58:19 +08:00
|
|
|
|
console.log(this.state.condition);
|
|
|
|
|
if (this.state.condition) {
|
2016-09-29 11:27:59 +08:00
|
|
|
|
this.confirm(); // next step
|
2015-12-04 14:58:19 +08:00
|
|
|
|
} else {
|
2016-09-29 11:27:59 +08:00
|
|
|
|
this.setState({ visible }); // show the popconfirm
|
2015-12-04 14:58:19 +08:00
|
|
|
|
}
|
2017-02-20 22:13:19 +08:00
|
|
|
|
}
|
2015-12-04 14:58:19 +08:00
|
|
|
|
render() {
|
2016-01-07 16:29:12 +08:00
|
|
|
|
return (
|
|
|
|
|
<div>
|
2016-09-29 11:27:59 +08:00
|
|
|
|
<Popconfirm title="Are you sure delete this task?"
|
2016-01-08 14:41:05 +08:00
|
|
|
|
visible={this.state.visible} onVisibleChange={this.handleVisibleChange}
|
2016-06-06 13:54:10 +08:00
|
|
|
|
onConfirm={this.confirm} onCancel={this.cancel}
|
2016-09-29 11:27:59 +08:00
|
|
|
|
okText="Yes"
|
|
|
|
|
cancelText="No"
|
2016-06-06 13:54:10 +08:00
|
|
|
|
>
|
2016-09-29 11:27:59 +08:00
|
|
|
|
<a href="#">Delete a task</a>
|
2016-01-07 16:29:12 +08:00
|
|
|
|
</Popconfirm>
|
|
|
|
|
<br />
|
|
|
|
|
<br />
|
2016-09-29 11:27:59 +08:00
|
|
|
|
Whether directly execute:<Switch defaultChecked onChange={this.changeCondition} />
|
2016-01-07 16:29:12 +08:00
|
|
|
|
</div>
|
|
|
|
|
);
|
2017-02-20 22:13:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-04 14:58:19 +08:00
|
|
|
|
|
2015-12-29 12:08:58 +08:00
|
|
|
|
ReactDOM.render(<App />, mountNode);
|
2015-12-04 14:58:19 +08:00
|
|
|
|
````
|