From 8ed6a4b029be5235c799f2863b129a9457533e2c Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Sat, 15 Apr 2023 12:39:08 -0400 Subject: [PATCH] switch/case rendering --- example/app.tsx | 6 ++++ example/deep/component.tsx | 2 +- lib/iso.tsx | 56 +++++++++++++++++++++++++++++++++++++- server.tsx | 2 +- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/example/app.tsx b/example/app.tsx index 405429d..d6bdc70 100644 --- a/example/app.tsx +++ b/example/app.tsx @@ -13,6 +13,12 @@ export default ()=>

Title!!

subtitle

+ + + About Us + + lol/idk + ; }; diff --git a/example/deep/component.tsx b/example/deep/component.tsx index 5bed3d8..124ecfc 100644 --- a/example/deep/component.tsx +++ b/example/deep/component.tsx @@ -12,6 +12,6 @@ export default ()=> Route is: {routeGet.Path.toString()} Component!!! - a link + a link ; }; \ No newline at end of file diff --git a/lib/iso.tsx b/lib/iso.tsx index 8333a1f..fa6a91a 100644 --- a/lib/iso.tsx +++ b/lib/iso.tsx @@ -97,4 +97,58 @@ export const Router = { { return React.useContext(Router.Context); } -}; \ No newline at end of file +}; + +type SwitchContext = {depth:number, keys:Record}; +export const SwitchContext = React.createContext({depth:0, keys:{}} as SwitchContext); +export const Switch =({children}:{children:typeof React.Children})=> +{ + const contextSelection = React.useContext(SwitchContext); + const [contextRoute] = Router.Consumer(); + const routeSegment = contextRoute.Path.slice(contextSelection.depth); + const checkChild =(inChild:{props:{value?:string}})=> + { + if(inChild?.props?.value) + { + const parts = inChild.props.value.split("/"); + if(parts.length > routeSegment.length) + { + return false; + } + + const output:SwitchContext = {depth:contextSelection.depth+parts.length, keys:{}}; + for(let i=0; iRoute Rendred!{child} + } + } + return null; +}; +export const Case =({children, value}:{children:typeof React.Children, value?:string})=> +{ + return <>{children}; +}; +export const useRouteVars =()=> React.useContext(SwitchContext).keys; \ No newline at end of file diff --git a/server.tsx b/server.tsx index 06c98d5..e6613ff 100644 --- a/server.tsx +++ b/server.tsx @@ -35,7 +35,7 @@ const Transpileable =(inFilePath:string):boolean=> }; const Transpile =async(inCode:string, inKey:string):Promise=> { - const transpile = await ESBuild.transform(inCode, { loader: "tsx", sourcemap: "inline", minify:false }); + const transpile = await ESBuild.transform(inCode, { loader: "tsx", sourcemap: "inline", minify:false}); Transpiled.set(inKey, transpile.code); return transpile.code; };