@@ -209,11 +209,12 @@ const getModalId = (modal: string | React.FC<any>): string => {
209
209
210
210
/** omit id and partial all required props */
211
211
type NiceModalArgs < T > = T extends keyof JSX . IntrinsicElements | React . JSXElementConstructor < any >
212
- ? Partial < Omit < React . ComponentProps < T > , 'id' > >
212
+ ? Omit < React . ComponentProps < T > , 'id' >
213
213
: Record < string , unknown > ;
214
214
export function show < T extends any > ( modal : React . FC < any > , args ?: NiceModalArgs < React . FC < any > > ) : Promise < T > ;
215
215
// export function show<T extends any, C extends React.FC>(modal: C, args?: Omit<React.ComponentProps<C>, 'id'>): Promise<T>;
216
216
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 > ;
217
218
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
218
219
export function show ( modal : React . FC < any > | string , args ?: NiceModalArgs < React . FC < any > > | Record < string , unknown > ) {
219
220
const modalId = getModalId ( modal ) ;
@@ -275,11 +276,24 @@ const setFlags = (modalId: string, flags: Record<string, unknown>): void => {
275
276
dispatch ( setModalFlags ( modalId , flags ) ) ;
276
277
} ;
277
278
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
+ > (
280
286
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
+ } ;
283
297
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
284
298
export function useModal ( modal ?: any , args ?: any ) : any {
285
299
const modals = useContext ( NiceModalContext ) ;
@@ -389,7 +403,7 @@ export const create = <P extends {}>(Comp: React.ComponentType<P>): React.FC<P &
389
403
} ;
390
404
391
405
// 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 => {
393
407
if ( ! MODAL_REGISTRY [ id ] ) {
394
408
MODAL_REGISTRY [ id ] = { comp, props } ;
395
409
} else {
@@ -505,7 +519,7 @@ export const antdModal = (
505
519
export const antdModalV5 = (
506
520
modal : NiceModalHandler ,
507
521
) : { open : boolean ; onCancel : ( ) => void ; onOk : ( ) => void ; afterClose : ( ) => void } => {
508
- const { onOk, onCancel, afterClose} = antdModal ( modal )
522
+ const { onOk, onCancel, afterClose } = antdModal ( modal ) ;
509
523
return {
510
524
open : modal . visible ,
511
525
onOk,
@@ -530,7 +544,7 @@ export const antdDrawer = (
530
544
export const antdDrawerV5 = (
531
545
modal : NiceModalHandler ,
532
546
) : { open : boolean ; onClose : ( ) => void ; afterOpenChange : ( visible : boolean ) => void } => {
533
- const { onClose, afterVisibleChange : afterOpenChange } = antdDrawer ( modal )
547
+ const { onClose, afterVisibleChange : afterOpenChange } = antdDrawer ( modal ) ;
534
548
return {
535
549
open : modal . visible ,
536
550
onClose,
@@ -547,6 +561,21 @@ export const muiDialog = (modal: NiceModalHandler): { open: boolean; onClose: ()
547
561
} ,
548
562
} ;
549
563
} ;
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
+ } ;
550
579
export const bootstrapDialog = (
551
580
modal : NiceModalHandler ,
552
581
) : { show : boolean ; onHide : ( ) => void ; onExited : ( ) => void } => {
0 commit comments