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

75 lines
1.4 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.
```tsx
2022-05-23 14:37:16 +08:00
import { message, Popconfirm, Switch } from 'antd';
import React, { useState } from 'react';
2015-12-04 14:58:19 +08:00
const App: React.FC = () => {
const [visible, setVisible] = useState(false);
const [condition, setCondition] = useState(true);
2018-06-27 15:55:04 +08:00
const changeCondition = (checked: boolean) => {
setCondition(checked);
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
const confirm = () => {
setVisible(false);
message.success('Next step.');
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
const cancel = () => {
setVisible(false);
message.error('Click on cancel.');
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
const handleVisibleChange = (newVisible: boolean) => {
if (!newVisible) {
setVisible(newVisible);
2015-12-04 14:58:19 +08:00
return;
}
// Determining condition before show the popconfirm.
console.log(condition);
if (condition) {
confirm(); // next step
2015-12-04 14:58:19 +08:00
} else {
setVisible(newVisible);
2015-12-04 14:58:19 +08:00
}
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
return (
<div>
<Popconfirm
title="Are you sure delete this task?"
visible={visible}
onVisibleChange={handleVisibleChange}
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
export default App;
2019-05-07 14:57:32 +08:00
```