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

doc: Fix the parseAsIndex demo #922

Merged
merged 1 commit into from
Feb 17, 2025
Merged
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
5 changes: 3 additions & 2 deletions packages/docs/content/docs/parsers/built-in.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ useQueryState('hex', parseAsHex.withDefault(0x00))

### Index

Same as integer, but adds a `+1` offset to the query value. Useful for pagination indexes.
Same as integer, but adds a `+1` offset to the serialized querystring (and `-1` when parsing).
Useful for pagination indexes.

```ts
import { parseAsIndex } from 'nuqs'

useQueryState('page', parseAsIndex.withDefault(0))
const [pageIndex] = useQueryState('page', parseAsIndex.withDefault(0))
```

<Suspense fallback={<DemoFallback />}>
Expand Down
58 changes: 44 additions & 14 deletions packages/docs/content/docs/parsers/demos.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
'use client'

import { CodeBlock } from '@/src/components/code-block.client'
import { QuerySpy } from '@/src/components/query-spy'
import { ContainerQueryHelper } from '@/src/components/responsive-helpers'
import { Button } from '@/src/components/ui/button'
import { Checkbox } from '@/src/components/ui/checkbox'
import {
Pagination,
PaginationButton,
PaginationContent,
PaginationItem,
PaginationNext,
PaginationPrevious
} from '@/src/components/ui/pagination'
import { Slider } from '@/src/components/ui/slider'
import { cn } from '@/src/lib/utils'
import { ChevronDown, ChevronUp, Minus, Star } from 'lucide-react'
Expand Down Expand Up @@ -173,25 +182,46 @@ export function HexParserDemo() {
}

export function IndexParserDemo() {
const [value, setValue] = useQueryState('page', parseAsIndex)
const numPages = 5
const [pageIndex, setPageIndex] = useQueryState(
'page',
parseAsIndex.withDefault(0).withOptions({ clearOnDefault: false })
)
return (
<DemoContainer demoKey="page">
<input
type="number"
className="flex h-10 flex-1 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 ?? ''} // Handle empty input
onChange={e => {
if (e.target.value === '') {
setValue(null)
} else {
setValue(e.target.valueAsNumber)
}
}}
placeholder="What page are you on?"
<Pagination className="not-prose items-center gap-2">
<PaginationContent>
<PaginationItem>
<PaginationPrevious
disabled={pageIndex === 0}
onClick={() => setPageIndex(p => Math.max(0, p - 1))}
/>
</PaginationItem>
{Array.from({ length: numPages }, (_, i) => (
<PaginationItem key={i}>
<PaginationButton
isActive={pageIndex === i}
onClick={() => setPageIndex(i)}
>
{i + 1}
</PaginationButton>
</PaginationItem>
))}
<PaginationItem>
<PaginationNext
disabled={pageIndex === numPages - 1}
onClick={() => setPageIndex(p => Math.min(numPages - 1, p + 1))}
/>
</PaginationItem>
</PaginationContent>
</Pagination>
<CodeBlock
className="my-0 flex-1 [&_pre]:py-1"
code={`pageIndex: ${pageIndex} // internal state is zero-indexed`}
/>
<Button
variant="secondary"
onClick={() => setValue(null)}
onClick={() => setPageIndex(null)}
className="ml-auto"
>
Clear
Expand Down