Skip to content

Commit

Permalink
feat: 为Modal增加show方法
Browse files Browse the repository at this point in the history
  • Loading branch information
chj-damon committed Sep 2, 2024
1 parent e8e9632 commit da60d29
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/giant-tables-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/react-native': patch
---

feat: 为Modal增加show方法,实现指令式打开弹窗的功能
3 changes: 2 additions & 1 deletion packages/react-native/src/modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import alert from './alert';
import confirm from './confirm';
import Modal from './Modal';
import prompt from './prompt';
import show from './show';
import tip from './tip';

export default Object.assign(Modal, { alert, confirm, prompt, tip });
export default Object.assign(Modal, { alert, confirm, prompt, tip, show });
43 changes: 43 additions & 0 deletions packages/react-native/src/modal/show/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React, { PropsWithChildren } from 'react';

import { useSafeState } from '@td-design/rn-hooks';

import Portal from '../../portal';
import ModalView from '../Modal/ModalView';
import { ModalProps } from '../type';

export default function show(
comp: React.ReactElement,
props?: Omit<ModalProps, 'onAnimationEnd' | 'visible' | 'onClose'>
) {
const key = Portal.add(
<ModalContent
{...props}
onAnimationEnd={visible => {
if (!visible) {
Portal.remove(key);
}
}}
>
{comp}
</ModalContent>
);
}

const ModalContent = ({ children, ...props }: PropsWithChildren<Omit<ModalProps, 'visible' | 'onClose'>>) => {
const [visible, setVisible] = useSafeState(true);

return (
<ModalView
position="center"
maskVisible
maskClosable
animationType="slide"
{...props}
visible={visible}
onClose={() => setVisible(false)}
>
{children}
</ModalView>
);
};

0 comments on commit da60d29

Please sign in to comment.