forked from reach/router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
209 lines (168 loc) · 5.53 KB
/
index.d.ts
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* eslint-disable react/no-multi-comp */
// Type definitions for @reach/router 1.3
// Project: https://github.com/reach/router
// Definitions by: Kingdaro <https://github.com/kingdaro>,
// A.Mokhtar <https://github.com/xMokAx>,
// Awwit <https://github.com/awwit>
// wroughtec <https://github.com/wroughtec>
// O.Jackman <https://github.com/chilledoj>
// Eyas <https://github.com/Eyas>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
import type React from "react";
type Dict<T = unknown> = Record<string, T>;
export interface HLocation<S = unknown> {
pathname: string;
search: string;
state: S;
hash: string;
key?: string;
}
export type WindowLocation<S = unknown> = Window["location"] & HLocation<S>;
export type HistoryActionType = "PUSH" | "POP";
export type HistoryLocation<S = any> = Omit<
WindowLocation & { state?: S },
"ancestorOrigins"
>;
export interface HistoryListenerParameter {
location: HistoryLocation;
action: HistoryActionType;
}
export type HistoryListener = (parameter: HistoryListenerParameter) => void;
export type HistoryUnsubscribe = () => void;
export interface History {
readonly location: HistoryLocation;
readonly transitioning: boolean;
listen: (listener: HistoryListener) => HistoryUnsubscribe;
navigate: NavigateFn;
}
export class Router extends React.Component<
RouterProps & React.HTMLProps<HTMLDivElement>
> {}
export interface RouterProps {
basepath?: string;
primary?: boolean;
location?: WindowLocation;
component?: React.ComponentType | string;
}
export type RouteComponentProps<TParams = Dict, S = any> = TParams & {
path?: string;
default?: boolean;
location?: WindowLocation<S>;
navigate?: NavigateFn;
uri?: string;
};
export type AnchorProps = Omit<
React.DetailedHTMLProps<
React.AnchorHTMLAttributes<HTMLAnchorElement>,
HTMLAnchorElement
>,
"href" // remove href, as it's ignored by the router
>;
export interface LinkProps<TState> extends AnchorProps {
to: string;
replace?: boolean;
getProps?: (props: LinkGetProps) => Dict;
state?: TState;
}
export interface LinkGetProps {
isCurrent: boolean;
isPartiallyCurrent: boolean;
href: string;
location: WindowLocation;
}
export function Link<TState>(
// TODO: Define this as ...params: Parameters<Link<TState>> when only TypeScript >= 3.1 support is needed.
// props: React.PropsWithoutRef<LinkProps<TState>> & React.RefAttributes<HTMLAnchorElement>
...params: Parameters<Link<TState>>
): ReturnType<Link<TState>>;
export interface Link<TState>
extends React.ForwardRefExoticComponent<
React.PropsWithoutRef<LinkProps<TState>> &
React.RefAttributes<HTMLAnchorElement>
> {}
export interface RedirectProps<TState> {
from?: string;
to: string;
noThrow?: boolean;
state?: TState;
replace?: boolean;
}
export class Redirect<TState> extends React.Component<
RouteComponentProps<RedirectProps<TState>>
> {}
export interface MatchProps<TParams> {
path: string;
children: MatchRenderFn<TParams>;
}
export type MatchRenderFn<TParams> = (
props: MatchRenderProps<TParams>
) => React.ReactNode;
export interface MatchRenderProps<TParams> {
match: null | ({ uri: string; path: string } & TParams);
location: WindowLocation;
navigate: NavigateFn;
}
export class Match<TParams> extends React.Component<MatchProps<TParams>> {}
export interface NavigateFn {
(to: string, options?: NavigateOptions<Dict>): Promise<void>;
(to: number): Promise<void>;
}
export interface NavigateOptions<TState> {
state?: TState;
replace?: boolean;
}
export interface LocationProps {
children: LocationProviderRenderFn;
}
export class Location extends React.Component<LocationProps> {}
export interface LocationProviderProps {
history?: History;
children?: React.ReactNode | LocationProviderRenderFn;
}
export type LocationProviderRenderFn = (
context: LocationContext
) => React.ReactNode;
export interface LocationContext {
location: WindowLocation;
navigate: NavigateFn;
}
export class LocationProvider extends React.Component<LocationProviderProps> {}
export interface ServerLocationProps {
url: string;
}
export class ServerLocation extends React.Component<ServerLocationProps> {}
export const navigate: NavigateFn;
export interface HistorySource {
readonly location: WindowLocation;
addEventListener(name: string, listener: (event: Event) => void): void;
removeEventListener(name: string, listener: (event: Event) => void): void;
history: {
readonly state: any;
pushState(state: any, title: string, uri: string): void;
replaceState(state: any, title: string, uri: string): void;
};
}
/**
* Creates a history object that enables you to listen for location changes.
* You don’t typically need this outside of some types of testing.
*/
export function createHistory(source: HistorySource): History;
/**
* Creates a source for createHistory that manages a history stack in memory.
* Mostly for testing.
* @param initialPath The initial path of the history.
*/
export function createMemorySource(initialPath?: string): HistorySource;
export interface RedirectRequest {
uri: string;
}
export function isRedirect(error: any): error is RedirectRequest;
export function redirectTo(uri: string): void;
export const globalHistory: History;
export function useLocation(): WindowLocation;
export function useNavigate(): NavigateFn;
export function useParams(): any;
export function useMatch(
pathname: string
): null | { uri: string; path: string; [param: string]: string };