Skip to content

Commit

Permalink
fixes default param bug
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenhs committed Oct 14, 2020
1 parent 24513fb commit cd4ffcc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
10 changes: 6 additions & 4 deletions src/createLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ export function createLocation({
state: {} as ParamWithContextCollection,
};

for (const paramName in paramCollection) {
const paramValue = paramCollection[paramName];
for (const paramName in paramDefCollection) {
const paramDef = paramDefCollection[paramName];
const paramValue =
paramCollection[paramName] === undefined
? paramDef["~internal"].default
: paramCollection[paramName];

if (paramValue === undefined) {
continue;
}

const paramDef = paramDefCollection[paramName];

const urlEncodeDefault =
paramDef["~internal"].kind !== "state" && !paramDef["~internal"].trailing;
const urlEncode =
Expand Down
12 changes: 3 additions & 9 deletions src/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// @ts-ignore
window.__DEV__ = true;

import React, { useEffect } from "react";
import React from "react";
import ReactDOM from "react-dom";
import {
createRouter,
Expand All @@ -16,7 +16,7 @@ export const { RouteProvider, useRoute, routes } = createRouter({
home: defineRoute("/"),
userList: defineRoute(
{
page: param.query.optional.number,
page: param.query.optional.number.default(1),
},
() => "/user"
),
Expand All @@ -31,12 +31,6 @@ export const { RouteProvider, useRoute, routes } = createRouter({
function App() {
const route = useRoute();

useEffect(() => {
if (route.name === "home") {
routes.user({ userId: "abc" }).replace();
}
}, [route]);

return (
<>
<Navigation />
Expand Down Expand Up @@ -75,7 +69,7 @@ function Navigation() {
return (
<nav>
<a {...replaceLink(routes.home())}>Home</a>
<a {...routes.userList({ page: 1 }).link}>User List</a>
<a {...routes.userList().link}>User List</a>
<a {...routes.user({ userId: "abc" }).link}>User "abc"</a>
</nav>
);
Expand Down
15 changes: 15 additions & 0 deletions test/createLocation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ describe("createLocation", () => {
state: undefined,
});
});

test.only("default param", () => {
expectLocation(
{
value: param.query.optional.string.default("bar"),
},
() => `/foo`,
{}
).toEqual({
fullPath: "/foo",
path: "/foo",
query: "value=bar",
state: undefined,
});
});
});

function expectLocation(
Expand Down

0 comments on commit cd4ffcc

Please sign in to comment.