Skip to content

Commit b85034d

Browse files
authored
Merge pull request #896 from thundersdata-frontend/rn-issue
feat: 为Modal增加show方法
2 parents 1b65cfa + da60d29 commit b85034d

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

.changeset/giant-tables-hunt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@td-design/react-native': patch
3+
---
4+
5+
feat: 为Modal增加show方法,实现指令式打开弹窗的功能

packages/react-native/src/modal/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import alert from './alert';
22
import confirm from './confirm';
33
import Modal from './Modal';
44
import prompt from './prompt';
5+
import show from './show';
56
import tip from './tip';
67

7-
export default Object.assign(Modal, { alert, confirm, prompt, tip });
8+
export default Object.assign(Modal, { alert, confirm, prompt, tip, show });
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React, { PropsWithChildren } from 'react';
2+
3+
import { useSafeState } from '@td-design/rn-hooks';
4+
5+
import Portal from '../../portal';
6+
import ModalView from '../Modal/ModalView';
7+
import { ModalProps } from '../type';
8+
9+
export default function show(
10+
comp: React.ReactElement,
11+
props?: Omit<ModalProps, 'onAnimationEnd' | 'visible' | 'onClose'>
12+
) {
13+
const key = Portal.add(
14+
<ModalContent
15+
{...props}
16+
onAnimationEnd={visible => {
17+
if (!visible) {
18+
Portal.remove(key);
19+
}
20+
}}
21+
>
22+
{comp}
23+
</ModalContent>
24+
);
25+
}
26+
27+
const ModalContent = ({ children, ...props }: PropsWithChildren<Omit<ModalProps, 'visible' | 'onClose'>>) => {
28+
const [visible, setVisible] = useSafeState(true);
29+
30+
return (
31+
<ModalView
32+
position="center"
33+
maskVisible
34+
maskClosable
35+
animationType="slide"
36+
{...props}
37+
visible={visible}
38+
onClose={() => setVisible(false)}
39+
>
40+
{children}
41+
</ModalView>
42+
);
43+
};

0 commit comments

Comments
 (0)