react-router-decorator
Version:
基于 react-router-dom 封装的路由工具,提供装饰器/函数模式设置路由,自动排序,支持嵌套路由
87 lines (86 loc) • 2.37 kB
TypeScript
import React from 'react';
import { NavigateFunction, RouteObject } from 'react-router-dom';
/**
* 组件上可设置属性用于开启或关闭 `withPageWrapper` `childrenAsOutlet` 优先级最高
*/
export interface Extra {
withPageWrapper?: boolean;
childrenAsOutlet?: boolean;
}
/**
* 支持的其他的 RouteObject
*/
export type PickRouteObject = Omit<RouteObject, 'Component' | 'path' | 'children'>;
/**
* 页面级组件
*/
export type PageComponent<P = any> = React.ComponentType<P> & Extra;
/**
* 解析 url search 参数
*/
export type Query = {
readonly [key: string]: undefined | string | string[] | Query | Query[];
};
/**
* 路由参数
*/
export type Params = {
readonly [key: string]: string | undefined;
};
/**
* 注册页面路由时额外的参数
*/
export interface PageDefine<P = Params, Q = Query> {
title?: string | ((params: P, query: Q) => string);
context?: string;
lazy?: boolean;
mpa?: string;
}
/**
* 被默认 PageWrapper 包裹的页面自动解析的参数
*/
export interface WithWrappedProps<P = Params, Q = Query> {
query: Q;
params: P;
navigate: NavigateFunction;
path: string;
children?: React.ReactNode;
}
export interface PageWrapperProps<P = any, PA = Params, Q = Query> extends PageDefine<PA, Q> {
path: string;
Component: PageComponent<P>;
childrenAsOutlet?: boolean;
}
export type PageWrapperType<P = any, PA = Params, Q = Query> = React.ComponentType<PageWrapperProps<P, PA, Q>>;
export type RouteOption<P = any, PA = Params, Q = Query> = Omit<PageWrapperProps<P, PA, Q>, 'childrenAsOutlet'> & PickRouteObject;
export type PageOptions<P = Params, Q = Query> = (PageDefine<P, Q> & PickRouteObject) | string;
export interface RenderOptions<WP = any, P = any, PA = Params, Q = Query> {
/**
* 路由类型
*/
type?: 'hash' | 'history';
/**
* App Wrapper
*/
Wrapper?: React.ComponentType<WP>;
/**
* 使用默认 Page Wrapper
*/
withPageWrapper?: boolean;
/**
* 自定义 Page Wrapper
*/
PageWrapper?: PageWrapperType<P, PA, Q>;
/**
* 使用 children 代替 Outlet
*/
childrenAsOutlet?: boolean;
/**
* 开启调试模式,控制台输出路由信息
*/
debug?: boolean;
/**
* 开启路由辅助工具
*/
helper?: boolean;
}