1- import { useState , useCallback , useRef } from "react" ;
1+ import { useCallback , useRef , useState } from "react" ;
22import {
3- ModalManager ,
3+ ModalAction ,
4+ ModalComponent ,
45 ModalInstance ,
6+ ModalManager ,
57 ModalOptions ,
6- ModalAction ,
8+ ModalProps ,
79} from "./types" ;
810
911export function useModalManager ( ) : ModalManager {
@@ -75,15 +77,15 @@ export function useModalManager(): ModalManager {
7577 const open = useCallback (
7678 < T = any , R = any > (
7779 component :
78- | React . ComponentType < T >
79- | ( ( ) => Promise < { default : React . ComponentType < T > } > ) ,
80- data ?: any ,
80+ | ModalComponent < T , R >
81+ | ( ( ) => Promise < { default : ModalComponent < T , R > } > ) ,
82+ props ?: Omit < T , keyof ModalProps < R > > ,
8183 options ?: ModalOptions
8284 ) : Promise < R > => {
8385 const id = options ?. id || generateId ( ) ;
8486
8587 return new Promise < R > ( async ( resolve ) => {
86- let actualComponent : React . ComponentType < T > ;
88+ let actualComponent : ModalComponent < T , R > ;
8789
8890 const isLazyImport =
8991 typeof component === "function" && ! component . prototype ;
@@ -92,7 +94,7 @@ export function useModalManager(): ModalManager {
9294 setIsLoading ( true ) ;
9395 try {
9496 const module = await (
95- component as ( ) => Promise < { default : React . ComponentType < T > } >
97+ component as ( ) => Promise < { default : ModalComponent < T , R > } >
9698 ) ( ) ;
9799 actualComponent = module . default ;
98100 } catch ( error ) {
@@ -101,29 +103,26 @@ export function useModalManager(): ModalManager {
101103 }
102104 setIsLoading ( false ) ;
103105 } else {
104- actualComponent = component as React . ComponentType < T > ;
106+ actualComponent = component as ModalComponent < T , R > ;
105107 }
106108
107- const newModalInstance : ModalInstance = {
109+ const newModalInstance : ModalInstance < T , R > = {
108110 component : actualComponent ,
109111 id,
110112 isOpen : false ,
111113 onBeforeClose : ( callback : ( ) => boolean ) => {
112114 beforeCloseCallbacks . current . set ( id , callback ) ;
113115 } ,
114- close : ( value ) => {
116+ close : ( value ?: R ) => {
115117 if ( canClose ( id ) ) {
116- resolve ( value ) ;
118+ resolve ( value as R ) ;
117119 closeCurrent ( id ) ;
118120 }
119121 } ,
122+ props,
120123 index : 0 ,
121124 } ;
122125
123- if ( data ) {
124- newModalInstance . data = data ;
125- }
126-
127126 setStack ( ( prev ) => {
128127 const existingModal = prev . find ( ( modal ) => modal . id === id ) ;
129128 if ( existingModal ) {
@@ -151,6 +150,11 @@ export function useModalManager(): ModalManager {
151150
152151 const close = useCallback (
153152 ( n : number = 1 ) : boolean => {
153+ if ( typeof n !== "number" || n < 1 ) {
154+ throw new Error (
155+ `amount must be a number greater than 0. Received ${ n } `
156+ ) ;
157+ }
154158 let closedCount = 0 ;
155159 setStack ( ( prev ) => {
156160 let newStack = [ ...prev ] ;
0 commit comments