@prairielearn/ui 1.1.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/components/CategoricalColumnFilter.d.ts.map +1 -1
  3. package/dist/components/CategoricalColumnFilter.js +13 -5
  4. package/dist/components/CategoricalColumnFilter.js.map +1 -1
  5. package/dist/components/ColumnManager.d.ts +2 -1
  6. package/dist/components/ColumnManager.d.ts.map +1 -1
  7. package/dist/components/ColumnManager.js +13 -28
  8. package/dist/components/ColumnManager.js.map +1 -1
  9. package/dist/components/MultiSelectColumnFilter.d.ts +25 -0
  10. package/dist/components/MultiSelectColumnFilter.d.ts.map +1 -0
  11. package/dist/components/MultiSelectColumnFilter.js +41 -0
  12. package/dist/components/MultiSelectColumnFilter.js.map +1 -0
  13. package/dist/components/NumericInputColumnFilter.d.ts +42 -0
  14. package/dist/components/NumericInputColumnFilter.d.ts.map +1 -0
  15. package/dist/components/NumericInputColumnFilter.js +79 -0
  16. package/dist/components/NumericInputColumnFilter.js.map +1 -0
  17. package/dist/components/TanstackTable.css +49 -0
  18. package/dist/components/TanstackTable.d.ts +8 -1
  19. package/dist/components/TanstackTable.d.ts.map +1 -1
  20. package/dist/components/TanstackTable.js +78 -46
  21. package/dist/components/TanstackTable.js.map +1 -1
  22. package/dist/components/TanstackTableDownloadButton.d.ts.map +1 -1
  23. package/dist/components/TanstackTableDownloadButton.js +3 -1
  24. package/dist/components/TanstackTableDownloadButton.js.map +1 -1
  25. package/dist/components/useShiftClickCheckbox.d.ts +26 -0
  26. package/dist/components/useShiftClickCheckbox.d.ts.map +1 -0
  27. package/dist/components/useShiftClickCheckbox.js +59 -0
  28. package/dist/components/useShiftClickCheckbox.js.map +1 -0
  29. package/dist/index.d.ts +4 -1
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +4 -1
  32. package/dist/index.js.map +1 -1
  33. package/package.json +6 -4
  34. package/src/components/CategoricalColumnFilter.tsx +57 -27
  35. package/src/components/ColumnManager.tsx +32 -57
  36. package/src/components/MultiSelectColumnFilter.tsx +103 -0
  37. package/src/components/NumericInputColumnFilter.test.ts +102 -0
  38. package/src/components/NumericInputColumnFilter.tsx +153 -0
  39. package/src/components/TanstackTable.css +49 -0
  40. package/src/components/TanstackTable.tsx +193 -116
  41. package/src/components/TanstackTableDownloadButton.tsx +27 -1
  42. package/src/components/useShiftClickCheckbox.tsx +67 -0
  43. package/src/index.ts +12 -1
@@ -0,0 +1,67 @@
1
+ import type { Row, Table } from '@tanstack/react-table';
2
+ import { type MouseEvent, useCallback, useState } from 'preact/compat';
3
+
4
+ /**
5
+ * A hook that provides shift-click range selection functionality for table checkboxes.
6
+ * Use this hook in the parent component and pass the returned props to individual checkboxes.
7
+ *
8
+ * @example
9
+ * ```tsx
10
+ * const { lastClickedRowIndex, createCheckboxProps } = useShiftClickCheckbox();
11
+ *
12
+ * // In your column definition:
13
+ * cell: ({ row, table }) => {
14
+ * return <input type="checkbox" {...createCheckboxProps(row, table)} />;
15
+ * }
16
+ * ```
17
+ */
18
+ export function useShiftClickCheckbox<TData>() {
19
+ const [lastClickedRowIndex, setLastClickedRowIndex] = useState<number | null>(null);
20
+
21
+ /**
22
+ * Creates props for a checkbox that supports shift-click range selection.
23
+ * @param row - The TanStack Table row
24
+ * @param table - The TanStack Table instance
25
+ * @returns Props to spread on the checkbox input element
26
+ */
27
+ const createCheckboxProps = useCallback(
28
+ (row: Row<TData>, table: Table<TData>) => {
29
+ const handleClick = (e: MouseEvent<HTMLInputElement>) => {
30
+ if (e.shiftKey && lastClickedRowIndex !== null) {
31
+ // Shift-click: select range
32
+ const currentIndex = row.index;
33
+ const start = Math.min(lastClickedRowIndex, currentIndex);
34
+ const end = Math.max(lastClickedRowIndex, currentIndex);
35
+
36
+ // Get all rows in the range
37
+ const rows = table.getRowModel().rows;
38
+ const shouldSelect = !row.getIsSelected();
39
+
40
+ // Select or deselect all rows in the range
41
+ for (let i = start; i <= end; i++) {
42
+ if (rows[i]?.getCanSelect()) {
43
+ rows[i].toggleSelected(shouldSelect);
44
+ }
45
+ }
46
+ } else {
47
+ // Normal click: toggle this row
48
+ row.getToggleSelectedHandler()(e);
49
+ }
50
+ setLastClickedRowIndex(row.index);
51
+ };
52
+
53
+ return {
54
+ checked: row.getIsSelected(),
55
+ disabled: !row.getCanSelect(),
56
+ onClick: handleClick,
57
+ };
58
+ },
59
+ [lastClickedRowIndex],
60
+ );
61
+
62
+ return {
63
+ lastClickedRowIndex,
64
+ setLastClickedRowIndex,
65
+ createCheckboxProps,
66
+ };
67
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,15 @@
1
- export { TanstackTable, TanstackTableCard } from './components/TanstackTable.js';
1
+ export {
2
+ TanstackTable,
3
+ TanstackTableCard,
4
+ TanstackTableEmptyState,
5
+ } from './components/TanstackTable.js';
2
6
  export { ColumnManager } from './components/ColumnManager.js';
3
7
  export { TanstackTableDownloadButton } from './components/TanstackTableDownloadButton.js';
4
8
  export { CategoricalColumnFilter } from './components/CategoricalColumnFilter.js';
9
+ export { MultiSelectColumnFilter } from './components/MultiSelectColumnFilter.js';
10
+ export {
11
+ NumericInputColumnFilter,
12
+ parseNumericFilter,
13
+ numericColumnFilterFn,
14
+ } from './components/NumericInputColumnFilter.js';
15
+ export { useShiftClickCheckbox } from './components/useShiftClickCheckbox.js';