legacyReactRouter.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * These are vendored from react-router v3
  3. *
  4. * Once we've fully migrated to react-router 6 we can drop these types
  5. */
  6. import type {
  7. Href,
  8. Location,
  9. LocationDescriptor,
  10. LocationState,
  11. Path,
  12. Pathname,
  13. Query,
  14. } from 'history';
  15. interface Params {
  16. [key: string]: string;
  17. }
  18. type RoutePattern = string;
  19. export type RouteComponent = React.ComponentClass<any> | React.FunctionComponent<any>;
  20. interface RouteComponents {
  21. [name: string]: RouteComponent;
  22. }
  23. interface RouterState<Q = any> {
  24. components: RouteComponent[];
  25. location: Location<Q>;
  26. params: Params;
  27. routes: PlainRoute[];
  28. }
  29. interface RedirectFunction {
  30. (location: LocationDescriptor): void;
  31. (state: LocationState, pathname: Pathname | Path, query?: Query): void;
  32. }
  33. type AnyFunction = (...args: any[]) => any;
  34. type EnterHook = (
  35. nextState: RouterState,
  36. replace: RedirectFunction,
  37. callback?: AnyFunction
  38. ) => any;
  39. type LeaveHook = (prevState: RouterState) => any;
  40. type ChangeHook = (
  41. prevState: RouterState,
  42. nextState: RouterState,
  43. replace: RedirectFunction,
  44. callback?: AnyFunction
  45. ) => any;
  46. type RouteHook = (nextLocation?: Location) => any;
  47. type ComponentCallback = (err: any, component: RouteComponent) => any;
  48. type ComponentsCallback = (err: any, components: RouteComponents) => any;
  49. export interface IndexRouteProps<Props = any> {
  50. component?: RouteComponent | undefined;
  51. components?: RouteComponents | undefined;
  52. getComponent?(nextState: RouterState, callback: ComponentCallback): void;
  53. getComponents?(nextState: RouterState, callback: ComponentsCallback): void;
  54. onChange?: ChangeHook | undefined;
  55. onEnter?: EnterHook | undefined;
  56. onLeave?: LeaveHook | undefined;
  57. props?: Props | undefined;
  58. }
  59. export interface RouteProps<Props = any> extends IndexRouteProps<Props> {
  60. children?: React.ReactNode;
  61. path?: RoutePattern | undefined;
  62. }
  63. type RouteCallback = (err: any, route: PlainRoute) => void;
  64. type RoutesCallback = (err: any, routesArray: PlainRoute[]) => void;
  65. export interface PlainRoute<Props = any> extends RouteProps<Props> {
  66. childRoutes?: PlainRoute[] | undefined;
  67. getChildRoutes?(partialNextState: LocationState, callback: RoutesCallback): void;
  68. getIndexRoute?(partialNextState: LocationState, callback: RouteCallback): void;
  69. indexRoute?: PlainRoute | undefined;
  70. }
  71. export interface RouteComponentProps<P, R, ComponentProps = any, Q = any> {
  72. location: Location<Q>;
  73. params: P & R;
  74. route: PlainRoute<ComponentProps>;
  75. routeParams: R;
  76. router: InjectedRouter;
  77. routes: PlainRoute[];
  78. }
  79. type LocationFunction = (location: LocationDescriptor) => void;
  80. type GoFunction = (n: number) => void;
  81. type NavigateFunction = () => void;
  82. type ActiveFunction = (location: LocationDescriptor, indexOnly?: boolean) => boolean;
  83. type LeaveHookFunction = (route: any, callback: RouteHook) => () => void;
  84. type CreatePartFunction<Part> = (pathOrLoc: LocationDescriptor, query?: any) => Part;
  85. export interface InjectedRouter<P = Record<string, string>, Q = any> {
  86. createHref: CreatePartFunction<Href>;
  87. createPath: CreatePartFunction<Path>;
  88. go: GoFunction;
  89. goBack: NavigateFunction;
  90. goForward: NavigateFunction;
  91. isActive: ActiveFunction;
  92. location: Location<Q>;
  93. params: P;
  94. push: LocationFunction;
  95. replace: LocationFunction;
  96. routes: PlainRoute[];
  97. setRouteLeaveHook: LeaveHookFunction;
  98. }
  99. export interface WithRouterProps<P = Record<string, string>, Q = any> {
  100. location: Location<Q>;
  101. params: P;
  102. router: InjectedRouter<P, Q>;
  103. routes: PlainRoute[];
  104. }
  105. export interface RouteContextInterface<P = Record<string, string>, Q = any> {
  106. location: Location<Q>;
  107. params: P;
  108. router: InjectedRouter<P, Q>;
  109. routes: PlainRoute[];
  110. }
  111. export type Route = React.ComponentClass<RouteProps>;
  112. export interface IndexRedirectProps {
  113. to: RoutePattern;
  114. query?: Query | undefined;
  115. }
  116. export interface RedirectProps extends IndexRedirectProps {
  117. from: RoutePattern;
  118. }