-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-table.tsx
More file actions
173 lines (154 loc) · 4.84 KB
/
Copy pathdata-table.tsx
File metadata and controls
173 lines (154 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use client';
import { useMemo } from 'react';
import {
makeParser,
parseAsStrictInteger,
useQueryState,
useQueryStates,
} from 'next-query-sync';
type SortKey = 'name' | 'price' | 'stock';
type Direction = 'asc' | 'desc';
const sortParser = makeParser<SortKey>(
(value) =>
value === 'name' || value === 'price' || value === 'stock'
? value
: null,
(value) => value
).withDefault('name');
const directionParser = makeParser<Direction>(
(value) => (value === 'asc' || value === 'desc' ? value : null),
(value) => value
).withDefault('asc');
const products = [
{ id: 'kbd-01', name: 'Mechanical Keyboard', price: 129, stock: 8 },
{ id: 'mouse-02', name: 'Wireless Mouse', price: 69, stock: 0 },
{ id: 'hub-03', name: 'USB-C Hub', price: 89, stock: 14 },
{ id: 'stand-04', name: 'Laptop Stand', price: 54, stock: 4 },
{ id: 'cam-05', name: '4K Webcam', price: 149, stock: 2 },
{ id: 'mic-06', name: 'USB Microphone', price: 119, stock: 6 },
{ id: 'pad-07', name: 'Desk Mat', price: 32, stock: 21 },
{ id: 'light-08', name: 'Monitor Light', price: 79, stock: 10 },
] as const;
const PAGE_SIZE = 3;
export function UrlBackedDataTable() {
// Search is high-frequency state: update the input immediately, but replace
// the URL only after typing settles so history is not filled with keystrokes.
const [query, setQuery] = useQueryState('q', '', {
history: 'replace',
debounce: 250,
});
// Sort controls are filters, so they replace the current history entry.
const [table, setTable] = useQueryStates(
{
sort: sortParser,
direction: directionParser,
},
{ history: 'replace' }
);
// Pagination is intentional navigation, so page changes use push history.
const [page, setPage] = useQueryState(
'page',
parseAsStrictInteger.withDefault(1),
{ history: 'push' }
);
const filtered = useMemo(() => {
const normalizedQuery = query.trim().toLowerCase();
const matching = products.filter((product) =>
normalizedQuery === ''
? true
: product.name.toLowerCase().includes(normalizedQuery)
);
return matching.slice().sort((a, b) => {
let result: number;
if (table.sort === 'name') result = a.name.localeCompare(b.name);
else if (table.sort === 'price') result = a.price - b.price;
else result = a.stock - b.stock;
return table.direction === 'asc' ? result : -result;
});
}, [query, table.direction, table.sort]);
const totalPages = Math.max(1, Math.ceil(filtered.length / PAGE_SIZE));
const visiblePage = Math.min(Math.max(1, page), totalPages);
const rows = filtered.slice(
(visiblePage - 1) * PAGE_SIZE,
visiblePage * PAGE_SIZE
);
const changeSort = (nextSort: SortKey) => {
if (table.sort === nextSort) {
setTable({
direction: table.direction === 'asc' ? 'desc' : 'asc',
});
return;
}
setTable({ sort: nextSort, direction: 'asc' });
};
return (
<section>
<label>
Search
<input
value={query}
onChange={(event) => setQuery(event.target.value)}
placeholder="keyboard, mouse, hub..."
/>
</label>
<table>
<thead>
<tr>
<th>
<button type="button" onClick={() => changeSort('name')}>
Name {table.sort === 'name' ? table.direction : ''}
</button>
</th>
<th>
<button type="button" onClick={() => changeSort('price')}>
Price {table.sort === 'price' ? table.direction : ''}
</button>
</th>
<th>
<button type="button" onClick={() => changeSort('stock')}>
Stock {table.sort === 'stock' ? table.direction : ''}
</button>
</th>
</tr>
</thead>
<tbody>
{rows.map((product) => (
<tr key={product.id}>
<td>{product.name}</td>
<td>${product.price}</td>
<td>{product.stock}</td>
</tr>
))}
</tbody>
</table>
<footer>
<button
type="button"
disabled={visiblePage <= 1}
onClick={() => setPage((current) => Math.max(1, current - 1))}
>
Previous
</button>
<span>
Page {visiblePage} of {totalPages}
</span>
<button
type="button"
disabled={visiblePage >= totalPages}
onClick={() => setPage((current) => current + 1)}
>
Next
</button>
</footer>
</section>
);
}
// Example URL:
// ?q=usb&sort=price&direction=desc&page=2
//
// History intent:
// - q/sort/direction -> replace
// - page -> push
//
// The strict page parser rejects malformed values such as ?page=2px and falls
// back to page 1 instead of accepting a numeric prefix.