Skip to content

Commit 41c23b5

Browse files
authored
Merge pull request #898 from thundersdata-frontend/rn-issue
fix: 修复弹窗子组件内无法关闭弹窗的bug
2 parents aace344 + f61c089 commit 41c23b5

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

.changeset/hip-worms-hide.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+
fix: 修复弹窗子组件内无法关闭弹窗的bug

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import React, { PropsWithChildren } from 'react';
1+
import React from 'react';
22

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

55
import Portal from '../../portal';
66
import ModalView from '../Modal/ModalView';
7-
import { ModalProps } from '../type';
7+
import { ImperativeModalChildrenProps, ModalProps } from '../type';
88

99
export default function show(
10-
comp: React.ReactElement,
10+
comp: React.ReactElement<ImperativeModalChildrenProps<{}>>,
1111
props?: Omit<ModalProps, 'onAnimationEnd' | 'visible' | 'onClose'>
1212
) {
1313
const key = Portal.add(
@@ -24,7 +24,10 @@ export default function show(
2424
);
2525
}
2626

27-
const ModalContent = ({ children, ...props }: PropsWithChildren<Omit<ModalProps, 'visible' | 'onClose'>>) => {
27+
const ModalContent = ({
28+
children,
29+
...props
30+
}: Omit<ModalProps, 'visible' | 'onClose'> & { children: React.ReactElement<ImperativeModalChildrenProps<{}>> }) => {
2831
const [visible, setVisible] = useSafeState(true);
2932

3033
return (
@@ -37,7 +40,14 @@ const ModalContent = ({ children, ...props }: PropsWithChildren<Omit<ModalProps,
3740
visible={visible}
3841
onClose={() => setVisible(false)}
3942
>
40-
{children}
43+
{React.isValidElement(children)
44+
? React.cloneElement(children, {
45+
// Add any props you want to pass to the child here
46+
closeModal() {
47+
setVisible(false);
48+
},
49+
})
50+
: null}
4151
</ModalView>
4252
);
4353
};

packages/react-native/src/modal/type.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,8 @@ export type TipProps = Omit<AlertProps, 'icon' | 'onPress' | 'confirmText'> & {
7070
/** 关闭图标的不透明度 */
7171
closeIconActiveOpacity?: number;
7272
};
73+
74+
export type ImperativeModalChildrenProps<P> = P & {
75+
/** 在弹窗组件内调用,用以关闭弹窗 */
76+
closeModal: () => void;
77+
};

0 commit comments

Comments
 (0)