Skip to content

Allow to pass layout to otherwise for createRoutersView #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/create-routes-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface RouteRecord<Props, Params extends RouteParams> {

export interface RoutesViewConfig {
routes: RouteRecord<any, any>[];
otherwise?: React.ComponentType<any>;
otherwise?: React.ComponentType<any> | Omit<RouteRecord<any, any>, 'route'>;
}

export function createRoutesView<Config extends RoutesViewConfig>(config: Config) {
Expand All @@ -29,9 +29,9 @@ export function createRoutesView<Config extends RoutesViewConfig>(config: Config
if (route.layout) {
const Layout = route.layout;
return (
<Layout>
<View />
</Layout>
<Layout>
<View />
</Layout>
);
}

Expand All @@ -40,11 +40,28 @@ export function createRoutesView<Config extends RoutesViewConfig>(config: Config
}

if (mergedConfig.otherwise) {
const Otherwise = mergedConfig.otherwise;
const otherwise = mergedConfig.otherwise;
if (isComponent(otherwise)) {
const Otherwise = otherwise;
return <Otherwise />;
}

return <Otherwise />;
const View = otherwise.view;
if (otherwise.layout) {
const Layout = otherwise.layout;
return (
<Layout>
<View />
</Layout>
)
}
return <View />
}

return null;
};
}

function isComponent(obj: unknown): obj is React.ComponentType<any> {
return typeof obj === 'function';
}