Skip to content

Commit

Permalink
fix: navigate
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Sep 19, 2024
1 parent d74c8e9 commit 362c5bf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
33 changes: 33 additions & 0 deletions src/components/router/Navigate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useEffect } from "react"
import { type To } from "react-router-dom"

import { useNavigate, type NavigateOptions } from "../../hooks"

export type NavigateProps<
Override extends "delta" | "to",
State extends Record<string, any> = Record<string, any>,
> = Override extends "delta"
? { delta: number; to?: undefined }
: { delta?: undefined; to: To } & NavigateOptions<State>

const Navigate: {
(props: NavigateProps<"delta">): JSX.Element
<State extends Record<string, any> = Record<string, any>>(
props: NavigateProps<"to", State>,
): JSX.Element
} = ({
delta,
to,
...options
}: NavigateProps<"delta"> | NavigateProps<"to">) => {
const navigate = useNavigate()

useEffect(() => {
if (typeof delta === "number") navigate(delta)
else navigate(to, options)
}, [navigate, delta, to, options])

return <></>
}

export default Navigate
2 changes: 2 additions & 0 deletions src/components/router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export * from "./LinkListItem"
export { default as LinkListItem } from "./LinkListItem"
export * from "./LinkTab"
export { default as LinkTab } from "./LinkTab"
export * from "./Navigate"
export { default as Navigate } from "./Navigate"
14 changes: 9 additions & 5 deletions src/hooks/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
useParams as _useParams,
useSearchParams as _useSearchParams,
type Location,
type NavigateOptions,
type NavigateOptions as _NavigateOptions,
type Params,
type To,
} from "react-router-dom"
Expand All @@ -21,13 +21,17 @@ import {
type TryValidateSyncRT,
} from "../utils/schema"

export type NavigateOptions<
State extends Record<string, any> = Record<string, any>,
> = Omit<_NavigateOptions, "state"> & {
state?: State & Partial<PageState>
next?: boolean
}

export type Navigate = {
<State extends Record<string, any> = Record<string, any>>(
to: To,
options?: Omit<NavigateOptions, "state"> & {
state?: State & Partial<PageState>
next?: boolean
},
options?: NavigateOptions<State>,
): void
(delta: number): void
}
Expand Down

0 comments on commit 362c5bf

Please sign in to comment.