1
1
'use client'
2
2
3
- import { parseAsInteger , useQueryState , useQueryStates } from 'nuqs'
3
+ import {
4
+ debounce ,
5
+ parseAsInteger ,
6
+ throttle ,
7
+ useQueryState ,
8
+ useQueryStates
9
+ } from 'nuqs'
4
10
import { searchParams , urlKeys } from './search-params'
5
11
6
12
export function Client ( ) {
7
13
const [ timeMs , setTimeMs ] = useQueryState (
8
14
'debounceTime' ,
9
15
parseAsInteger . withDefault ( 100 ) . withOptions ( {
10
- limitUrlUpdates : {
11
- method : 'throttle' ,
12
- timeMs : 200
13
- }
16
+ // No real need to throttle this one, but it showcases usage:
17
+ limitUrlUpdates : throttle ( 200 )
14
18
} )
15
19
)
16
20
const [ { search, pageIndex } , setSearchParams ] = useQueryStates (
@@ -28,30 +32,23 @@ export function Client() {
28
32
setSearchParams (
29
33
{ search : e . target . value } ,
30
34
{
31
- limitUrlUpdates : {
32
- method : e . target . value === '' ? 'throttle' : 'debounce' ,
33
- timeMs : e . target . value === '' ? 50 : timeMs
34
- }
35
+ // Instant update when clearing the input, otherwise debounce
36
+ limitUrlUpdates :
37
+ e . target . value === '' ? undefined : debounce ( timeMs )
35
38
}
36
39
)
37
40
}
41
+ onKeyDown = { e => {
42
+ if ( e . key === 'Enter' ) {
43
+ // Send the search immediately when pressing Enter
44
+ setSearchParams ( { search : e . currentTarget . value } )
45
+ }
46
+ } }
38
47
/>
39
48
< button onClick = { ( ) => setSearchParams ( { pageIndex : pageIndex + 1 } ) } >
40
49
Next Page
41
50
</ button >
42
- < button
43
- onClick = { ( ) => {
44
- setTimeMs ( null )
45
- setSearchParams ( null , {
46
- limitUrlUpdates : {
47
- method : 'throttle' ,
48
- timeMs : 50
49
- }
50
- } )
51
- } }
52
- >
53
- Reset
54
- </ button >
51
+ < button onClick = { ( ) => setSearchParams ( null ) } > Reset</ button >
55
52
< div style = { { marginTop : '1rem' } } >
56
53
< label style = { { display : 'flex' , alignItems : 'center' , gap : '0.5rem' } } >
57
54
< input
0 commit comments