Skip to content

Commit 5b3dbae

Browse files
committed
Add mui v5 dialog helper #65
1 parent 6cd83cf commit 5b3dbae

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

src/index.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import NiceModal, {
1111
antdModal,
1212
antdModalV5,
1313
muiDialog,
14+
muiDialogV5,
1415
bootstrapDialog,
1516
reducer,
1617
} from './index';
@@ -179,12 +180,12 @@ test('useModal by id of declared modal via ModalDef', async () => {
179180
modal = useModal('mytestmodal2');
180181
return (
181182
<Provider>
182-
<ModalDef id="mytestmodal2" component={HocTestModal} />
183+
<ModalDef id="mytestmodal2" component={HocTestModal} />
183184
</Provider>
184185
);
185186
};
186187
render(<App />);
187-
await testUseModal(modal, { name: 'bood'});
188+
await testUseModal(modal, { name: 'bood' });
188189
});
189190

190191
test('useModal by component directly', async () => {
@@ -427,6 +428,17 @@ test('test mui dialog helper', async () => {
427428
await testHelper(MuiDialog, muiDialog, 'MuiDialogTest');
428429
});
429430

431+
test('test mui v5 dialog helper', async () => {
432+
const MuiDialog = ({ open, onClose, TransitionProps: { onExited }, children }) => {
433+
return (
434+
<TestModal visible={open} onClose={onClose} onExited={onExited}>
435+
{children}
436+
</TestModal>
437+
);
438+
};
439+
await testHelper(MuiDialog, muiDialogV5, 'MuiDialogTest');
440+
});
441+
430442
test('test bootstrap dialog helper', async () => {
431443
const BootstrapDialog = ({ show, onHide, onExited, children }) => {
432444
return (

src/index.tsx

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,12 @@ const getModalId = (modal: string | React.FC<any>): string => {
209209

210210
/** omit id and partial all required props */
211211
type NiceModalArgs<T> = T extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>
212-
? Partial<Omit<React.ComponentProps<T>, 'id'>>
212+
? Omit<React.ComponentProps<T>, 'id'>
213213
: Record<string, unknown>;
214214
export function show<T extends any>(modal: React.FC<any>, args?: NiceModalArgs<React.FC<any>>): Promise<T>;
215215
// export function show<T extends any, C extends React.FC>(modal: C, args?: Omit<React.ComponentProps<C>, 'id'>): Promise<T>;
216216
export function show<T extends any>(modal: string, args?: Record<string, unknown>): Promise<T>;
217+
export function show<T extends any, P extends any>(modal: string, args: P): Promise<T>;
217218
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
218219
export function show(modal: React.FC<any> | string, args?: NiceModalArgs<React.FC<any>> | Record<string, unknown>) {
219220
const modalId = getModalId(modal);
@@ -275,11 +276,24 @@ const setFlags = (modalId: string, flags: Record<string, unknown>): void => {
275276
dispatch(setModalFlags(modalId, flags));
276277
};
277278
export function useModal(): NiceModalHandler;
278-
export function useModal<T extends string>(modal: T, args?: Record<string, unknown>): NiceModalHandler;
279-
export function useModal<T extends React.FC<any>>(
279+
export function useModal(modal: string, args?: Record<string, unknown>): NiceModalHandler;
280+
export function useModal<
281+
T extends React.FC<any>,
282+
ComponentProps extends NiceModalArgs<T>,
283+
PreparedProps extends Partial<ComponentProps> = {},
284+
RemainingProps = Omit<ComponentProps, keyof PreparedProps> & Partial<ComponentProps>,
285+
>(
280286
modal: T,
281-
args?: NiceModalArgs<T>,
282-
): NiceModalHandler<NiceModalArgs<T>>;
287+
args?: PreparedProps,
288+
): Omit<NiceModalHandler, 'show'> & {
289+
show: {
290+
[K in keyof RemainingProps]: RemainingProps[K] extends Exclude<RemainingProps[keyof RemainingProps], undefined>
291+
? K
292+
: never;
293+
}[keyof RemainingProps] extends undefined
294+
? (args?: RemainingProps) => Promise<unknown>
295+
: (args: RemainingProps) => Promise<unknown>;
296+
};
283297
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
284298
export function useModal(modal?: any, args?: any): any {
285299
const modals = useContext(NiceModalContext);
@@ -389,7 +403,7 @@ export const create = <P extends {}>(Comp: React.ComponentType<P>): React.FC<P &
389403
};
390404

391405
// All registered modals will be rendered in modal placeholder
392-
export const register = <T extends React.FC<any>>(id: string, comp: T, props?: NiceModalArgs<T>): void => {
406+
export const register = <T extends React.FC<any>>(id: string, comp: T, props?: Partial<NiceModalArgs<T>>): void => {
393407
if (!MODAL_REGISTRY[id]) {
394408
MODAL_REGISTRY[id] = { comp, props };
395409
} else {
@@ -505,7 +519,7 @@ export const antdModal = (
505519
export const antdModalV5 = (
506520
modal: NiceModalHandler,
507521
): { open: boolean; onCancel: () => void; onOk: () => void; afterClose: () => void } => {
508-
const {onOk, onCancel, afterClose} = antdModal(modal)
522+
const { onOk, onCancel, afterClose } = antdModal(modal);
509523
return {
510524
open: modal.visible,
511525
onOk,
@@ -530,7 +544,7 @@ export const antdDrawer = (
530544
export const antdDrawerV5 = (
531545
modal: NiceModalHandler,
532546
): { open: boolean; onClose: () => void; afterOpenChange: (visible: boolean) => void } => {
533-
const {onClose, afterVisibleChange: afterOpenChange} = antdDrawer(modal)
547+
const { onClose, afterVisibleChange: afterOpenChange } = antdDrawer(modal);
534548
return {
535549
open: modal.visible,
536550
onClose,
@@ -547,6 +561,21 @@ export const muiDialog = (modal: NiceModalHandler): { open: boolean; onClose: ()
547561
},
548562
};
549563
};
564+
565+
export const muiDialogV5 = (
566+
modal: NiceModalHandler,
567+
): { open: boolean; onClose: () => void; TransitionProps: { onExited: () => void } } => {
568+
return {
569+
open: modal.visible,
570+
onClose: () => modal.hide(),
571+
TransitionProps: {
572+
onExited: () => {
573+
modal.resolveHide();
574+
!modal.keepMounted && modal.remove();
575+
},
576+
},
577+
};
578+
};
550579
export const bootstrapDialog = (
551580
modal: NiceModalHandler,
552581
): { show: boolean; onHide: () => void; onExited: () => void } => {

0 commit comments

Comments
 (0)