UNPKG

5.47 kBTypeScriptView Raw
1import * as React from "react";
2
3export interface HLocation<S = unknown> {
4 pathname: string;
5 search: string;
6 state: S;
7 hash: string;
8 key?: string | undefined;
9}
10export type WindowLocation<S = unknown> = Window["location"] & HLocation<S>;
11
12export type HistoryActionType = "PUSH" | "POP";
13export type HistoryLocation = WindowLocation & { state?: any };
14export interface HistoryListenerParameter {
15 location: HistoryLocation;
16 action: HistoryActionType;
17}
18export type HistoryListener = (parameter: HistoryListenerParameter) => void;
19export type HistoryUnsubscribe = () => void;
20
21export interface History {
22 readonly location: HistoryLocation;
23 readonly transitioning: boolean;
24 listen: (listener: HistoryListener) => HistoryUnsubscribe;
25 navigate: NavigateFn;
26}
27
28export class Router extends React.Component<RouterProps & React.HTMLProps<HTMLDivElement>> {}
29
30export interface RouterProps {
31 basepath?: string | undefined;
32 primary?: boolean | undefined;
33 location?: WindowLocation | undefined;
34 component?: React.ComponentType | string | undefined;
35}
36
37export type RouteComponentProps<TParams = {}> = Partial<TParams> & {
38 path?: string | undefined;
39 default?: boolean | undefined;
40 location?: WindowLocation | undefined;
41 navigate?: NavigateFn | undefined;
42 uri?: string | undefined;
43};
44
45export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
46
47export type AnchorProps = Omit<
48 React.DetailedHTMLProps<React.AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>,
49 "href" // remove href, as it's ignored by the router
50>;
51
52export interface LinkProps<TState> extends AnchorProps {
53 to: string;
54 replace?: boolean | undefined;
55 getProps?: ((props: LinkGetProps) => {}) | undefined;
56 state?: TState | undefined;
57 /** @deprecated If using React >= 16.4, use ref instead. */
58 innerRef?: React.Ref<HTMLAnchorElement> | undefined;
59}
60
61export interface LinkGetProps {
62 isCurrent: boolean;
63 isPartiallyCurrent: boolean;
64 href: string;
65 location: WindowLocation;
66}
67
68export function Link<TState>(
69 // TODO: Define this as ...params: Parameters<Link<TState>> when only TypeScript >= 3.1 support is needed.
70 props: React.PropsWithoutRef<LinkProps<TState>> & React.RefAttributes<HTMLAnchorElement>,
71): ReturnType<Link<TState>>;
72export interface Link<TState> extends
73 React.ForwardRefExoticComponent<
74 React.PropsWithoutRef<LinkProps<TState>> & React.RefAttributes<HTMLAnchorElement>
75 >
76{}
77
78export interface RedirectProps<TState> {
79 from?: string | undefined;
80 to: string;
81 noThrow?: boolean | undefined;
82 state?: TState | undefined;
83 replace?: boolean | undefined;
84}
85
86export class Redirect<TState> extends React.Component<RouteComponentProps<RedirectProps<TState>>> {}
87
88export interface MatchProps<TParams> {
89 path: string;
90 children: MatchRenderFn<TParams>;
91}
92
93export type MatchRenderFn<TParams> = (props: MatchRenderProps<TParams>) => React.ReactNode;
94
95export interface MatchRenderProps<TParams> {
96 match: null | ({ uri: string; path: string } & TParams);
97 location: WindowLocation;
98 navigate: NavigateFn;
99}
100
101export class Match<TParams> extends React.Component<MatchProps<TParams>> {}
102
103export interface NavigateFn {
104 (to: string, options?: NavigateOptions<{}>): Promise<void>;
105 (to: number, options?: undefined): Promise<void>;
106}
107
108export interface NavigateOptions<TState> {
109 state?: TState | undefined;
110 replace?: boolean | undefined;
111}
112
113export interface LocationProps {
114 children: LocationProviderRenderFn;
115}
116
117export class Location extends React.Component<LocationProps> {}
118
119export interface LocationProviderProps {
120 history?: History | undefined;
121 children?: React.ReactNode | LocationProviderRenderFn | undefined;
122}
123
124export type LocationProviderRenderFn = (context: LocationContext) => React.ReactNode;
125
126export interface LocationContext {
127 location: WindowLocation;
128 navigate: NavigateFn;
129}
130
131export class LocationProvider extends React.Component<LocationProviderProps> {}
132
133export interface ServerLocationProps {
134 url: string;
135}
136
137export class ServerLocation extends React.Component<ServerLocationProps> {}
138
139export const navigate: NavigateFn;
140
141export interface HistorySource {
142 readonly location: WindowLocation;
143 addEventListener(name: string, listener: (event: Event) => void): void;
144 removeEventListener(name: string, listener: (event: Event) => void): void;
145 history: {
146 readonly state: any;
147 pushState(state: any, title: string, uri: string): void;
148 replaceState(state: any, title: string, uri: string): void;
149 };
150}
151
152export function createHistory(source: HistorySource): History;
153
154export function createMemorySource(initialPath: string): HistorySource;
155
156export interface RedirectRequest {
157 uri: string;
158}
159
160export function isRedirect(error: any): error is RedirectRequest;
161
162export function redirectTo(uri: string): void;
163
164export const globalHistory: History;
165
166export function useLocation(): WindowLocation;
167
168export function useNavigate(): NavigateFn;
169
170// TODO: In the next major release update we should update the type parameter default of TPrams from `any` to `{}`,
171// it is currently being keep for backwards compatibility with version 1.3.7 or below.
172export function useParams<TParams extends { [Param in keyof TParams]?: string } = any>(): TParams;
173
174export function useMatch(pathname: string): null | { uri: string; path: string; [param: string]: string };