Skip to content

Commit

Permalink
fix: state param for link components
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Sep 20, 2024
1 parent a270c11 commit 3972609
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
13 changes: 10 additions & 3 deletions src/components/router/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Link as MuiLink, type LinkProps as MuiLinkProps } from "@mui/material"
import type { FC } from "react"
import { Link as RouterLink } from "react-router-dom"

import { type LinkProps as RouterLinkProps } from "../../utils/router"

export type LinkProps = Omit<MuiLinkProps, "component"> & RouterLinkProps
export type LinkProps<
Override extends "delta" | "to",
State extends Record<string, any> = Record<string, any>,
> = Omit<MuiLinkProps, "component"> & RouterLinkProps<Override, State>

// https://mui.com/material-ui/integrations/routing/#link
const Link: FC<LinkProps> = props => {
const Link: {
(props: LinkProps<"delta">): JSX.Element
<State extends Record<string, any> = Record<string, any>>(
props: LinkProps<"to", State>,
): JSX.Element
} = (props: LinkProps<"delta"> | LinkProps<"to">) => {
// @ts-expect-error
return <MuiLink component={RouterLink} {...props} />
}
Expand Down
13 changes: 10 additions & 3 deletions src/components/router/LinkButton.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Button, type ButtonProps } from "@mui/material"
import type { FC } from "react"
import { Link } from "react-router-dom"

import { type LinkProps } from "../../utils/router"

export type LinkButtonProps = Omit<ButtonProps, "component"> & LinkProps
export type LinkButtonProps<
Override extends "delta" | "to",
State extends Record<string, any> = Record<string, any>,
> = Omit<ButtonProps, "component"> & LinkProps<Override, State>

// https://mui.com/material-ui/integrations/routing/#button
const LinkButton: FC<LinkButtonProps> = props => {
const LinkButton: {
(props: LinkButtonProps<"delta">): JSX.Element
<State extends Record<string, any> = Record<string, any>>(
props: LinkButtonProps<"to", State>,
): JSX.Element
} = (props: LinkButtonProps<"delta"> | LinkButtonProps<"to">) => {
return <Button {...{ ...props, component: Link }} />
}

Expand Down
13 changes: 10 additions & 3 deletions src/components/router/LinkIconButton.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { IconButton, type IconButtonProps } from "@mui/material"
import type { FC } from "react"
import { Link } from "react-router-dom"

import { type LinkProps } from "../../utils/router"

export type LinkIconButtonProps = Omit<IconButtonProps, "component"> & LinkProps
export type LinkIconButtonProps<
Override extends "delta" | "to",
State extends Record<string, any> = Record<string, any>,
> = Omit<IconButtonProps, "component"> & LinkProps<Override, State>

// https://mui.com/material-ui/integrations/routing/#button
const LinkIconButton: FC<LinkIconButtonProps> = props => {
const LinkIconButton: {
(props: LinkIconButtonProps<"delta">): JSX.Element
<State extends Record<string, any> = Record<string, any>>(
props: LinkIconButtonProps<"to", State>,
): JSX.Element
} = (props: LinkIconButtonProps<"delta"> | LinkIconButtonProps<"to">) => {
return <IconButton {...{ ...props, component: Link }} />
}

Expand Down
13 changes: 10 additions & 3 deletions src/components/router/LinkListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { ListItem, type ListItemProps } from "@mui/material"
import type { FC } from "react"
import { Link } from "react-router-dom"

import { type LinkProps } from "../../utils/router"

export type LinkListItemProps = Omit<ListItemProps, "component"> & LinkProps
export type LinkListItemProps<
Override extends "delta" | "to",
State extends Record<string, any> = Record<string, any>,
> = Omit<ListItemProps, "component"> & LinkProps<Override, State>

// https://mui.com/material-ui/integrations/routing/#list
const LinkListItem: FC<LinkListItemProps> = props => {
const LinkListItem: {
(props: LinkListItemProps<"delta">): JSX.Element
<State extends Record<string, any> = Record<string, any>>(
props: LinkListItemProps<"to", State>,
): JSX.Element
} = (props: LinkListItemProps<"delta"> | LinkListItemProps<"to">) => {
return <ListItem {...{ ...props, component: Link }} />
}

Expand Down
13 changes: 10 additions & 3 deletions src/components/router/LinkTab.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Tab, type TabProps } from "@mui/material"
import type { FC } from "react"
import { Link } from "react-router-dom"

import { type LinkProps } from "../../utils/router"

export type LinkTabProps = Omit<TabProps, "component"> & LinkProps
export type LinkTabProps<
Override extends "delta" | "to",
State extends Record<string, any> = Record<string, any>,
> = Omit<TabProps, "component"> & LinkProps<Override, State>

// https://mui.com/material-ui/integrations/routing/#tabs
const LinkTab: FC<LinkTabProps> = props => {
const LinkTab: {
(props: LinkTabProps<"delta">): JSX.Element
<State extends Record<string, any> = Record<string, any>>(
props: LinkTabProps<"to", State>,
): JSX.Element
} = (props: LinkTabProps<"delta"> | LinkTabProps<"to">) => {
return <Tab {...{ ...props, component: Link }} />
}

Expand Down
10 changes: 9 additions & 1 deletion src/utils/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import {
type To,
} from "react-router-dom"

export type LinkProps = Omit<_LinkProps, "to"> & { to: To | number }
import { type PageState } from "../components/page/Page"

export type LinkProps<
Override extends "delta" | "to",
State extends Record<string, any> = Record<string, any>,
> = Omit<_LinkProps, "to" | "state"> &
(Override extends "delta"
? { to: number }
: { to: To; state?: State & Partial<PageState> })

export type ReadOnly<T> = {
readonly [P in keyof T]: T[P]
Expand Down

0 comments on commit 3972609

Please sign in to comment.