Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Router and Link type definitions #435

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 11 additions & 24 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ export interface RoutableProps {
default?: boolean;
}

type DefaultParams = Record<string, string | undefined> | null;

export interface RouterOnChangeArgs<
RouteParams extends Record<string, string | undefined> | null = Record<
string,
string | undefined
> | null
RouteParams extends DefaultParams = DefaultParams
> {
router: Router;
url: string;
Expand All @@ -37,12 +36,8 @@ export interface RouterOnChangeArgs<
matches: RouteParams;
}

export interface RouterProps<
RouteParams extends Record<string, string | undefined> | null = Record<
string,
string | undefined
> | null
> extends RoutableProps {
export interface RouterProps<RouteParams extends DefaultParams = DefaultParams>
extends RoutableProps {
history?: CustomHistory;
static?: boolean;
url?: string;
Expand Down Expand Up @@ -72,22 +67,14 @@ export function Route<Props>(
props: RouteProps<Props> & Partial<Props>
): preact.VNode;

export function Link(
props: { activeClassName?: string } & preact.JSX.HTMLAttributes
): preact.VNode;
export interface LinkProps
extends Omit<preact.JSX.HTMLAttributes<HTMLAnchorElement>, 'onClick'> {}

export function Link(props: LinkProps): preact.VNode;

export function useRouter<
RouteParams extends Record<string, string | undefined> | null = Record<
string,
string | undefined
> | null
>(): [
RouterOnChangeArgs<RouteParams>,
(
urlOrOptions: string | { url: string; replace?: boolean },
replace?: boolean
) => boolean
];
RouteParams extends DefaultParams = DefaultParams
>(): [RouterOnChangeArgs<RouteParams>, typeof route];

declare module 'preact' {
export interface Attributes extends RoutableProps {}
Expand Down
7 changes: 4 additions & 3 deletions match/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as preact from 'preact';

import { Link as StaticLink, RoutableProps } from '..';
import { LinkProps as StaticLinkProps, RoutableProps } from '..';

export class Match extends preact.Component<RoutableProps, {}> {
render(): preact.VNode;
}

export interface LinkProps extends preact.JSX.HTMLAttributes {
export interface LinkProps extends StaticLinkProps {
activeClass?: string;
activeClassName?: string;
children?: preact.ComponentChildren;
path?: string;
}

export function Link(props: LinkProps): preact.VNode;
Expand Down
7 changes: 5 additions & 2 deletions test/match.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { h } from 'preact';
import { Link, RoutableProps } from '../';
import { Match } from '../match';
import { Match, Link } from '../match';

function ChildComponent({}: {}) {
return <div></div>;
Expand All @@ -11,6 +10,10 @@ function LinkComponent({}: {}) {
<div>
<Link href="/a" />
<Link activeClassName="active" href="/b" />
<Link activeClass="active" href="/c">
This is some text
</Link>
<Link path="d" />
</div>
);
}
Expand Down
15 changes: 13 additions & 2 deletions test/router.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { h, render, Component, FunctionalComponent } from 'preact';
import Router, { Route, RoutableProps, useRouter } from '../';
import { h, Component, FunctionalComponent } from 'preact';
import Router, { Link, Route, useRouter } from '../';

class ClassComponent extends Component<{}, {}> {
render() {
Expand All @@ -11,6 +11,17 @@ const SomeFunctionalComponent: FunctionalComponent<{}> = ({}) => {
return <div></div>;
};

function LinkComponent({}: {}) {
return (
<div>
<Link href="/a" />
<Link class="link" href="/b">
This is some text
</Link>
</div>
);
}

function RouterWithComponents() {
return (
<Router>
Expand Down