Skip to content

Commit

Permalink
fix: Comments from review
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler committed Oct 25, 2024
1 parent 19ac959 commit b7d0668
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
26 changes: 21 additions & 5 deletions packages/docs/content/docs/parsers/demos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,20 @@ export function StringLiteralParserDemo() {

export function DateParserDemo({
queryKey,
parser
parser,
type
}: {
queryKey: string
parser: ParserBuilder<Date>
type: 'date' | 'datetime-local'
}) {
const [value, setValue] = useQueryState(queryKey, parser)
return (
<DemoContainer className="@container" demoKey={queryKey}>
<div className="flex w-full flex-col items-stretch gap-2 @md:flex-row">
<div className="flex flex-1 items-center gap-2">
<input
type="datetime-local"
type={type}
className="flex h-10 flex-[2] rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
value={value === null ? '' : value.toISOString().slice(0, -8)}
onChange={e => {
Expand Down Expand Up @@ -292,15 +294,29 @@ export function DateParserDemo({
}

export function DatetimeISOParserDemo() {
return <DateParserDemo queryKey="iso" parser={parseAsIsoDateTime} />
return (
<DateParserDemo
type="datetime-local"
queryKey="iso-datetime"
parser={parseAsIsoDateTime}
/>
)
}

export function DateISOParserDemo() {
return <DateParserDemo queryKey="iso" parser={parseAsIsoDate} />
return (
<DateParserDemo type="date" queryKey="iso-date" parser={parseAsIsoDate} />
)
}

export function DateTimestampParserDemo() {
return <DateParserDemo queryKey="ts" parser={parseAsTimestamp} />
return (
<DateParserDemo
type="datetime-local"
queryKey="ts"
parser={parseAsTimestamp}
/>
)
}

const jsonParserSchema = z.object({
Expand Down
4 changes: 1 addition & 3 deletions packages/nuqs/src/parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ describe('parsers', () => {
const moment = '2020-01-01'
const ref = new Date(moment)
expect(parseAsIsoDate.parse(moment)).toStrictEqual(ref)
expect(parseAsIsoDate.parse(moment.slice(0, 10))).toStrictEqual(ref)
expect(parseAsIsoDate.serialize(ref).length).toBe(10)
expect(parseAsIsoDate.serialize(ref).length).not.toContain('T')
expect(parseAsIsoDate.serialize(ref)).toEqual(moment)
})
test('parseAsArrayOf', () => {
const parser = parseAsArrayOf(parseAsString)
Expand Down
5 changes: 4 additions & 1 deletion packages/nuqs/src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,13 @@ export const parseAsIsoDateTime = createParser({
* Querystring encoded as an ISO-8601 string (UTC)
* without the time zone offset, and returned as
* a Date object.
*
* The Date is parsed without the time zone offset,
* making it at GMT 00:00:00 UTC.
*/
export const parseAsIsoDate = createParser({
parse: v => {
const date = new Date(v + 'T00:00:00.000Z')
const date = new Date(v.slice(0, 10))
if (Number.isNaN(date.valueOf())) {
return null
}
Expand Down

0 comments on commit b7d0668

Please sign in to comment.