-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcreate-route-view.tsx
More file actions
34 lines (26 loc) · 941 Bytes
/
create-route-view.tsx
File metadata and controls
34 lines (26 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import React from "react";
import { RouteInstance, RouteParams } from "atomic-router";
import { useIsOpened } from "./use-is-opened";
export interface RouteViewConfig<Props, Params extends RouteParams> {
route: RouteInstance<Params> | RouteInstance<Params>[];
view: React.ComponentType<Props>;
otherwise?: React.ComponentType<Props>;
}
export function createRouteView<
Props,
Params extends RouteParams
>(config: RouteViewConfig<Props, Params>) {
return (props: Props & Omit<RouteViewConfig<Props, Params>, keyof typeof config>) => {
const mergedConfig = { ...config, ...props } as RouteViewConfig<Props, Params>;
const isOpened = useIsOpened(mergedConfig.route);
if (isOpened) {
const View = mergedConfig.view;
return <View {...props} />;
}
if (mergedConfig.otherwise) {
const Otherwise = mergedConfig.otherwise;
return <Otherwise {...props} />;
}
return null;
};
}