Skip to content

Commit cfc9f55

Browse files
authored
fix: dialog.show method return close function (#740)
1 parent 1e0de22 commit cfc9f55

File tree

1 file changed

+18
-3
lines changed
  • packages/react-vant/src/components/dialog

1 file changed

+18
-3
lines changed

packages/react-vant/src/components/dialog/method.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import canUseDom from '../utils/dom/canUseDom'
99

1010
const Dialog = BaseDialog as DialogStatic
1111

12+
// 用于存储弹窗的销毁方法
13+
const dialogCloseMap = new Map<string, VoidFunction>()
14+
1215
// 可返回用于销毁此弹窗的方法
1316
Dialog.show = (props: DialogProps) => {
1417
if (!canUseDom()) return null
@@ -35,7 +38,8 @@ Dialog.show = (props: DialogProps) => {
3538
const userContainer = resolveContainer(props.teleport)
3639
const container = document.createElement('div')
3740
userContainer.appendChild(container)
38-
let destroy = noop
41+
42+
const id = `rv-dialog-root--${Date.now()}`
3943

4044
const TempDialog = () => {
4145
const [visible, setVisible] = useState(false)
@@ -46,10 +50,13 @@ Dialog.show = (props: DialogProps) => {
4650
setVisible(true)
4751
}, [])
4852

49-
destroy = () => {
53+
const destroy = () => {
5054
setVisible(false)
5155
if (onClose) onClose()
5256
}
57+
58+
dialogCloseMap[id] = destroy
59+
5360
const _afterClose = () => {
5461
if (onClosed) {
5562
onClosed()
@@ -102,7 +109,15 @@ Dialog.show = (props: DialogProps) => {
102109
}
103110
render(<TempDialog />, container)
104111

105-
return destroy
112+
const close = () => {
113+
if (!dialogCloseMap.has(id)) {
114+
return;
115+
}
116+
dialogCloseMap.get(id)?.();
117+
dialogCloseMap.delete(id);
118+
}
119+
120+
return close
106121
}
107122

108123
// 可使用 async/await 的方式

0 commit comments

Comments
 (0)