Skip to content

Commit b1d639e

Browse files
author
wuzheqing
committed
Improve ModalHolder usage
1 parent 9b589e6 commit b1d639e

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

example/src/PropsBinding.jsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { useEffect, useState } from 'react';
22
import { Modal, Button } from 'antd';
3-
import NiceModal, { useModal, ModalHolder } from '@ebay/nice-modal-react';
3+
import NiceModal, {
4+
useModal, ModalHolder,
5+
// TS type friendly
6+
createModalHandler
7+
} from '@ebay/nice-modal-react';
48

59
export const MyAntdModal = NiceModal.create(({ time }) => {
610
const modal = useModal();
@@ -26,13 +30,15 @@ export default function Example() {
2630

2731
// modalHandler will be assign show/hide method.
2832
const modalHandler = {};
33+
// TS type friendly
34+
// const modalHandler = createModalHandler<typeof MyAntdModal>()
2935

3036
return (
3137
<>
32-
<Button type="primary" onClick={() => modalHandler.show()}>
38+
<Button type="primary" onClick={() => modalHandler.show({ time })}>
3339
Show Modal
3440
</Button>
35-
<ModalHolder modal={MyAntdModal} handler={modalHandler} time={time} />
41+
<ModalHolder modal={MyAntdModal} handler={modalHandler} />
3642
</>
3743
);
3844
}

src/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,13 @@ export const ModalHolder: React.FC<Record<string, unknown>> = ({
565565
return <ModalComp id={mid} {...restProps} />;
566566
};
567567

568+
export function createModalHandler<T extends React.ComponentType<any>>(): {
569+
show: (args?: Omit<React.ComponentProps<T>, keyof NiceModalHocProps>) => Promise<unknown>;
570+
hide: () => void;
571+
} {
572+
return Object.create(null);
573+
}
574+
568575
export const antdModal = (
569576
modal: NiceModalHandler,
570577
): { visible: boolean; onCancel: () => void; onOk: () => void; afterClose: () => void } => {

0 commit comments

Comments
 (0)