ant-design/components/popconfirm/demo/dynamic-trigger.md

72 lines
1.6 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 3
title:
zh-CN: 条件触发
en-US: Conditional trigger
2016-03-31 09:40:55 +08:00
---
2015-12-04 14:58:19 +08:00
## zh-CN
2015-12-04 14:58:19 +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';
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
}
changeCondition = (value) => {
2015-12-04 14:58:19 +08:00
this.setState({ condition: value });
}
confirm = () => {
2015-12-04 14:58:19 +08:00
this.setState({ visible: false });
message.success('Next step.');
}
cancel = () => {
2015-12-04 14:58:19 +08:00
this.setState({ visible: false });
message.error('Click on cancel.');
}
handleVisibleChange = (visible) => {
2015-12-04 14:58:19 +08:00
if (!visible) {
this.setState({ visible });
return;
}
// Determining condition before show the popconfirm.
2015-12-04 14:58:19 +08:00
console.log(this.state.condition);
if (this.state.condition) {
this.confirm(); // next step
2015-12-04 14:58:19 +08:00
} else {
this.setState({ visible }); // show the popconfirm
2015-12-04 14:58:19 +08:00
}
}
2015-12-04 14:58:19 +08:00
render() {
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}
okText="Yes"
cancelText="No"
>
<a href="#">Delete a task</a>
</Popconfirm>
<br />
<br />
Whether directly execute<Switch defaultChecked onChange={this.changeCondition} />
</div>
);
}
}
2015-12-04 14:58:19 +08:00
ReactDOM.render(<App />, mountNode);
2015-12-04 14:58:19 +08:00
````