tachybase_todo/packages/client/src/route-switch/RouteSwitch.tsx
chenos a87a089acf
feat: client v0.6 (#150)
* v0.6

* update...

* feat: improve code

* improve code

* action & form

* update...

* improve code

* improve code

* improve code

* designable

* update

* update...

* api client

* RecordProvider

* collection manager

* update...

* update api client

* update use request

* update

* update doc

* test cases for compose

* docs: improve documentation
2022-01-10 19:22:21 +08:00

60 lines
1.6 KiB
TypeScript

import React from 'react';
import { Redirect, Route, Switch } from 'react-router-dom';
import { RouteContext } from './context';
import { useRouteComponent } from './hooks';
import { RouteSwitchProps } from './types';
export function RouteSwitch(props: RouteSwitchProps) {
const { routes = [] } = props;
if (!routes.length) {
return null;
}
return (
<Switch>
{routes.map((route, index) => {
if (route.type == 'redirect') {
return (
<Redirect
key={index}
to={route.to}
push={route.push}
from={route.from}
path={route.path}
exact={route.exact}
strict={route.strict}
/>
);
}
if (!route.path && Array.isArray(route.routes)) {
route.path = route.routes.map((r) => r.path) as any;
}
return (
<Route
key={index}
path={route.path}
exact={route.exact}
strict={route.strict}
sensitive={route.sensitive}
render={(props) => {
return (
<RouteContext.Provider value={route}>
<ComponentRenderer {...props} route={route} />
</RouteContext.Provider>
);
}}
/>
);
})}
</Switch>
);
}
function ComponentRenderer(props) {
const Component = useRouteComponent(props?.route?.component);
return (
<Component {...props}>
<RouteSwitch routes={props.route.routes} />
</Component>
);
}