Skip to content

Commit

Permalink
Merge pull request #172 from Fiv5/feat/createModalHandler
Browse files Browse the repository at this point in the history
Improve ModalHolder usage
  • Loading branch information
supnate authored Dec 2, 2024
2 parents 9b589e6 + b1d639e commit 20e2c0c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 9 additions & 3 deletions example/src/PropsBinding.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useEffect, useState } from 'react';
import { Modal, Button } from 'antd';
import NiceModal, { useModal, ModalHolder } from '@ebay/nice-modal-react';
import NiceModal, {
useModal, ModalHolder,
// TS type friendly
createModalHandler
} from '@ebay/nice-modal-react';

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

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

return (
<>
<Button type="primary" onClick={() => modalHandler.show()}>
<Button type="primary" onClick={() => modalHandler.show({ time })}>
Show Modal
</Button>
<ModalHolder modal={MyAntdModal} handler={modalHandler} time={time} />
<ModalHolder modal={MyAntdModal} handler={modalHandler} />
</>
);
}
7 changes: 7 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,13 @@ export const ModalHolder: React.FC<Record<string, unknown>> = ({
return <ModalComp id={mid} {...restProps} />;
};

export function createModalHandler<T extends React.ComponentType<any>>(): {
show: (args?: Omit<React.ComponentProps<T>, keyof NiceModalHocProps>) => Promise<unknown>;
hide: () => void;
} {
return Object.create(null);
}

export const antdModal = (
modal: NiceModalHandler,
): { visible: boolean; onCancel: () => void; onOk: () => void; afterClose: () => void } => {
Expand Down

0 comments on commit 20e2c0c

Please sign in to comment.