@tanstack/react-router 0.0.1-beta.234 → 0.0.1-beta.235
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/Matches.js +18 -32
- package/build/cjs/Matches.js.map +1 -1
- package/build/cjs/RouterProvider.js +0 -8
- package/build/cjs/RouterProvider.js.map +1 -1
- package/build/cjs/index.js +0 -1
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/route.js +7 -13
- package/build/cjs/route.js.map +1 -1
- package/build/cjs/useParams.js +2 -7
- package/build/cjs/useParams.js.map +1 -1
- package/build/cjs/useSearch.js +1 -6
- package/build/cjs/useSearch.js.map +1 -1
- package/build/cjs/utils.js +1 -4
- package/build/cjs/utils.js.map +1 -1
- package/build/esm/index.js +30 -70
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +291 -291
- package/build/types/Matches.d.ts +3 -9
- package/build/types/RouterProvider.d.ts +0 -3
- package/build/types/route.d.ts +10 -30
- package/build/types/useParams.d.ts +1 -3
- package/build/types/useSearch.d.ts +1 -3
- package/build/types/utils.d.ts +1 -3
- package/build/umd/index.development.js +29 -70
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/Matches.tsx +37 -59
- package/src/RouterProvider.tsx +0 -10
- package/src/route.ts +15 -37
- package/src/useParams.tsx +4 -14
- package/src/useSearch.tsx +3 -11
- package/src/utils.ts +12 -20
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-router",
|
|
3
3
|
"author": "Tanner Linsley",
|
|
4
|
-
"version": "0.0.1-beta.
|
|
4
|
+
"version": "0.0.1-beta.235",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "tanstack/router",
|
|
7
7
|
"homepage": "https://tanstack.com/router",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"@babel/runtime": "^7.16.7",
|
|
43
43
|
"tiny-invariant": "^1.3.1",
|
|
44
44
|
"tiny-warning": "^1.0.3",
|
|
45
|
-
"@tanstack/history": "0.0.1-beta.
|
|
45
|
+
"@tanstack/history": "0.0.1-beta.235"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "rollup --config rollup.config.js"
|
package/src/Matches.tsx
CHANGED
|
@@ -2,7 +2,7 @@ import * as React from 'react'
|
|
|
2
2
|
import invariant from 'tiny-invariant'
|
|
3
3
|
import warning from 'tiny-warning'
|
|
4
4
|
import { CatchBoundary, ErrorComponent } from './CatchBoundary'
|
|
5
|
-
import { useRouter
|
|
5
|
+
import { useRouter } from './RouterProvider'
|
|
6
6
|
import { ResolveRelativePath, ToOptions } from './link'
|
|
7
7
|
import { AnyRoute, ReactNode, rootRouteId } from './route'
|
|
8
8
|
import {
|
|
@@ -47,12 +47,10 @@ export interface RouteMatch<
|
|
|
47
47
|
export type AnyRouteMatch = RouteMatch<any>
|
|
48
48
|
|
|
49
49
|
export function Matches() {
|
|
50
|
-
const
|
|
51
|
-
const { matches } = state
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
const route = routesById[rootRouteId]!
|
|
50
|
+
const router = useRouter()
|
|
51
|
+
const { matches } = router.state
|
|
52
|
+
const locationKey = router.state.location.state.key
|
|
53
|
+
const route = router.routesById[rootRouteId]!
|
|
56
54
|
|
|
57
55
|
const errorComponent = React.useCallback(
|
|
58
56
|
(props: any) => {
|
|
@@ -296,24 +294,20 @@ export function useMatch<
|
|
|
296
294
|
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
|
|
297
295
|
TStrict extends boolean = true,
|
|
298
296
|
TRouteMatchState = RouteMatch<TRouteTree, TFrom>,
|
|
299
|
-
TSelected = TRouteMatchState,
|
|
300
297
|
>(
|
|
301
|
-
opts: StrictOrFrom<TFrom
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
): TStrict extends true ? TSelected : TSelected | undefined {
|
|
298
|
+
opts: StrictOrFrom<TFrom>,
|
|
299
|
+
): TStrict extends true ? TRouteMatchState : TRouteMatchState | undefined {
|
|
300
|
+
const router = useRouter()
|
|
305
301
|
const nearestMatch = React.useContext(matchesContext)[0]!
|
|
306
302
|
const nearestMatchRouteId = nearestMatch?.routeId
|
|
307
303
|
|
|
308
|
-
const matchRouteId =
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
: state.matches.find((d) => d.id === nearestMatch.id)
|
|
304
|
+
const matchRouteId = (() => {
|
|
305
|
+
const match = opts?.from
|
|
306
|
+
? router.state.matches.find((d) => d.routeId === opts?.from)
|
|
307
|
+
: router.state.matches.find((d) => d.id === nearestMatch.id)
|
|
313
308
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
})
|
|
309
|
+
return match!.routeId
|
|
310
|
+
})()
|
|
317
311
|
|
|
318
312
|
if (opts?.strict ?? true) {
|
|
319
313
|
invariant(
|
|
@@ -328,43 +322,32 @@ export function useMatch<
|
|
|
328
322
|
)
|
|
329
323
|
}
|
|
330
324
|
|
|
331
|
-
const matchSelection =
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
: state.matches.find((d) => d.id === nearestMatch.id)
|
|
336
|
-
|
|
337
|
-
invariant(
|
|
338
|
-
match,
|
|
339
|
-
`Could not find ${
|
|
340
|
-
opts?.from
|
|
341
|
-
? `an active match from "${opts.from}"`
|
|
342
|
-
: 'a nearest match!'
|
|
343
|
-
}`,
|
|
344
|
-
)
|
|
325
|
+
const matchSelection = (() => {
|
|
326
|
+
const match = opts?.from
|
|
327
|
+
? router.state.matches.find((d) => d.routeId === opts?.from)
|
|
328
|
+
: router.state.matches.find((d) => d.id === nearestMatch.id)
|
|
345
329
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
330
|
+
invariant(
|
|
331
|
+
match,
|
|
332
|
+
`Could not find ${
|
|
333
|
+
opts?.from ? `an active match from "${opts.from}"` : 'a nearest match!'
|
|
334
|
+
}`,
|
|
335
|
+
)
|
|
336
|
+
|
|
337
|
+
return match
|
|
338
|
+
})()
|
|
349
339
|
|
|
350
340
|
return matchSelection as any
|
|
351
341
|
}
|
|
352
342
|
|
|
353
343
|
export const matchesContext = React.createContext<RouteMatch[]>(null!)
|
|
354
344
|
|
|
355
|
-
export function useMatches
|
|
356
|
-
|
|
357
|
-
}): T {
|
|
345
|
+
export function useMatches(): RouteMatch[] {
|
|
346
|
+
const router = useRouter()
|
|
358
347
|
const contextMatches = React.useContext(matchesContext)
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
const matches = state.matches.slice(
|
|
363
|
-
state.matches.findIndex((d) => d.id === contextMatches[0]?.id),
|
|
364
|
-
)
|
|
365
|
-
return opts?.select ? opts.select(matches) : (matches as T)
|
|
366
|
-
},
|
|
367
|
-
})
|
|
348
|
+
return router.state.matches.slice(
|
|
349
|
+
router.state.matches.findIndex((d) => d.id === contextMatches[0]?.id),
|
|
350
|
+
)
|
|
368
351
|
}
|
|
369
352
|
|
|
370
353
|
export function useLoaderData<
|
|
@@ -375,15 +358,10 @@ export function useLoaderData<
|
|
|
375
358
|
TRouteTree,
|
|
376
359
|
TFrom
|
|
377
360
|
>,
|
|
378
|
-
TSelected = TRouteMatch['loaderData'],
|
|
379
361
|
>(
|
|
380
|
-
opts: StrictOrFrom<TFrom
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
return typeof opts.select === 'function'
|
|
387
|
-
? opts.select(match?.loaderData)
|
|
388
|
-
: match?.loaderData
|
|
362
|
+
opts: StrictOrFrom<TFrom>,
|
|
363
|
+
): TStrict extends true
|
|
364
|
+
? TRouteMatch['loaderData']
|
|
365
|
+
: TRouteMatch['loaderData'] | undefined {
|
|
366
|
+
return useMatch(opts)?.loaderData
|
|
389
367
|
}
|
package/src/RouterProvider.tsx
CHANGED
|
@@ -189,16 +189,6 @@ export function getRouteMatch<TRouteTree extends AnyRoute>(
|
|
|
189
189
|
return [...state.pendingMatches, ...state.matches].find((d) => d.id === id)
|
|
190
190
|
}
|
|
191
191
|
|
|
192
|
-
export function useRouterState<
|
|
193
|
-
TSelected = RouterState<RegisteredRouter['routeTree']>,
|
|
194
|
-
>(opts?: {
|
|
195
|
-
select: (state: RouterState<RegisteredRouter['routeTree']>) => TSelected
|
|
196
|
-
}): TSelected {
|
|
197
|
-
const { state } = useRouter()
|
|
198
|
-
// return useStore(router.__store, opts?.select as any)
|
|
199
|
-
return opts?.select ? opts.select(state) : (state as any)
|
|
200
|
-
}
|
|
201
|
-
|
|
202
192
|
export type RouterProps<
|
|
203
193
|
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
204
194
|
TDehydrated extends Record<string, any> = Record<string, any>,
|
package/src/route.ts
CHANGED
|
@@ -590,34 +590,22 @@ export class Route<
|
|
|
590
590
|
// replaced by a framework specific implementation if necessary
|
|
591
591
|
}
|
|
592
592
|
|
|
593
|
-
useMatch =
|
|
594
|
-
|
|
595
|
-
}): TSelected => {
|
|
596
|
-
return useMatch({ ...opts, from: this.id }) as any
|
|
593
|
+
useMatch = (): TAllContext => {
|
|
594
|
+
return useMatch({ from: this.id }) as any
|
|
597
595
|
}
|
|
598
|
-
useRouteContext =
|
|
599
|
-
select?: (search: TAllContext) => TSelected
|
|
600
|
-
}): TSelected => {
|
|
596
|
+
useRouteContext = (): TAllContext => {
|
|
601
597
|
return useMatch({
|
|
602
|
-
...opts,
|
|
603
598
|
from: this.id,
|
|
604
|
-
|
|
605
|
-
} as any)
|
|
599
|
+
} as any).context
|
|
606
600
|
}
|
|
607
|
-
useSearch =
|
|
608
|
-
|
|
609
|
-
}): TSelected => {
|
|
610
|
-
return useSearch({ ...opts, from: this.id } as any)
|
|
601
|
+
useSearch = (): TFullSearchSchema => {
|
|
602
|
+
return useSearch({ from: this.id } as any)
|
|
611
603
|
}
|
|
612
|
-
useParams =
|
|
613
|
-
|
|
614
|
-
}): TSelected => {
|
|
615
|
-
return useParams({ ...opts, from: this.id } as any)
|
|
604
|
+
useParams = (): TAllParams => {
|
|
605
|
+
return useParams({ from: this.id } as any)
|
|
616
606
|
}
|
|
617
|
-
useLoaderData =
|
|
618
|
-
|
|
619
|
-
}): TSelected => {
|
|
620
|
-
return useLoaderData({ ...opts, from: this.id } as any) as any
|
|
607
|
+
useLoaderData = (): TLoaderData => {
|
|
608
|
+
return useLoaderData({ from: this.id } as any) as any
|
|
621
609
|
}
|
|
622
610
|
}
|
|
623
611
|
|
|
@@ -768,21 +756,11 @@ export type RouteProps<
|
|
|
768
756
|
TAllContext extends Record<string, any> = AnyContext,
|
|
769
757
|
TLoaderData extends any = unknown,
|
|
770
758
|
> = {
|
|
771
|
-
useMatch:
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
}) => TSelected
|
|
777
|
-
useSearch: <TSelected = TFullSearchSchema>(opts?: {
|
|
778
|
-
select?: (search: TFullSearchSchema) => TSelected
|
|
779
|
-
}) => TSelected
|
|
780
|
-
useParams: <TSelected = TAllParams>(opts?: {
|
|
781
|
-
select?: (search: TAllParams) => TSelected
|
|
782
|
-
}) => TSelected
|
|
783
|
-
useLoaderData: <TSelected = TLoaderData>(opts?: {
|
|
784
|
-
select?: (search: TLoaderData) => TSelected
|
|
785
|
-
}) => TSelected
|
|
759
|
+
useMatch: () => TAllContext
|
|
760
|
+
useRouteContext: () => TAllContext
|
|
761
|
+
useSearch: () => TFullSearchSchema
|
|
762
|
+
useParams: () => TAllParams
|
|
763
|
+
useLoaderData: () => TLoaderData
|
|
786
764
|
}
|
|
787
765
|
|
|
788
766
|
export type ErrorRouteProps<
|
package/src/useParams.tsx
CHANGED
|
@@ -2,24 +2,14 @@ import { AnyRoute } from './route'
|
|
|
2
2
|
import { RouteIds, RouteById, AllParams } from './routeInfo'
|
|
3
3
|
import { RegisteredRouter } from './router'
|
|
4
4
|
import { last } from './utils'
|
|
5
|
-
import {
|
|
5
|
+
import { useRouter } from './RouterProvider'
|
|
6
6
|
import { StrictOrFrom } from './utils'
|
|
7
7
|
|
|
8
8
|
export function useParams<
|
|
9
9
|
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
10
10
|
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
|
|
11
|
-
TDefaultSelected = AllParams<TRouteTree> &
|
|
12
|
-
RouteById<TRouteTree, TFrom>['types']['allParams'],
|
|
13
|
-
TSelected = TDefaultSelected,
|
|
14
11
|
>(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
): TSelected {
|
|
19
|
-
return useRouterState({
|
|
20
|
-
select: (state: any) => {
|
|
21
|
-
const params = (last(state.matches) as any)?.params
|
|
22
|
-
return opts?.select ? opts.select(params) : params
|
|
23
|
-
},
|
|
24
|
-
})
|
|
12
|
+
_opts: StrictOrFrom<TFrom>,
|
|
13
|
+
): AllParams<TRouteTree> & RouteById<TRouteTree, TFrom>['types']['allParams'] {
|
|
14
|
+
return (last(useRouter().state.matches) as any)?.params
|
|
25
15
|
}
|
package/src/useSearch.tsx
CHANGED
|
@@ -10,16 +10,8 @@ export function useSearch<
|
|
|
10
10
|
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
|
|
11
11
|
TStrict extends boolean = true,
|
|
12
12
|
TSearch = RouteById<TRouteTree, TFrom>['types']['fullSearchSchema'],
|
|
13
|
-
TSelected = TSearch,
|
|
14
13
|
>(
|
|
15
|
-
opts: StrictOrFrom<TFrom
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
): TStrict extends true ? TSelected : TSelected | undefined {
|
|
19
|
-
return useMatch({
|
|
20
|
-
...(opts as any),
|
|
21
|
-
select: (match: RouteMatch) => {
|
|
22
|
-
return opts?.select ? opts.select(match.search as TSearch) : match.search
|
|
23
|
-
},
|
|
24
|
-
})
|
|
14
|
+
opts: StrictOrFrom<TFrom>,
|
|
15
|
+
): TStrict extends true ? TSearch : TSearch | undefined {
|
|
16
|
+
return useMatch(opts).search
|
|
25
17
|
}
|
package/src/utils.ts
CHANGED
|
@@ -18,8 +18,8 @@ export type PickExtra<T, K> = {
|
|
|
18
18
|
[TKey in keyof K as string extends TKey
|
|
19
19
|
? never
|
|
20
20
|
: TKey extends keyof T
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
? never
|
|
22
|
+
: TKey]: K[TKey]
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export type PickRequired<T> = {
|
|
@@ -105,15 +105,15 @@ export type ValueKeys<O> = Extract<keyof O, PropertyKey>
|
|
|
105
105
|
export type DeepAwaited<T> = T extends Promise<infer A>
|
|
106
106
|
? DeepAwaited<A>
|
|
107
107
|
: T extends Record<infer A, Promise<infer B>>
|
|
108
|
-
|
|
109
|
-
|
|
108
|
+
? { [K in A]: DeepAwaited<B> }
|
|
109
|
+
: T
|
|
110
110
|
|
|
111
111
|
export type PathParamMask<TRoutePath extends string> =
|
|
112
112
|
TRoutePath extends `${infer L}/$${infer C}/${infer R}`
|
|
113
113
|
? PathParamMask<`${L}/${string}/${R}`>
|
|
114
114
|
: TRoutePath extends `${infer L}/$${infer C}`
|
|
115
|
-
|
|
116
|
-
|
|
115
|
+
? PathParamMask<`${L}/${string}`>
|
|
116
|
+
: TRoutePath
|
|
117
117
|
|
|
118
118
|
export type Timeout = ReturnType<typeof setTimeout>
|
|
119
119
|
|
|
@@ -314,10 +314,10 @@ export type RouteFromIdOrRoute<
|
|
|
314
314
|
> = T extends ParseRoute<TRouteTree>
|
|
315
315
|
? T
|
|
316
316
|
: T extends RouteIds<TRouteTree>
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
317
|
+
? RoutesById<TRouteTree>[T]
|
|
318
|
+
: T extends string
|
|
319
|
+
? RouteIds<TRouteTree>
|
|
320
|
+
: never
|
|
321
321
|
|
|
322
322
|
export function useRouteContext<
|
|
323
323
|
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
@@ -326,17 +326,9 @@ export function useRouteContext<
|
|
|
326
326
|
TRouteContext = RouteById<TRouteTree, TFrom>['types']['allContext'],
|
|
327
327
|
TSelected = TRouteContext,
|
|
328
328
|
>(
|
|
329
|
-
opts: StrictOrFrom<TFrom
|
|
330
|
-
select?: (search: TRouteContext) => TSelected
|
|
331
|
-
},
|
|
329
|
+
opts: StrictOrFrom<TFrom>,
|
|
332
330
|
): TStrict extends true ? TSelected : TSelected | undefined {
|
|
333
|
-
return useMatch(
|
|
334
|
-
...(opts as any),
|
|
335
|
-
select: (match: RouteMatch) =>
|
|
336
|
-
opts?.select
|
|
337
|
-
? opts.select(match.context as TRouteContext)
|
|
338
|
-
: match.context,
|
|
339
|
-
})
|
|
331
|
+
return useMatch(opts).context
|
|
340
332
|
}
|
|
341
333
|
|
|
342
334
|
export const useLayoutEffect =
|