From f949f3e1919dec00c62555edee658ca9ef1064cd Mon Sep 17 00:00:00 2001 From: Madeorsk Date: Sun, 14 Jul 2024 16:01:50 +0200 Subject: [PATCH] Allow to pass generic variables to callable curtains / subapps and improve type check. --- src/Components/Curtains/Curtains.tsx | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Components/Curtains/Curtains.tsx b/src/Components/Curtains/Curtains.tsx index 12d6076..fb9eaf0 100644 --- a/src/Components/Curtains/Curtains.tsx +++ b/src/Components/Curtains/Curtains.tsx @@ -50,21 +50,38 @@ export function useCurtains(): CurtainsContextState return useContext(CurtainsContext); } + + /** - * Curtain callable. + * A generic callable curtain element. + */ +export type CallableCurtainElement = (...args: any) => React.ReactNode; + +/** + * A callable curtain function with typed parameters. + */ +export type CallableCurtain = (...args: Parameters) => CurtainUuidType; + +/** + * Callable curtain function generator. * @param curtains The curtains context state. * @param curtainElement The curtain element to open when called. */ -export function callableCurtain(curtains: CurtainsContextState, curtainElement: React.ReactNode): () => CurtainUuidType +export function callableCurtain(curtains: CurtainsContextState, curtainElement: React.ReactNode|F): CallableCurtain { - return () => curtains.open(curtainElement); + if (typeof curtainElement == "function") + // It's a callable curtain element, the callable curtain should be called with the same parameters. + return (...args: Parameters) => curtains.open(curtainElement(...args)); + else + // It's a simple element, just open it. + return () => curtains.open(curtainElement); } /** * Hook to create a simple curtain. * @param curtainElement Content of the curtain to open. */ -export function useCallableCurtain(curtainElement: React.ReactNode): () => CurtainUuidType +export function useCallableCurtain(curtainElement: React.ReactNode|F): CallableCurtain { // Get curtains context state. const curtains = useCurtains();