legacyReactRouter.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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<
  72. P = Record<string, string | undefined>,
  73. R = Record<string, string | undefined>,
  74. ComponentProps = any,
  75. Q = any,
  76. > {
  77. location: Location<Q>;
  78. params: P;
  79. route: PlainRoute<ComponentProps>;
  80. routeParams: R;
  81. router: InjectedRouter;
  82. routes: PlainRoute[];
  83. }
  84. type LocationFunction = (location: LocationDescriptor) => void;
  85. type GoFunction = (n: number) => void;
  86. type NavigateFunction = () => void;
  87. type ActiveFunction = (location: LocationDescriptor, indexOnly?: boolean) => boolean;
  88. type LeaveHookFunction = (route: any, callback: RouteHook) => () => void;
  89. type CreatePartFunction<Part> = (pathOrLoc: LocationDescriptor, query?: any) => Part;
  90. export interface InjectedRouter<P = Record<string, string | undefined>, Q = any> {
  91. createHref: CreatePartFunction<Href>;
  92. createPath: CreatePartFunction<Path>;
  93. go: GoFunction;
  94. goBack: NavigateFunction;
  95. goForward: NavigateFunction;
  96. isActive: ActiveFunction;
  97. location: Location<Q>;
  98. params: P;
  99. push: LocationFunction;
  100. replace: LocationFunction;
  101. routes: PlainRoute[];
  102. setRouteLeaveHook: LeaveHookFunction;
  103. }
  104. export interface WithRouterProps<P = Record<string, string | undefined>, Q = any> {
  105. location: Location<Q>;
  106. params: P;
  107. router: InjectedRouter<P, Q>;
  108. routes: PlainRoute[];
  109. }
  110. export interface RouteContextInterface<P = Record<string, string | undefined>, Q = any> {
  111. location: Location<Q>;
  112. params: P;
  113. router: InjectedRouter<P, Q>;
  114. routes: PlainRoute[];
  115. }
  116. export type Route = React.ComponentClass<RouteProps>;
  117. export interface IndexRedirectProps {
  118. to: RoutePattern;
  119. query?: Query | undefined;
  120. }
  121. export interface RedirectProps extends IndexRedirectProps {
  122. from: RoutePattern;
  123. }