Allow to pass generic variables to callable curtains / subapps and improve type check.
This commit is contained in:
parent
2b4965a2ee
commit
f949f3e191
1 changed files with 21 additions and 4 deletions
|
@ -50,13 +50,30 @@ export function useCurtains(): CurtainsContextState
|
||||||
return useContext(CurtainsContext);
|
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<F extends CallableCurtainElement> = (...args: Parameters<F>) => CurtainUuidType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callable curtain function generator.
|
||||||
* @param curtains The curtains context state.
|
* @param curtains The curtains context state.
|
||||||
* @param curtainElement The curtain element to open when called.
|
* @param curtainElement The curtain element to open when called.
|
||||||
*/
|
*/
|
||||||
export function callableCurtain(curtains: CurtainsContextState, curtainElement: React.ReactNode): () => CurtainUuidType
|
export function callableCurtain<F extends CallableCurtainElement>(curtains: CurtainsContextState, curtainElement: React.ReactNode|F): CallableCurtain<F>
|
||||||
{
|
{
|
||||||
|
if (typeof curtainElement == "function")
|
||||||
|
// It's a callable curtain element, the callable curtain should be called with the same parameters.
|
||||||
|
return (...args: Parameters<F>) => curtains.open(curtainElement(...args));
|
||||||
|
else
|
||||||
|
// It's a simple element, just open it.
|
||||||
return () => curtains.open(curtainElement);
|
return () => curtains.open(curtainElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +81,7 @@ export function callableCurtain(curtains: CurtainsContextState, curtainElement:
|
||||||
* Hook to create a simple curtain.
|
* Hook to create a simple curtain.
|
||||||
* @param curtainElement Content of the curtain to open.
|
* @param curtainElement Content of the curtain to open.
|
||||||
*/
|
*/
|
||||||
export function useCallableCurtain(curtainElement: React.ReactNode): () => CurtainUuidType
|
export function useCallableCurtain<F extends CallableCurtainElement>(curtainElement: React.ReactNode|F): CallableCurtain<F>
|
||||||
{
|
{
|
||||||
// Get curtains context state.
|
// Get curtains context state.
|
||||||
const curtains = useCurtains();
|
const curtains = useCurtains();
|
||||||
|
|
Loading…
Reference in a new issue