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.
|
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
|
```tsx
|
2022-05-23 14:37:16 +08:00
|
|
|
|
import { message, Popconfirm, Switch } from 'antd';
|
2022-05-19 09:46:26 +08:00
|
|
|
|
import React, { useState } from 'react';
|
2015-12-04 14:58:19 +08:00
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
|
const App: React.FC = () => {
|
2022-08-26 17:17:13 +08:00
|
|
|
|
const [open, setOpen] = useState(false);
|
2022-05-19 09:46:26 +08:00
|
|
|
|
const [condition, setCondition] = useState(true);
|
2018-06-27 15:55:04 +08:00
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
|
const changeCondition = (checked: boolean) => {
|
|
|
|
|
setCondition(checked);
|
2019-05-07 14:57:32 +08:00
|
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
|
const confirm = () => {
|
2022-08-26 17:17:13 +08:00
|
|
|
|
setOpen(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
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
|
const cancel = () => {
|
2022-08-26 17:17:13 +08:00
|
|
|
|
setOpen(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
|
|
|
|
|
2022-08-26 17:17:13 +08:00
|
|
|
|
const handleOpenChange = (newOpen: boolean) => {
|
|
|
|
|
if (!newOpen) {
|
|
|
|
|
setOpen(newOpen);
|
2015-12-04 14:58:19 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-09-29 11:27:59 +08:00
|
|
|
|
// Determining condition before show the popconfirm.
|
2022-05-19 09:46:26 +08:00
|
|
|
|
console.log(condition);
|
|
|
|
|
if (condition) {
|
|
|
|
|
confirm(); // next step
|
2015-12-04 14:58:19 +08:00
|
|
|
|
} else {
|
2022-08-26 17:17:13 +08:00
|
|
|
|
setOpen(newOpen);
|
2015-12-04 14:58:19 +08:00
|
|
|
|
}
|
2019-05-07 14:57:32 +08:00
|
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<Popconfirm
|
|
|
|
|
title="Are you sure delete this task?"
|
2022-08-26 17:17:13 +08:00
|
|
|
|
open={open}
|
|
|
|
|
onOpenChange={handleOpenChange}
|
2022-05-19 09:46:26 +08:00
|
|
|
|
onConfirm={confirm}
|
|
|
|
|
onCancel={cancel}
|
|
|
|
|
okText="Yes"
|
|
|
|
|
cancelText="No"
|
|
|
|
|
>
|
|
|
|
|
<a href="#">Delete a task</a>
|
|
|
|
|
</Popconfirm>
|
|
|
|
|
<br />
|
|
|
|
|
<br />
|
|
|
|
|
Whether directly execute:
|
|
|
|
|
<Switch defaultChecked onChange={changeCondition} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
2015-12-04 14:58:19 +08:00
|
|
|
|
|
2022-04-15 16:20:56 +08:00
|
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
|
```
|