Skip to content

Commit 06090dc

Browse files
authored
fix(useDialogContainer): pass DialogContainer props (#615)
1 parent e5b912b commit 06090dc

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

.changeset/modern-cats-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cube-dev/ui-kit': patch
3+
---
4+
5+
Allow to pass props for DialogContainer in useDialogContainer hook.
Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import React, { useState, useMemo } from 'react';
1+
import {
2+
useState,
3+
useMemo,
4+
ComponentProps,
5+
ComponentType,
6+
useRef,
7+
} from 'react';
28

39
import { useEvent } from '../../../_internal/index';
410

@@ -10,30 +16,60 @@ import { DialogContainer } from './DialogContainer';
1016
* @param Component - A React component that represents the dialog content. It must accept props of type P.
1117
* @returns An object with `open` function to open the dialog with provided props and `rendered` JSX element to include in your component tree.
1218
*/
13-
export function useDialogContainer<P>(Component: React.ComponentType<P>) {
19+
export function useDialogContainer<
20+
P,
21+
E = ComponentProps<typeof DialogContainer>,
22+
>(Component: ComponentType<P>) {
1423
const [isOpen, setIsOpen] = useState(false);
1524
const [componentProps, setComponentProps] = useState<P | null>(null);
25+
const [containerProps, setContainerProps] = useState<E | null>(null);
26+
const setupRef = useRef(false);
1627

1728
// 'open' accepts props required by the Component and opens the dialog
18-
const open = useEvent((props: P) => {
29+
const open = useEvent((props: P, containerProps?: E) => {
1930
setComponentProps(props);
31+
setContainerProps(containerProps ?? null);
2032
setIsOpen(true);
2133
});
2234

35+
const update = useEvent((props: P, containerProps?: E) => {
36+
setComponentProps(props);
37+
setContainerProps(containerProps ?? null);
38+
});
39+
2340
const close = useEvent(() => {
2441
setIsOpen(false);
2542
});
2643

2744
// Render the dialog only when componentProps is set
28-
const rendered = useMemo(() => {
45+
const renderedDialog = useMemo(() => {
46+
if (!setupRef.current) {
47+
throw new Error(
48+
'useDialogContainer: DialogContainer must be rendered. Use `rendered` property to include it in your component tree.',
49+
);
50+
}
51+
2952
if (!componentProps) return null;
3053

3154
return (
32-
<DialogContainer isOpen={isOpen} onDismiss={close}>
55+
<DialogContainer
56+
isOpen={isOpen}
57+
onDismiss={close}
58+
{...(containerProps ?? {})}
59+
>
3360
<Component {...componentProps} />
3461
</DialogContainer>
3562
);
36-
}, [componentProps, isOpen]);
63+
}, [componentProps, containerProps, isOpen]);
64+
65+
return {
66+
open,
67+
update,
68+
close,
69+
get rendered() {
70+
setupRef.current = true;
3771

38-
return { open, close, rendered };
72+
return renderedDialog;
73+
},
74+
};
3975
}

0 commit comments

Comments
 (0)