@@ -45,6 +45,17 @@ export function DataTable<TData, TValue>({
4545
4646 const extraClasses = modal && 'border-none shadow-none' ;
4747
48+ // Initialize search query from URL parameters on component mount
49+ const [ isInitialSearch , setIsInitialSearch ] = useState ( true ) ;
50+ const [ searchQuery , setSearchQuery ] = useState ( ( ) => {
51+ if ( typeof window !== 'undefined' ) {
52+ const params = new URLSearchParams ( window . location . search ) ;
53+ return params . get ( 'search' ) || '' ;
54+ }
55+ return '' ;
56+ } ) ;
57+ const [ isSearching , setIsSearching ] = useState ( false ) ;
58+
4859 const handlePageChange = ( url : string ) => {
4960 if ( onPageChange ) {
5061 // Use custom page change handler (for axios/API calls)
@@ -58,14 +69,18 @@ export function DataTable<TData, TValue>({
5869 onPageChange ( 1 ) ;
5970 } else {
6071 // Use Inertia router for server-side rendered pages
61- router . get ( url , { } , { preserveState : true } ) ;
72+ const urlObj = new URL ( url ) ;
73+
74+ // Preserve the current search parameter when navigating between pages
75+ if ( searchQuery ) {
76+ urlObj . searchParams . set ( 'search' , searchQuery ) ;
77+ }
78+
79+ router . get ( urlObj . toString ( ) , { } , { preserveState : true , preserveScroll : true } ) ;
6280 }
6381 } ;
6482
65- // handle search
66- const [ isInitialSearch , setIsInitialSearch ] = useState ( true ) ;
67- const [ searchQuery , setSearchQuery ] = useState ( '' ) ;
68- const [ isSearching , setIsSearching ] = useState ( false ) ;
83+ // handle search with debouncing
6984 useEffect ( ( ) => {
7085 const handler = setTimeout ( ( ) => {
7186 if ( ! isInitialSearch ) {
@@ -75,6 +90,7 @@ export function DataTable<TData, TValue>({
7590
7691 return ( ) => clearTimeout ( handler ) ;
7792 } , [ searchQuery ] ) ;
93+
7894 const handleSearch = ( ) => {
7995 if ( paginatedData ) {
8096 setIsSearching ( true ) ;
@@ -87,6 +103,7 @@ export function DataTable<TData, TValue>({
87103 { } ,
88104 {
89105 preserveState : true ,
106+ preserveScroll : true ,
90107 onSuccess : ( ) => {
91108 setIsSearching ( false ) ;
92109 } ,
@@ -103,6 +120,7 @@ export function DataTable<TData, TValue>({
103120 < Input
104121 placeholder = "Search..."
105122 className = "max-w-sm"
123+ value = { searchQuery }
106124 onChange = { ( e ) => {
107125 setIsInitialSearch ( false ) ;
108126 setSearchQuery ( e . target . value ) ;
0 commit comments