|
1 | 1 | import { Column, Table } from '@tanstack/solid-table'
|
2 | 2 | import { debounce } from '@solid-primitives/scheduled'
|
3 |
| -import { createMemo } from 'solid-js' |
| 3 | +import { createMemo, For, Show } from 'solid-js' |
4 | 4 |
|
5 |
| -function ColumnFilter({ |
6 |
| - column, |
7 |
| - table, |
8 |
| -}: { |
| 5 | +function ColumnFilter(props: { |
9 | 6 | column: Column<any, unknown>
|
10 | 7 | table: Table<any>
|
11 | 8 | }) {
|
12 |
| - const firstValue = table |
| 9 | + const firstValue = props.table |
13 | 10 | .getPreFilteredRowModel()
|
14 |
| - .flatRows[0]?.getValue(column.id) |
| 11 | + .flatRows[0]?.getValue(props.column.id) |
15 | 12 |
|
16 |
| - const columnFilterValue = column.getFilterValue() |
| 13 | + const columnFilterValue = () => props.column.getFilterValue() |
17 | 14 |
|
18 |
| - const sortedUniqueValues = createMemo( |
19 |
| - () => |
20 |
| - typeof firstValue === 'number' |
21 |
| - ? [] |
22 |
| - : Array.from(column.getFacetedUniqueValues().keys()).sort(), |
23 |
| - [column.getFacetedUniqueValues()] |
| 15 | + const sortedUniqueValues = createMemo(() => |
| 16 | + typeof firstValue === 'number' |
| 17 | + ? [] |
| 18 | + : Array.from(props.column.getFacetedUniqueValues().keys()).sort() |
24 | 19 | )
|
25 | 20 |
|
26 |
| - const debounceColumnFilter = debounce( |
27 |
| - value => column.setFilterValue(value), |
28 |
| - 500 |
29 |
| - ) |
30 |
| - |
31 |
| - return typeof firstValue === 'number' ? ( |
32 |
| - <div> |
33 |
| - <div className="flex space-x-2"> |
34 |
| - <input |
35 |
| - type="number" |
36 |
| - min={Number(column.getFacetedMinMaxValues()?.[0] ?? '')} |
37 |
| - max={Number(column.getFacetedMinMaxValues()?.[1] ?? '')} |
38 |
| - value={(columnFilterValue as [number, number])?.[0] ?? ''} |
39 |
| - onInput={e => |
40 |
| - debounceColumnFilter((old: [number, number]) => [ |
41 |
| - e.target.value, |
42 |
| - old?.[1], |
43 |
| - ]) |
44 |
| - } |
45 |
| - placeholder={`Min ${ |
46 |
| - column.getFacetedMinMaxValues()?.[0] |
47 |
| - ? `(${column.getFacetedMinMaxValues()?.[0]})` |
48 |
| - : '' |
49 |
| - }`} |
50 |
| - className="w-24 border shadow rounded" |
51 |
| - /> |
52 |
| - <input |
53 |
| - type="number" |
54 |
| - min={Number(column.getFacetedMinMaxValues()?.[0] ?? '')} |
55 |
| - max={Number(column.getFacetedMinMaxValues()?.[1] ?? '')} |
56 |
| - value={(columnFilterValue as [number, number])?.[1] ?? ''} |
57 |
| - onInput={e => |
58 |
| - debounceColumnFilter((old: [number, number]) => [ |
59 |
| - old?.[0], |
60 |
| - e.target.value, |
61 |
| - ]) |
62 |
| - } |
63 |
| - placeholder={`Max ${ |
64 |
| - column.getFacetedMinMaxValues()?.[1] |
65 |
| - ? `(${column.getFacetedMinMaxValues()?.[1]})` |
66 |
| - : '' |
67 |
| - }`} |
68 |
| - className="w-24 border shadow rounded" |
69 |
| - /> |
| 21 | + return ( |
| 22 | + <Show |
| 23 | + when={typeof firstValue === 'number'} |
| 24 | + fallback={ |
| 25 | + <div> |
| 26 | + <datalist id={`${props.column.id}list`}> |
| 27 | + <For each={sortedUniqueValues().slice(0, 5000)}> |
| 28 | + {(value: string) => <option value={value} />} |
| 29 | + </For> |
| 30 | + </datalist> |
| 31 | + <input |
| 32 | + type="text" |
| 33 | + value={(columnFilterValue() ?? '') as string} |
| 34 | + onInput={debounce( |
| 35 | + e => props.column.setFilterValue(e.target.value), |
| 36 | + 500 |
| 37 | + )} |
| 38 | + placeholder={`Search... (${props.column.getFacetedUniqueValues().size})`} |
| 39 | + class="w-36 border shadow rounded" |
| 40 | + list={`${props.column.id}list`} |
| 41 | + /> |
| 42 | + </div> |
| 43 | + } |
| 44 | + > |
| 45 | + <div> |
| 46 | + <div class="flex space-x-2"> |
| 47 | + <input |
| 48 | + type="number" |
| 49 | + min={Number(props.column.getFacetedMinMaxValues()?.[0] ?? '')} |
| 50 | + max={Number(props.column.getFacetedMinMaxValues()?.[1] ?? '')} |
| 51 | + value={(columnFilterValue() as [number, number])?.[0] ?? ''} |
| 52 | + onInput={debounce( |
| 53 | + e => |
| 54 | + props.column.setFilterValue((old: [number, number]) => [ |
| 55 | + e.target.value, |
| 56 | + old?.[1], |
| 57 | + ]), |
| 58 | + 500 |
| 59 | + )} |
| 60 | + placeholder={`Min ${ |
| 61 | + props.column.getFacetedMinMaxValues()?.[0] |
| 62 | + ? `(${props.column.getFacetedMinMaxValues()?.[0]})` |
| 63 | + : '' |
| 64 | + }`} |
| 65 | + class="w-24 border shadow rounded" |
| 66 | + /> |
| 67 | + <input |
| 68 | + type="number" |
| 69 | + min={Number(props.column.getFacetedMinMaxValues()?.[0] ?? '')} |
| 70 | + max={Number(props.column.getFacetedMinMaxValues()?.[1] ?? '')} |
| 71 | + value={(columnFilterValue() as [number, number])?.[1] ?? ''} |
| 72 | + onInput={debounce( |
| 73 | + e => |
| 74 | + props.column.setFilterValue((old: [number, number]) => [ |
| 75 | + old?.[0], |
| 76 | + e.target.value, |
| 77 | + ]), |
| 78 | + 500 |
| 79 | + )} |
| 80 | + placeholder={`Max ${ |
| 81 | + props.column.getFacetedMinMaxValues()?.[1] |
| 82 | + ? `(${props.column.getFacetedMinMaxValues()?.[1]})` |
| 83 | + : '' |
| 84 | + }`} |
| 85 | + class="w-24 border shadow rounded" |
| 86 | + /> |
| 87 | + </div> |
70 | 88 | </div>
|
71 |
| - <div className="h-1" /> |
72 |
| - </div> |
73 |
| - ) : ( |
74 |
| - <> |
75 |
| - <datalist id={column.id + 'list'}> |
76 |
| - {sortedUniqueValues() |
77 |
| - .slice(0, 5000) |
78 |
| - .map((value: any) => ( |
79 |
| - <option value={value} key={value} /> |
80 |
| - ))} |
81 |
| - </datalist> |
82 |
| - <input |
83 |
| - type="text" |
84 |
| - value={(columnFilterValue ?? '') as string} |
85 |
| - onInput={e => debounceColumnFilter(e.target.value)} |
86 |
| - placeholder={`Search... (${column.getFacetedUniqueValues().size})`} |
87 |
| - className="w-36 border shadow rounded" |
88 |
| - list={column.id + 'list'} |
89 |
| - /> |
90 |
| - <div className="h-1" /> |
91 |
| - </> |
| 89 | + </Show> |
92 | 90 | )
|
93 | 91 | }
|
94 | 92 |
|
|
0 commit comments