Skip to content

Commit bb0e744

Browse files
committed
useBeforeClose now includes the close value in params
1 parent f166b3a commit bb0e744

3 files changed

Lines changed: 20 additions & 15 deletions

File tree

src/item-context.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ export function useModal() {
1313
return modal;
1414
}
1515

16-
export function useBeforeClose(callback: () => boolean) {
16+
export function useBeforeClose<R = unknown>(callback: (value?: R) => boolean) {
1717
const modal = useModal();
1818

1919
useEffect(() => {
20-
modal.onBeforeClose(callback);
20+
modal.onBeforeClose(callback as (value?: any) => boolean);
2121
}, [modal, callback]);
2222
}
2323

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface ModalInstance<T = any, ReturnValue = any> {
3131
isOpen: boolean;
3232
close: (value?: ReturnValue) => void;
3333
index: number;
34-
onBeforeClose: (callback: () => boolean) => void;
34+
onBeforeClose: (callback: (value?: ReturnValue) => boolean) => void;
3535
}
3636

3737
export interface ModalManager {

src/use-modal-manager.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export function useModalManager(): ModalManager {
1313
const [action, setAction] = useState<ModalAction>("none");
1414
const [isLoading, setIsLoading] = useState(false);
1515
const nextId = useRef(0);
16-
const beforeCloseCallbacks = useRef<Map<string, () => boolean>>(new Map());
16+
const beforeCloseCallbacks = useRef<
17+
Map<string, (value?: unknown) => boolean>
18+
>(new Map());
1719

1820
const generateId = useCallback(() => {
1921
return `modal-${nextId.current++}`;
@@ -27,9 +29,9 @@ export function useModalManager(): ModalManager {
2729
}));
2830
}, []);
2931

30-
const canClose = useCallback((id: string): boolean => {
32+
const canClose = useCallback((id: string, value?: unknown): boolean => {
3133
const callback = beforeCloseCallbacks.current.get(id);
32-
return callback ? callback() : true;
34+
return callback ? callback(value) : true;
3335
}, []);
3436

3537
const closeById = useCallback(
@@ -56,12 +58,12 @@ export function useModalManager(): ModalManager {
5658

5759
return wasClosed;
5860
},
59-
[updateModalStack, canClose]
61+
[updateModalStack]
6062
);
6163

6264
const closeCurrent = useCallback(
6365
(id: string): boolean => {
64-
let wasClosed = false;
66+
const wasClosed = true;
6567

6668
setStack((prev) => {
6769
const newStack = prev.filter((m) => m.id !== id);
@@ -71,7 +73,7 @@ export function useModalManager(): ModalManager {
7173

7274
return wasClosed;
7375
},
74-
[updateModalStack, canClose]
76+
[updateModalStack]
7577
);
7678

7779
const open = useCallback(
@@ -93,10 +95,10 @@ export function useModalManager(): ModalManager {
9395
if (isLazyImport) {
9496
setIsLoading(true);
9597
try {
96-
const module = await (
98+
const lazyModule = await (
9799
component as () => Promise<{ default: ModalComponent<T, R> }>
98100
)();
99-
actualComponent = module.default;
101+
actualComponent = lazyModule.default;
100102
} catch (error) {
101103
setIsLoading(false);
102104
throw error;
@@ -110,11 +112,14 @@ export function useModalManager(): ModalManager {
110112
component: actualComponent,
111113
id,
112114
isOpen: false,
113-
onBeforeClose: (callback: () => boolean) => {
114-
beforeCloseCallbacks.current.set(id, callback);
115+
onBeforeClose: (callback: (value?: R) => boolean) => {
116+
beforeCloseCallbacks.current.set(
117+
id,
118+
callback as (value?: unknown) => boolean
119+
);
115120
},
116121
close: (value?: R) => {
117-
if (canClose(id)) {
122+
if (canClose(id, value)) {
118123
resolve(value as R);
119124
closeCurrent(id);
120125
}
@@ -145,7 +150,7 @@ export function useModalManager(): ModalManager {
145150
});
146151
});
147152
},
148-
[generateId, updateModalStack, closeById, canClose]
153+
[generateId, updateModalStack, closeCurrent, canClose]
149154
);
150155

151156
const close = useCallback(

0 commit comments

Comments
 (0)