MCPcopy Create free account
hub / github.com/bvaughn/react-virtualized / Example

Function Example

src/routes/guides/examples/MultiColumnSortTable.tsx:22–121  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

20};
21
22function Example() {
23 // Sort criteria are stored in React state so that changes re-render the Table
24 const [{ sortBy, sortDirection }, setSortState] = useState<SortState>({
25 sortBy: ["name"],
26 sortDirection: { name: "ASC" }
27 });
28
29 // createTableMultiSort tracks the sort criteria;
30 // it calls this function (with new sortBy and sortDirection values) whenever they change.
31 const [multiSort] = useState(() =>
32 createTableMultiSort(
33 ({ sortBy, sortDirection }) => setSortState({ sortBy, sortDirection }),
34 {
35 defaultSortBy: ["name"],
36 defaultSortDirection: { name: "ASC" }
37 }
38 )
39 );
40
41 // Sort the collection by each field, in order of importance
42 const sortedList = useMemo(
43 () =>
44 [...list].sort((a, b) => {
45 for (const dataKey of sortBy) {
46 const key = dataKey as keyof ListItem;
47 const order = a[key] < b[key] ? -1 : a[key] > b[key] ? 1 : 0;
48 if (order !== 0) {
49 return sortDirection[dataKey] === "DESC" ? -order : order;
50 }
51 }
52 return 0;
53 }),
54 [sortBy, sortDirection]
55 );
56
57 // Use the multi-sort state (rather than the Table's sortBy prop) to show indicators
58 const headerRenderer: ColumnProps["headerRenderer"] = ({
59 dataKey,
60 label
61 }) => {
62 const position = sortBy.indexOf(dataKey);
63 return (
64 <div className="flex items-center gap-1">
65 <span title={typeof label === "string" ? label : undefined}>
66 {label}
67 </span>
68 {position >= 0 && (
69 <>
70 <SortIndicator sortDirection={sortDirection[dataKey]} />
71 {sortBy.length > 1 && (
72 <span className="text-xs text-ink/60">{position + 1}</span>
73 )}
74 </>
75 )}
76 </div>
77 );
78 };
79

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected