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

71 lines
1.5 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-01-19 15:19:03 +08:00
````__react
2015-12-04 14:58:19 +08:00
import { Popconfirm, Switch, message } from 'antd';
2016-08-28 17:47:06 +08:00
const App = React.createClass({
2015-12-04 14:58:19 +08:00
getInitialState() {
return {
visible: false,
condition: true, // Whether meet the condition, if not show popconfirm.
2015-12-04 14:58:19 +08:00
};
},
changeCondition(value) {
this.setState({ condition: value });
},
confirm() {
this.setState({ visible: false });
message.success('Next step.');
2015-12-04 14:58:19 +08:00
},
cancel() {
this.setState({ visible: false });
message.error('Click on cancel.');
2015-12-04 14:58:19 +08:00
},
handleVisibleChange(visible) {
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
}
},
render() {
return (
<div>
<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>
);
2016-05-11 09:32:33 +08:00
},
2015-12-04 14:58:19 +08:00
});
ReactDOM.render(<App />, mountNode);
2015-12-04 14:58:19 +08:00
````