@@ -4,12 +4,11 @@ import { randomId } from '@mantine/hooks';
44import { ConfirmModal } from './ConfirmModal' ;
55import {
66 ConfirmLabels ,
7- ContextModalProps ,
7+ MantineModals ,
88 ModalsContext ,
99 ModalsContextProps ,
1010 ModalSettings ,
1111 OpenConfirmModal ,
12- OpenContextModal ,
1312} from './context' ;
1413import { useModalsEvents } from './events' ;
1514import { modalsReducer } from './reducer' ;
@@ -19,7 +18,7 @@ export interface ModalsProviderProps {
1918 children ?: React . ReactNode ;
2019
2120 /** Predefined modals */
22- modals ?: Record < string , React . FC < ContextModalProps < any > > > ;
21+ modals ?: MantineModals ;
2322
2423 /** Shared Modal component props, applied for every modal */
2524 modalProps ?: ModalSettings ;
@@ -72,17 +71,9 @@ export function ModalsProvider({ children, modalProps, labels, modals }: ModalsP
7271 const stateRef = useRef ( state ) ;
7372 stateRef . current = state ;
7473
75- const closeAll = useCallback (
76- ( canceled ?: boolean ) => {
77- dispatch ( { type : 'CLOSE_ALL' , canceled } ) ;
78- } ,
79- [ stateRef , dispatch ]
80- ) ;
81-
82- const openModal = useCallback (
83- ( { modalId, ...props } : ModalSettings ) => {
74+ const openModal : ModalsContextProps [ 'openModal' ] = useCallback (
75+ ( { modalId, ...props } ) => {
8476 const id = modalId || randomId ( ) ;
85-
8677 dispatch ( {
8778 type : 'OPEN' ,
8879 modal : {
@@ -96,8 +87,8 @@ export function ModalsProvider({ children, modalProps, labels, modals }: ModalsP
9687 [ dispatch ]
9788 ) ;
9889
99- const openConfirmModal = useCallback (
100- ( { modalId, ...props } : OpenConfirmModal ) => {
90+ const openConfirmModal : ModalsContextProps [ 'openConfirmModal' ] = useCallback (
91+ ( { modalId, ...props } ) => {
10192 const id = modalId || randomId ( ) ;
10293 dispatch ( {
10394 type : 'OPEN' ,
@@ -112,55 +103,72 @@ export function ModalsProvider({ children, modalProps, labels, modals }: ModalsP
112103 [ dispatch ]
113104 ) ;
114105
115- const openContextModal = useCallback (
116- ( modal : string , { modalId, ...props } : OpenContextModal ) => {
106+ const openContextModal : ModalsContextProps [ 'openContextModal' ] = useCallback (
107+ ( { modalId, modalKey , ...props } ) => {
117108 const id = modalId || randomId ( ) ;
118109 dispatch ( {
119110 type : 'OPEN' ,
120111 modal : {
121112 id,
122113 type : 'context' ,
123114 props,
124- ctx : modal ,
115+ ctx : modalKey ,
125116 } ,
126117 } ) ;
127118 return id ;
128119 } ,
129120 [ dispatch ]
130121 ) ;
131122
132- const closeModal = useCallback (
133- ( id : string , canceled ?: boolean ) => {
123+ const closeModal : ModalsContextProps [ 'closeModal' ] = useCallback (
124+ ( modalId : string , canceled ?: boolean ) => {
125+ dispatch ( { type : 'CLOSE' , modalId, canceled } ) ;
126+ } ,
127+ [ stateRef , dispatch ]
128+ ) ;
129+
130+ const closeContextModal : ModalsContextProps [ 'closeContextModal' ] = useCallback (
131+ ( modalKey : string , canceled ?: boolean ) => {
132+ const id =
133+ stateRef . current . modals . find ( ( m ) => m . type === 'context' && m . ctx === modalKey ) ?. id ??
134+ modalKey ;
134135 dispatch ( { type : 'CLOSE' , modalId : id , canceled } ) ;
135136 } ,
136137 [ stateRef , dispatch ]
137138 ) ;
138139
139- const updateModal = useCallback (
140- ( { modalId, ...newProps } : Partial < ModalSettings > & { modalId : string } ) => {
141- dispatch ( {
142- type : 'UPDATE' ,
143- modalId,
144- newProps,
145- } ) ;
140+ const closeAllModals : ModalsContextProps [ 'closeAllModals' ] = useCallback (
141+ ( canceled ?: boolean ) => {
142+ dispatch ( { type : 'CLOSE_ALL' , canceled } ) ;
146143 } ,
147- [ dispatch ]
144+ [ stateRef , dispatch ]
148145 ) ;
149146
150- const updateContextModal = useCallback (
151- ( { modalId, ...newProps } : { modalId : string } & Partial < OpenContextModal < any > > ) => {
147+ const updateModal : ModalsContextProps [ 'updateModal' ] = useCallback (
148+ ( { modalId, ...newProps } ) => {
152149 dispatch ( { type : 'UPDATE' , modalId, newProps } ) ;
153150 } ,
154151 [ dispatch ]
155152 ) ;
156153
154+ const updateContextModal : ModalsContextProps [ 'updateContextModal' ] = useCallback (
155+ ( { modalKey, modalId, ...newProps } ) => {
156+ const id =
157+ modalId ??
158+ stateRef . current . modals . find ( ( m ) => m . type === 'context' && m . ctx === modalKey ) ?. id ??
159+ modalKey ;
160+ dispatch ( { type : 'UPDATE' , newProps, modalId : id } ) ;
161+ } ,
162+ [ stateRef , dispatch ]
163+ ) ;
164+
157165 useModalsEvents ( {
158166 openModal,
159167 openConfirmModal,
160- openContextModal : ( { modal , ... payload } : any ) => openContextModal ( modal , payload ) ,
168+ openContextModal,
161169 closeModal,
162- closeContextModal : closeModal ,
163- closeAllModals : closeAll ,
170+ closeContextModal,
171+ closeAllModals,
164172 updateModal,
165173 updateContextModal,
166174 } ) ;
@@ -172,8 +180,8 @@ export function ModalsProvider({ children, modalProps, labels, modals }: ModalsP
172180 openConfirmModal,
173181 openContextModal,
174182 closeModal,
175- closeContextModal : closeModal ,
176- closeAll ,
183+ closeContextModal,
184+ closeAllModals ,
177185 updateModal,
178186 updateContextModal,
179187 } ;
@@ -231,7 +239,7 @@ export function ModalsProvider({ children, modalProps, labels, modals }: ModalsP
231239 { ...modalProps }
232240 { ...currentModalProps }
233241 opened = { state . modals . length > 0 }
234- onClose = { ( ) => closeModal ( state . current ?. id as any ) }
242+ onClose = { ( ) => closeModal ( state . current ?. id as string ) }
235243 >
236244 { content }
237245 </ Modal >
0 commit comments