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

v3: link with as child pattern #378

Merged
merged 18 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"@rollup/plugin-node-resolve": "^15.0.2",
"@rollup/plugin-replace": "^5.0.2",
"@size-limit/preset-small-lib": "^10.0.1",
"@testing-library/jest-dom": "^6.1.4",
"@testing-library/react": "^14.0.0",
"@types/babel__core": "^7.20.2",
"@types/react": "^18.2.0",
Expand All @@ -158,6 +159,6 @@
"rollup": "^3.7.4",
"size-limit": "^10.0.1",
"typescript": "5.0.4",
"vitest": "^0.34.3"
"vitest": "^0.34.6"
}
}
3 changes: 2 additions & 1 deletion packages/wouter-preact/test/preact.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ describe("Preact support", () => {
<Link href="/albums/all" onClick={fn} data-testid="index-link">
The Best Albums Ever
</Link>
<Link to="/albums/london-calling">
{/* @ts-ignore */}
<Link to="/albums/london-calling" asChild>
<a data-testid="featured-link">
Featured Now: London Calling, Clash
</a>
Expand Down
1 change: 1 addition & 0 deletions packages/wouter/setup-vitest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@testing-library/jest-dom/vitest";
27 changes: 18 additions & 9 deletions packages/wouter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export const Link = forwardRef((props, ref) => {
const router = useRouter();
const [, navigate] = useLocationFromRouter(router);

const { to, href = to, children, onClick } = props;
const { to, href = to, children, asChild, onClick } = props;

const handleClick = useEvent((event) => {
// ignores the navigation when clicked using right mouse button or
Expand All @@ -204,17 +204,26 @@ export const Link = forwardRef((props, ref) => {
}
});

// wraps children in `a` if needed
const extraProps = {
// handle nested routers and absolute paths
href: href[0] === "~" ? href.slice(1) : router.base + href,
const link = href[0] === "~" ? href.slice(1) : router.base + href;

if (asChild) {
const jsx = isValidElement(children) ? children : null;
return cloneElement(jsx, {
// handle nested routers and absolute paths
href: link,
onClick: handleClick,
});
}

return h("a", {
...props,
href: link,
onClick: handleClick,
to: null,
replace: null,
state: null,
ref,
};
const jsx = isValidElement(children) ? children : h("a", props);

return cloneElement(jsx, extraProps);
});
});

const flattenChildren = (children) => {
Expand Down
Loading