Files
SnowLuma/.pnpm-store/v11/files/34/36ac6c101c565215f2d9adfa0a0e990d2bbacd0e3195337db7685a53c54c285af0bfafefb0b25ea2750c80035523179083cb843d10452e5f56110c7663d375
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Skipped
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

106 lines
2.7 KiB
Plaintext

import { useMatch } from './useMatch'
import type {
StructuralSharingOption,
ValidateSelected,
} from './structuralSharing'
import type {
AnyRouter,
RegisteredRouter,
ResolveUseSearch,
StrictOrFrom,
ThrowConstraint,
ThrowOrOptional,
UseSearchResult,
} from '@tanstack/router-core'
export interface UseSearchBaseOptions<
TRouter extends AnyRouter,
TFrom,
TStrict extends boolean,
TThrow extends boolean,
TSelected,
TStructuralSharing,
> {
select?: (
state: ResolveUseSearch<TRouter, TFrom, TStrict>,
) => ValidateSelected<TRouter, TSelected, TStructuralSharing>
shouldThrow?: TThrow
}
export type UseSearchOptions<
TRouter extends AnyRouter,
TFrom,
TStrict extends boolean,
TThrow extends boolean,
TSelected,
TStructuralSharing,
> = StrictOrFrom<TRouter, TFrom, TStrict> &
UseSearchBaseOptions<
TRouter,
TFrom,
TStrict,
TThrow,
TSelected,
TStructuralSharing
> &
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>
export type UseSearchRoute<out TFrom> = <
TRouter extends AnyRouter = RegisteredRouter,
TSelected = unknown,
TStructuralSharing extends boolean = boolean,
>(
opts?: UseSearchBaseOptions<
TRouter,
TFrom,
/* TStrict */ true,
/* TThrow */ true,
TSelected,
TStructuralSharing
> &
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
) => UseSearchResult<TRouter, TFrom, true, TSelected>
/**
* Read and select the current route's search parameters with type-safety.
*
* Options:
* - `from`/`strict`: Control which route's search is read and how strictly it's typed
* - `select`: Map the search object to a derived value for render optimization
* - `structuralSharing`: Enable structural sharing for stable references
* - `shouldThrow`: Throw when the route is not found (strict contexts)
*
* @returns The search object (or selected value) for the matched route.
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useSearchHook
*/
export function useSearch<
TRouter extends AnyRouter = RegisteredRouter,
const TFrom extends string | undefined = undefined,
TStrict extends boolean = true,
TThrow extends boolean = true,
TSelected = unknown,
TStructuralSharing extends boolean = boolean,
>(
opts: UseSearchOptions<
TRouter,
TFrom,
TStrict,
ThrowConstraint<TStrict, TThrow>,
TSelected,
TStructuralSharing
>,
): ThrowOrOptional<
UseSearchResult<TRouter, TFrom, TStrict, TSelected>,
TThrow
> {
return useMatch({
from: opts.from!,
strict: opts.strict,
shouldThrow: opts.shouldThrow,
structuralSharing: opts.structuralSharing,
select: (match: any) => {
return opts.select ? opts.select(match.search) : match.search
},
}) as any
}