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.
|
|
|
|
|
|
2019-05-07 14:57:32 +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,
|
2017-10-09 13:23:20 +08:00
|
|
|
|
condition: true, // Whether meet the condition, if not show popconfirm.
|
2019-05-07 14:57:32 +08:00
|
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
|
changeCondition = value => {
|
2015-12-04 14:58:19 +08:00
|
|
|
|
this.setState({ condition: value });
|
2019-05-07 14:57:32 +08:00
|
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
|
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.');
|
2019-05-07 14:57:32 +08:00
|
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
|
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.');
|
2019-05-07 14:57:32 +08:00
|
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
|
2019-05-07 14:57:32 +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) {
|
2017-10-09 13:23:20 +08:00
|
|
|
|
this.confirm(); // next step
|
2015-12-04 14:58:19 +08:00
|
|
|
|
} else {
|
2017-10-09 13:23:20 +08:00
|
|
|
|
this.setState({ visible }); // show the popconfirm
|
2015-12-04 14:58:19 +08:00
|
|
|
|
}
|
2019-05-07 14:57:32 +08:00
|
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
|
2015-12-04 14:58:19 +08:00
|
|
|
|
render() {
|
2016-01-07 16:29:12 +08:00
|
|
|
|
return (
|
|
|
|
|
<div>
|
2017-05-15 14:37:22 +08:00
|
|
|
|
<Popconfirm
|
|
|
|
|
title="Are you sure delete this task?"
|
|
|
|
|
visible={this.state.visible}
|
|
|
|
|
onVisibleChange={this.handleVisibleChange}
|
|
|
|
|
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 />
|
2019-05-07 14:57:32 +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);
|
2019-05-07 14:57:32 +08:00
|
|
|
|
```
|