Skip to content

Commit e408195

Browse files
committed
doc: Fix the parseAsIndex demo
- Add pagination UI - Show the internal state - Clarify in which direction the +/-1 occurs
1 parent b71a222 commit e408195

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

packages/docs/content/docs/parsers/built-in.mdx

+3-2
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ useQueryState('hex', parseAsHex.withDefault(0x00))
108108

109109
### Index
110110

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

113114
```ts
114115
import { parseAsIndex } from 'nuqs'
115116

116-
useQueryState('page', parseAsIndex.withDefault(0))
117+
const [pageIndex] = useQueryState('page', parseAsIndex.withDefault(0))
117118
```
118119

119120
<Suspense fallback={<DemoFallback />}>

packages/docs/content/docs/parsers/demos.tsx

+44-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
'use client'
22

3+
import { CodeBlock } from '@/src/components/code-block.client'
34
import { QuerySpy } from '@/src/components/query-spy'
45
import { ContainerQueryHelper } from '@/src/components/responsive-helpers'
56
import { Button } from '@/src/components/ui/button'
67
import { Checkbox } from '@/src/components/ui/checkbox'
8+
import {
9+
Pagination,
10+
PaginationButton,
11+
PaginationContent,
12+
PaginationItem,
13+
PaginationNext,
14+
PaginationPrevious
15+
} from '@/src/components/ui/pagination'
716
import { Slider } from '@/src/components/ui/slider'
817
import { cn } from '@/src/lib/utils'
918
import { ChevronDown, ChevronUp, Minus, Star } from 'lucide-react'
@@ -173,25 +182,46 @@ export function HexParserDemo() {
173182
}
174183

175184
export function IndexParserDemo() {
176-
const [value, setValue] = useQueryState('page', parseAsIndex)
185+
const numPages = 5
186+
const [pageIndex, setPageIndex] = useQueryState(
187+
'page',
188+
parseAsIndex.withDefault(0).withOptions({ clearOnDefault: false })
189+
)
177190
return (
178191
<DemoContainer demoKey="page">
179-
<input
180-
type="number"
181-
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"
182-
value={value ?? ''} // Handle empty input
183-
onChange={e => {
184-
if (e.target.value === '') {
185-
setValue(null)
186-
} else {
187-
setValue(e.target.valueAsNumber)
188-
}
189-
}}
190-
placeholder="What page are you on?"
192+
<Pagination className="not-prose items-center gap-2">
193+
<PaginationContent>
194+
<PaginationItem>
195+
<PaginationPrevious
196+
disabled={pageIndex === 0}
197+
onClick={() => setPageIndex(p => Math.max(0, p - 1))}
198+
/>
199+
</PaginationItem>
200+
{Array.from({ length: numPages }, (_, i) => (
201+
<PaginationItem key={i}>
202+
<PaginationButton
203+
isActive={pageIndex === i}
204+
onClick={() => setPageIndex(i)}
205+
>
206+
{i + 1}
207+
</PaginationButton>
208+
</PaginationItem>
209+
))}
210+
<PaginationItem>
211+
<PaginationNext
212+
disabled={pageIndex === numPages - 1}
213+
onClick={() => setPageIndex(p => Math.min(numPages - 1, p + 1))}
214+
/>
215+
</PaginationItem>
216+
</PaginationContent>
217+
</Pagination>
218+
<CodeBlock
219+
className="my-0 flex-1 [&_pre]:py-1"
220+
code={`pageIndex: ${pageIndex} // internal state is zero-indexed`}
191221
/>
192222
<Button
193223
variant="secondary"
194-
onClick={() => setValue(null)}
224+
onClick={() => setPageIndex(null)}
195225
className="ml-auto"
196226
>
197227
Clear

0 commit comments

Comments
 (0)