@navikt/ds-react 8.10.4 → 8.10.6

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 (244) hide show
  1. package/cjs/data/data-grid/index.d.ts +2 -0
  2. package/cjs/data/data-grid/index.js +7 -0
  3. package/cjs/data/data-grid/index.js.map +1 -0
  4. package/cjs/data/data-grid/root/DataGridRoot.context.d.ts +11 -0
  5. package/cjs/data/data-grid/root/DataGridRoot.context.js +11 -0
  6. package/cjs/data/data-grid/root/DataGridRoot.context.js.map +1 -0
  7. package/cjs/data/data-grid/root/DataGridRoot.d.ts +38 -0
  8. package/cjs/data/data-grid/root/DataGridRoot.js +68 -0
  9. package/cjs/data/data-grid/root/DataGridRoot.js.map +1 -0
  10. package/cjs/data/drag-and-drop/drag-handler/DragAndDropDragHandler.js +11 -13
  11. package/cjs/data/drag-and-drop/drag-handler/DragAndDropDragHandler.js.map +1 -1
  12. package/cjs/data/drag-and-drop/root/DragAndDrop.context.d.ts +4 -2
  13. package/cjs/data/drag-and-drop/root/DragAndDrop.context.js.map +1 -1
  14. package/cjs/data/drag-and-drop/root/DragAndDropRoot.js +44 -46
  15. package/cjs/data/drag-and-drop/root/DragAndDropRoot.js.map +1 -1
  16. package/cjs/data/drag-and-drop/types.d.ts +0 -4
  17. package/cjs/data/stories/Data.test-data.d.ts +2 -5
  18. package/cjs/data/stories/Data.test-data.js +30 -39
  19. package/cjs/data/stories/Data.test-data.js.map +1 -1
  20. package/cjs/data/table/base-cell/DataTableBaseCell.d.ts +15 -15
  21. package/cjs/data/table/base-cell/DataTableBaseCell.js +4 -8
  22. package/cjs/data/table/base-cell/DataTableBaseCell.js.map +1 -1
  23. package/cjs/data/table/column-header/DataTableColumnHeader.d.ts +24 -6
  24. package/cjs/data/table/column-header/DataTableColumnHeader.js +22 -27
  25. package/cjs/data/table/column-header/DataTableColumnHeader.js.map +1 -1
  26. package/cjs/data/table/column-header/useTableColumnResize.d.ts +19 -29
  27. package/cjs/data/table/column-header/useTableColumnResize.js +24 -22
  28. package/cjs/data/table/column-header/useTableColumnResize.js.map +1 -1
  29. package/cjs/data/table/details-panel-row/DataTableDetailsPanelRow.d.ts +1 -1
  30. package/cjs/data/table/details-panel-row/DataTableDetailsPanelRow.js +2 -2
  31. package/cjs/data/table/details-panel-row/DataTableDetailsPanelRow.js.map +1 -1
  32. package/cjs/data/table/helpers/collectTableRowEntries.d.ts +9 -7
  33. package/cjs/data/table/helpers/collectTableRowEntries.js +18 -10
  34. package/cjs/data/table/helpers/collectTableRowEntries.js.map +1 -1
  35. package/cjs/data/table/helpers/selection/getMultipleSelectProps.d.ts +13 -11
  36. package/cjs/data/table/helpers/selection/getMultipleSelectProps.js +43 -53
  37. package/cjs/data/table/helpers/selection/getMultipleSelectProps.js.map +1 -1
  38. package/cjs/data/table/helpers/selection/getSingleSelectProps.d.ts +9 -8
  39. package/cjs/data/table/helpers/selection/getSingleSelectProps.js +23 -10
  40. package/cjs/data/table/helpers/selection/getSingleSelectProps.js.map +1 -1
  41. package/cjs/data/table/helpers/selection/selection.types.d.ts +19 -19
  42. package/cjs/data/table/helpers/selection/selection.utils.d.ts +21 -0
  43. package/cjs/data/table/helpers/selection/selection.utils.js +46 -0
  44. package/cjs/data/table/helpers/selection/selection.utils.js.map +1 -0
  45. package/cjs/data/table/helpers/table-focus.d.ts +0 -3
  46. package/cjs/data/table/helpers/table-focus.js +38 -8
  47. package/cjs/data/table/helpers/table-focus.js.map +1 -1
  48. package/cjs/data/table/hooks/useColumnOptions.d.ts +16 -5
  49. package/cjs/data/table/hooks/useColumnOptions.js +26 -8
  50. package/cjs/data/table/hooks/useColumnOptions.js.map +1 -1
  51. package/cjs/data/table/hooks/useGridCache.js +2 -2
  52. package/cjs/data/table/hooks/useGridCache.js.map +1 -1
  53. package/cjs/data/table/hooks/useTableDetailsPanel.d.ts +10 -13
  54. package/cjs/data/table/hooks/useTableDetailsPanel.js +7 -6
  55. package/cjs/data/table/hooks/useTableDetailsPanel.js.map +1 -1
  56. package/cjs/data/table/hooks/useTableItems.d.ts +31 -17
  57. package/cjs/data/table/hooks/useTableItems.js +10 -20
  58. package/cjs/data/table/hooks/useTableItems.js.map +1 -1
  59. package/cjs/data/table/hooks/useTableKeyboardNav.d.ts +1 -6
  60. package/cjs/data/table/hooks/useTableKeyboardNav.js +6 -5
  61. package/cjs/data/table/hooks/useTableKeyboardNav.js.map +1 -1
  62. package/cjs/data/table/hooks/useTableSelection.d.ts +6 -6
  63. package/cjs/data/table/hooks/useTableSelection.js +13 -13
  64. package/cjs/data/table/hooks/useTableSelection.js.map +1 -1
  65. package/cjs/data/table/hooks/useTableSort.d.ts +2 -2
  66. package/cjs/data/table/hooks/useTableSort.js +4 -5
  67. package/cjs/data/table/hooks/useTableSort.js.map +1 -1
  68. package/cjs/data/table/root/DataTable.types.d.ts +22 -13
  69. package/cjs/data/table/root/DataTableRoot.context.d.ts +13 -7
  70. package/cjs/data/table/root/DataTableRoot.context.js.map +1 -1
  71. package/cjs/data/table/root/DataTableRoot.d.ts +49 -72
  72. package/cjs/data/table/root/DataTableRoot.js +56 -72
  73. package/cjs/data/table/root/DataTableRoot.js.map +1 -1
  74. package/cjs/data/table/root/DataTableRoot.legacy.d.ts +2 -7
  75. package/cjs/data/table/root/DataTableRoot.legacy.js +17 -3
  76. package/cjs/data/table/root/DataTableRoot.legacy.js.map +1 -1
  77. package/cjs/data/table/sub-row-toggle/DataTableSubRowToggle.js +4 -4
  78. package/cjs/data/table/sub-row-toggle/DataTableSubRowToggle.js.map +1 -1
  79. package/cjs/data/table/tbody/DataTableTbody.js +4 -2
  80. package/cjs/data/table/tbody/DataTableTbody.js.map +1 -1
  81. package/cjs/data/table/tr/DataTableTr.d.ts +5 -3
  82. package/cjs/data/table/tr/DataTableTr.js +36 -23
  83. package/cjs/data/table/tr/DataTableTr.js.map +1 -1
  84. package/cjs/table/ColumnHeader.js +2 -1
  85. package/cjs/table/ColumnHeader.js.map +1 -1
  86. package/esm/data/data-grid/index.d.ts +2 -0
  87. package/esm/data/data-grid/index.js +3 -0
  88. package/esm/data/data-grid/index.js.map +1 -0
  89. package/esm/data/data-grid/root/DataGridRoot.context.d.ts +11 -0
  90. package/esm/data/data-grid/root/DataGridRoot.context.js +7 -0
  91. package/esm/data/data-grid/root/DataGridRoot.context.js.map +1 -0
  92. package/esm/data/data-grid/root/DataGridRoot.d.ts +38 -0
  93. package/esm/data/data-grid/root/DataGridRoot.js +32 -0
  94. package/esm/data/data-grid/root/DataGridRoot.js.map +1 -0
  95. package/esm/data/drag-and-drop/drag-handler/DragAndDropDragHandler.js +11 -13
  96. package/esm/data/drag-and-drop/drag-handler/DragAndDropDragHandler.js.map +1 -1
  97. package/esm/data/drag-and-drop/root/DragAndDrop.context.d.ts +4 -2
  98. package/esm/data/drag-and-drop/root/DragAndDrop.context.js.map +1 -1
  99. package/esm/data/drag-and-drop/root/DragAndDropRoot.js +44 -46
  100. package/esm/data/drag-and-drop/root/DragAndDropRoot.js.map +1 -1
  101. package/esm/data/drag-and-drop/types.d.ts +0 -4
  102. package/esm/data/stories/Data.test-data.d.ts +2 -5
  103. package/esm/data/stories/Data.test-data.js +30 -39
  104. package/esm/data/stories/Data.test-data.js.map +1 -1
  105. package/esm/data/table/base-cell/DataTableBaseCell.d.ts +15 -15
  106. package/esm/data/table/base-cell/DataTableBaseCell.js +4 -8
  107. package/esm/data/table/base-cell/DataTableBaseCell.js.map +1 -1
  108. package/esm/data/table/column-header/DataTableColumnHeader.d.ts +24 -6
  109. package/esm/data/table/column-header/DataTableColumnHeader.js +23 -28
  110. package/esm/data/table/column-header/DataTableColumnHeader.js.map +1 -1
  111. package/esm/data/table/column-header/useTableColumnResize.d.ts +19 -29
  112. package/esm/data/table/column-header/useTableColumnResize.js +24 -22
  113. package/esm/data/table/column-header/useTableColumnResize.js.map +1 -1
  114. package/esm/data/table/details-panel-row/DataTableDetailsPanelRow.d.ts +1 -1
  115. package/esm/data/table/details-panel-row/DataTableDetailsPanelRow.js +2 -2
  116. package/esm/data/table/details-panel-row/DataTableDetailsPanelRow.js.map +1 -1
  117. package/esm/data/table/helpers/collectTableRowEntries.d.ts +9 -7
  118. package/esm/data/table/helpers/collectTableRowEntries.js +18 -10
  119. package/esm/data/table/helpers/collectTableRowEntries.js.map +1 -1
  120. package/esm/data/table/helpers/selection/getMultipleSelectProps.d.ts +13 -11
  121. package/esm/data/table/helpers/selection/getMultipleSelectProps.js +43 -53
  122. package/esm/data/table/helpers/selection/getMultipleSelectProps.js.map +1 -1
  123. package/esm/data/table/helpers/selection/getSingleSelectProps.d.ts +9 -8
  124. package/esm/data/table/helpers/selection/getSingleSelectProps.js +23 -10
  125. package/esm/data/table/helpers/selection/getSingleSelectProps.js.map +1 -1
  126. package/esm/data/table/helpers/selection/selection.types.d.ts +19 -19
  127. package/esm/data/table/helpers/selection/selection.utils.d.ts +21 -0
  128. package/esm/data/table/helpers/selection/selection.utils.js +43 -0
  129. package/esm/data/table/helpers/selection/selection.utils.js.map +1 -0
  130. package/esm/data/table/helpers/table-focus.d.ts +0 -3
  131. package/esm/data/table/helpers/table-focus.js +38 -8
  132. package/esm/data/table/helpers/table-focus.js.map +1 -1
  133. package/esm/data/table/hooks/useColumnOptions.d.ts +16 -5
  134. package/esm/data/table/hooks/useColumnOptions.js +26 -8
  135. package/esm/data/table/hooks/useColumnOptions.js.map +1 -1
  136. package/esm/data/table/hooks/useGridCache.js +2 -2
  137. package/esm/data/table/hooks/useGridCache.js.map +1 -1
  138. package/esm/data/table/hooks/useTableDetailsPanel.d.ts +10 -13
  139. package/esm/data/table/hooks/useTableDetailsPanel.js +7 -6
  140. package/esm/data/table/hooks/useTableDetailsPanel.js.map +1 -1
  141. package/esm/data/table/hooks/useTableItems.d.ts +31 -17
  142. package/esm/data/table/hooks/useTableItems.js +11 -18
  143. package/esm/data/table/hooks/useTableItems.js.map +1 -1
  144. package/esm/data/table/hooks/useTableKeyboardNav.d.ts +1 -6
  145. package/esm/data/table/hooks/useTableKeyboardNav.js +7 -6
  146. package/esm/data/table/hooks/useTableKeyboardNav.js.map +1 -1
  147. package/esm/data/table/hooks/useTableSelection.d.ts +6 -6
  148. package/esm/data/table/hooks/useTableSelection.js +13 -13
  149. package/esm/data/table/hooks/useTableSelection.js.map +1 -1
  150. package/esm/data/table/hooks/useTableSort.d.ts +2 -2
  151. package/esm/data/table/hooks/useTableSort.js +4 -5
  152. package/esm/data/table/hooks/useTableSort.js.map +1 -1
  153. package/esm/data/table/root/DataTable.types.d.ts +22 -13
  154. package/esm/data/table/root/DataTableRoot.context.d.ts +13 -7
  155. package/esm/data/table/root/DataTableRoot.context.js.map +1 -1
  156. package/esm/data/table/root/DataTableRoot.d.ts +49 -72
  157. package/esm/data/table/root/DataTableRoot.js +58 -74
  158. package/esm/data/table/root/DataTableRoot.js.map +1 -1
  159. package/esm/data/table/root/DataTableRoot.legacy.d.ts +2 -7
  160. package/esm/data/table/root/DataTableRoot.legacy.js +17 -3
  161. package/esm/data/table/root/DataTableRoot.legacy.js.map +1 -1
  162. package/esm/data/table/sub-row-toggle/DataTableSubRowToggle.js +4 -4
  163. package/esm/data/table/sub-row-toggle/DataTableSubRowToggle.js.map +1 -1
  164. package/esm/data/table/tbody/DataTableTbody.js +4 -2
  165. package/esm/data/table/tbody/DataTableTbody.js.map +1 -1
  166. package/esm/data/table/tr/DataTableTr.d.ts +5 -3
  167. package/esm/data/table/tr/DataTableTr.js +35 -23
  168. package/esm/data/table/tr/DataTableTr.js.map +1 -1
  169. package/esm/table/ColumnHeader.js +2 -1
  170. package/esm/table/ColumnHeader.js.map +1 -1
  171. package/package.json +8 -7
  172. package/src/data/data-grid/index.ts +3 -0
  173. package/src/data/data-grid/root/DataGridRoot.context.ts +16 -0
  174. package/src/data/data-grid/root/DataGridRoot.tsx +71 -0
  175. package/src/data/drag-and-drop/drag-handler/DragAndDropDragHandler.tsx +11 -17
  176. package/src/data/drag-and-drop/root/DragAndDrop.context.tsx +4 -2
  177. package/src/data/drag-and-drop/root/DragAndDropRoot.tsx +63 -52
  178. package/src/data/drag-and-drop/types.ts +0 -5
  179. package/src/data/stories/Data.test-data.tsx +52 -44
  180. package/src/data/table/agent-component-review.md +175 -0
  181. package/src/data/table/base-cell/DataTableBaseCell.tsx +31 -21
  182. package/src/data/table/column-header/DataTableColumnHeader.tsx +63 -58
  183. package/src/data/table/column-header/useTableColumnResize.ts +55 -71
  184. package/src/data/table/details-panel-row/DataTableDetailsPanelRow.tsx +7 -3
  185. package/src/data/table/helpers/collectTableRowEntries.ts +32 -19
  186. package/src/data/table/helpers/selection/getMultipleSelectProps.ts +65 -85
  187. package/src/data/table/helpers/selection/getSingleSelectProps.ts +35 -17
  188. package/src/data/table/helpers/selection/selection.types.ts +19 -19
  189. package/src/data/table/helpers/selection/selection.utils.test.ts +161 -0
  190. package/src/data/table/helpers/selection/selection.utils.ts +73 -0
  191. package/src/data/table/helpers/table-focus.ts +63 -9
  192. package/src/data/table/hooks/__tests__/useTableItems.test.ts +48 -8
  193. package/src/data/table/hooks/useColumnOptions.ts +48 -14
  194. package/src/data/table/hooks/useGridCache.ts +3 -2
  195. package/src/data/table/hooks/useTableDetailsPanel.tsx +25 -25
  196. package/src/data/table/hooks/useTableItems.ts +51 -42
  197. package/src/data/table/hooks/useTableKeyboardNav.ts +7 -15
  198. package/src/data/table/hooks/useTableSelection.ts +26 -31
  199. package/src/data/table/hooks/useTableSort.ts +10 -9
  200. package/src/data/table/root/DataTable.types.ts +30 -25
  201. package/src/data/table/root/DataTableRoot.context.ts +19 -7
  202. package/src/data/table/root/DataTableRoot.legacy.tsx +22 -14
  203. package/src/data/table/root/DataTableRoot.tsx +271 -320
  204. package/src/data/table/sub-row-toggle/DataTableSubRowToggle.tsx +5 -4
  205. package/src/data/table/tbody/DataTableTbody.tsx +6 -2
  206. package/src/data/table/tr/DataTableTr.tsx +98 -35
  207. package/src/table/ColumnHeader.tsx +2 -1
  208. package/cjs/data/drag-and-drop-old/drag-handler/DataDragAndDropDragHandler.d.ts +0 -22
  209. package/cjs/data/drag-and-drop-old/drag-handler/DataDragAndDropDragHandler.js +0 -35
  210. package/cjs/data/drag-and-drop-old/drag-handler/DataDragAndDropDragHandler.js.map +0 -1
  211. package/cjs/data/drag-and-drop-old/item/DataDragAndDropItem.d.ts +0 -27
  212. package/cjs/data/drag-and-drop-old/item/DataDragAndDropItem.js +0 -86
  213. package/cjs/data/drag-and-drop-old/item/DataDragAndDropItem.js.map +0 -1
  214. package/cjs/data/drag-and-drop-old/root/DataDragAndDrop.context.d.ts +0 -5
  215. package/cjs/data/drag-and-drop-old/root/DataDragAndDrop.context.js +0 -6
  216. package/cjs/data/drag-and-drop-old/root/DataDragAndDrop.context.js.map +0 -1
  217. package/cjs/data/drag-and-drop-old/root/DataDragAndDropRoot.d.ts +0 -24
  218. package/cjs/data/drag-and-drop-old/root/DataDragAndDropRoot.js +0 -108
  219. package/cjs/data/drag-and-drop-old/root/DataDragAndDropRoot.js.map +0 -1
  220. package/cjs/data/table/helpers/selection/SelectionSubtreeHelper.d.ts +0 -46
  221. package/cjs/data/table/helpers/selection/SelectionSubtreeHelper.js +0 -112
  222. package/cjs/data/table/helpers/selection/SelectionSubtreeHelper.js.map +0 -1
  223. package/esm/data/drag-and-drop-old/drag-handler/DataDragAndDropDragHandler.d.ts +0 -22
  224. package/esm/data/drag-and-drop-old/drag-handler/DataDragAndDropDragHandler.js +0 -29
  225. package/esm/data/drag-and-drop-old/drag-handler/DataDragAndDropDragHandler.js.map +0 -1
  226. package/esm/data/drag-and-drop-old/item/DataDragAndDropItem.d.ts +0 -27
  227. package/esm/data/drag-and-drop-old/item/DataDragAndDropItem.js +0 -50
  228. package/esm/data/drag-and-drop-old/item/DataDragAndDropItem.js.map +0 -1
  229. package/esm/data/drag-and-drop-old/root/DataDragAndDrop.context.d.ts +0 -5
  230. package/esm/data/drag-and-drop-old/root/DataDragAndDrop.context.js +0 -3
  231. package/esm/data/drag-and-drop-old/root/DataDragAndDrop.context.js.map +0 -1
  232. package/esm/data/drag-and-drop-old/root/DataDragAndDropRoot.d.ts +0 -24
  233. package/esm/data/drag-and-drop-old/root/DataDragAndDropRoot.js +0 -68
  234. package/esm/data/drag-and-drop-old/root/DataDragAndDropRoot.js.map +0 -1
  235. package/esm/data/table/helpers/selection/SelectionSubtreeHelper.d.ts +0 -46
  236. package/esm/data/table/helpers/selection/SelectionSubtreeHelper.js +0 -109
  237. package/esm/data/table/helpers/selection/SelectionSubtreeHelper.js.map +0 -1
  238. package/src/data/drag-and-drop-old/drag-handler/DataDragAndDropDragHandler.tsx +0 -104
  239. package/src/data/drag-and-drop-old/item/DataDragAndDropItem.tsx +0 -74
  240. package/src/data/drag-and-drop-old/root/DataDragAndDrop.context.tsx +0 -11
  241. package/src/data/drag-and-drop-old/root/DataDragAndDropRoot.tsx +0 -96
  242. package/src/data/table/helpers/selection/SelectionSubtreeHelper.test.ts +0 -66
  243. package/src/data/table/helpers/selection/SelectionSubtreeHelper.ts +0 -162
  244. package/src/data/table/hooks/__tests__/useTableSelection.test.ts +0 -488
@@ -1,109 +0,0 @@
1
- /**
2
- * Helper class for managing selection state in a tree structure.
3
- * - It provides methods to get selectable keys in a subtree
4
- * - Compute selection statistics for a subtree
5
- * - Determine if a subtree is fully selected.
6
- *
7
- * Results of selection statistics are cached to optimize performance for repeated calls on the same subtree.
8
- */
9
- class SelectionSubtreeHelper {
10
- constructor({ childRowIdsById, disabledKeysSet, selectedKeysSet, }) {
11
- this.selectionStatsCache = new Map();
12
- this.childRowIdsById = childRowIdsById !== null && childRowIdsById !== void 0 ? childRowIdsById : new Map();
13
- this.disabledKeysSet = disabledKeysSet;
14
- this.selectedKeysSet = selectedKeysSet;
15
- }
16
- getSelectableKeys(rootIds) {
17
- var _a;
18
- const visitedKeys = new Set();
19
- const selectableKeys = [];
20
- const stack = [...rootIds].reverse();
21
- while (stack.length > 0) {
22
- const key = stack.pop();
23
- if (key == null || visitedKeys.has(key)) {
24
- continue;
25
- }
26
- visitedKeys.add(key);
27
- if (!this.disabledKeysSet.has(key)) {
28
- selectableKeys.push(key);
29
- }
30
- const childRowIds = (_a = this.childRowIdsById.get(key)) !== null && _a !== void 0 ? _a : [];
31
- for (let childIndex = childRowIds.length - 1; childIndex >= 0; childIndex--) {
32
- stack.push(childRowIds[childIndex]);
33
- }
34
- }
35
- return selectableKeys;
36
- }
37
- /**
38
- * Returns the number of selectable and selected rows in the subtree of the given root ID.
39
- * Results are cached after the first computation to optimize repeated calls for the same root ID.
40
- *
41
- * The selectable count excludes disabled rows, and the selected count excludes disabled rows that are selected.
42
- * The method is implemented iteratively to handle deep trees without hitting call stack limits.
43
- *
44
- * How it works:
45
- * - Manually add root ID to stack to get processing going. Note that the ready-flag is `false`.
46
- * - Pop stack until empty. For each entry:
47
- * - - If entry is already cached, skip it.
48
- * - - If entry is not ready, push it back as ready and push all its children as not ready.
49
- * - - If entry is ready, compute its stats based on its own state and the stats of its children, then cache the result.
50
- * - Since we add all the children to the stack after pushing element with ready: true, while "popping" the stack we will always encounter the children before their parent is ready, ensuring that the stats for all children are computed and cached before computing the stats for their parent.
51
- * - Finally, return the cached stats for the root ID.
52
- */
53
- getSelectionStats(rootId) {
54
- var _a, _b, _c;
55
- const cachedStats = this.selectionStatsCache.get(rootId);
56
- if (cachedStats) {
57
- return cachedStats;
58
- }
59
- /* Compute subtree totals iteratively so deep trees do not depend on call stack depth. */
60
- const stack = [
61
- { key: rootId, ready: false },
62
- ];
63
- while (stack.length > 0) {
64
- const entry = stack.pop();
65
- if (!entry) {
66
- continue;
67
- }
68
- if (this.selectionStatsCache.has(entry.key)) {
69
- continue;
70
- }
71
- if (entry.ready) {
72
- let selectableCount = this.disabledKeysSet.has(entry.key) ? 0 : 1;
73
- let selectedCount = !this.disabledKeysSet.has(entry.key) &&
74
- this.selectedKeysSet.has(entry.key)
75
- ? 1
76
- : 0;
77
- for (const childKey of (_a = this.childRowIdsById.get(entry.key)) !== null && _a !== void 0 ? _a : []) {
78
- const childStats = this.selectionStatsCache.get(childKey);
79
- if (!childStats) {
80
- continue;
81
- }
82
- selectableCount += childStats.selectableCount;
83
- selectedCount += childStats.selectedCount;
84
- }
85
- this.selectionStatsCache.set(entry.key, {
86
- selectableCount,
87
- selectedCount,
88
- });
89
- continue;
90
- }
91
- stack.push({ key: entry.key, ready: true });
92
- for (const childKey of (_b = this.childRowIdsById.get(entry.key)) !== null && _b !== void 0 ? _b : []) {
93
- if (!this.selectionStatsCache.has(childKey)) {
94
- stack.push({ key: childKey, ready: false });
95
- }
96
- }
97
- }
98
- return ((_c = this.selectionStatsCache.get(rootId)) !== null && _c !== void 0 ? _c : {
99
- selectableCount: 0,
100
- selectedCount: 0,
101
- });
102
- }
103
- isFullySelected(rootId) {
104
- const stats = this.getSelectionStats(rootId);
105
- return (stats.selectableCount > 0 && stats.selectedCount === stats.selectableCount);
106
- }
107
- }
108
- export { SelectionSubtreeHelper };
109
- //# sourceMappingURL=SelectionSubtreeHelper.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"SelectionSubtreeHelper.js","sourceRoot":"","sources":["../../../../../src/data/table/helpers/selection/SelectionSubtreeHelper.ts"],"names":[],"mappings":"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"}
@@ -1,104 +0,0 @@
1
- import React from "react";
2
- import {
3
- CaretDownCircleFillIcon,
4
- CaretUpCircleFillIcon,
5
- DragVerticalIcon,
6
- } from "@navikt/aksel-icons";
7
- import { Floating } from "../../../utils/components/floating/Floating";
8
-
9
- export interface DataDragAndDropDragHandlerProps {
10
- /**
11
- * Whether the drag handler is disabled
12
- */
13
- // disabled?: boolean;
14
- /**
15
- * Wether dragging is done by keyboard. Used to conditionally render drag indicators.
16
- */
17
- keyboardDragging?: boolean;
18
- /**
19
- * Handle ref is forwarded to the button element serving as drag handle.
20
- */
21
- handleRef: React.Ref<HTMLDivElement>;
22
- // Just for testing purposes, to render an alternative drag handler
23
- alt?: boolean;
24
- }
25
-
26
- /**
27
- * DataDragAndDropDragHandler
28
- *
29
- * A button component that serves as a drag handle for drag and drop operations.
30
- * Can be used to initiate dragging of elements in a data table or list.
31
- */
32
- export const DataDragAndDropDragHandler = React.forwardRef<
33
- HTMLButtonElement,
34
- DataDragAndDropDragHandlerProps
35
- >(({ keyboardDragging, handleRef, alt }) => {
36
- if (alt) {
37
- return (
38
- <div className="aksel-data-drag-and-drop__drag-handler__alt">
39
- {keyboardDragging && (
40
- <span
41
- className="aksel-data-drag-and-drop__drag-handler__arrow"
42
- data-direction="up"
43
- >
44
- <CaretUpCircleFillIcon aria-hidden fontSize="1.2rem" />
45
- </span>
46
- )}
47
- <div
48
- ref={handleRef}
49
- className="aksel-data-drag-and-drop__drag-handler__button"
50
- >
51
- <DragVerticalIcon
52
- aria-hidden
53
- title="Dra for å flytte"
54
- fontSize="1.5rem"
55
- />
56
- </div>
57
- {keyboardDragging && (
58
- <span
59
- className="aksel-data-drag-and-drop__drag-handler__arrow"
60
- data-direction="down"
61
- >
62
- <CaretDownCircleFillIcon aria-hidden fontSize="1.2rem" />
63
- </span>
64
- )}
65
- </div>
66
- );
67
- }
68
-
69
- return (
70
- <Floating>
71
- {keyboardDragging && (
72
- <Floating.Content
73
- side="top"
74
- avoidCollisions={false}
75
- updatePositionStrategy="always"
76
- className="aksel-data-drag-and-drop__drag-handler__arrow"
77
- >
78
- <CaretUpCircleFillIcon aria-hidden fontSize="1.2rem" />
79
- </Floating.Content>
80
- )}
81
- <Floating.Anchor asChild>
82
- <div
83
- ref={handleRef}
84
- className="aksel-data-drag-and-drop__drag-handler__button"
85
- >
86
- <DragVerticalIcon
87
- aria-hidden
88
- title="Dra for å flytte"
89
- fontSize="1.5rem"
90
- />
91
- </div>
92
- </Floating.Anchor>
93
- {keyboardDragging && (
94
- <Floating.Content
95
- avoidCollisions={false}
96
- updatePositionStrategy="always"
97
- className="aksel-data-drag-and-drop__drag-handler__arrow"
98
- >
99
- <CaretDownCircleFillIcon aria-hidden fontSize="1.2rem" />
100
- </Floating.Content>
101
- )}
102
- </Floating>
103
- );
104
- });
@@ -1,74 +0,0 @@
1
- import { useSortable } from "@dnd-kit/react/sortable";
2
- import React, { useRef } from "react";
3
- import { HStack } from "../../../primitives/stack";
4
- import { cl } from "../../../utils/helpers";
5
- import { useMergeRefs } from "../../../utils/hooks";
6
- import { DataDragAndDropDragHandler } from "../drag-handler/DataDragAndDropDragHandler";
7
- import { DataDragAndDropContext } from "../root/DataDragAndDrop.context";
8
-
9
- interface DataDragAndDropItemProps extends React.HTMLAttributes<HTMLDivElement> {
10
- children: React.ReactNode;
11
- /**
12
- * Unique id
13
- */
14
- id: string;
15
- /**
16
- * Index of the item being dragged
17
- */
18
- index: number;
19
- }
20
-
21
- /**
22
- * TODO
23
- *
24
- * @see 🏷️ {@link DataDragAndDropItemProps}
25
- * @example
26
- * ```tsx
27
- * <DragAndDrop.Item numOfSelectedRows={selectedRows.length} onClear={handleClear}>
28
- * TODO
29
- * </DragAndDrop.Item>
30
- * ```
31
- */
32
- const DataDragAndDropItem = React.forwardRef<
33
- HTMLDivElement,
34
- DataDragAndDropItemProps
35
- >(({ children, id, index, className, ...rest }, forwardedRef) => {
36
- const handleRef = useRef<HTMLDivElement>(null);
37
- const { ref, isDragging, isDropTarget } = useSortable({
38
- id,
39
- index,
40
- handle: handleRef,
41
- });
42
- const mergedRef = useMergeRefs(ref, forwardedRef);
43
- const context = React.useContext(DataDragAndDropContext);
44
- const mouseDragging = isDragging && context?.inputMethod === "mouse";
45
- const mouseDropTarget = isDropTarget && context?.inputMethod === "mouse";
46
- const keyboardDragging = isDragging && context?.inputMethod === "keyboard";
47
-
48
- return (
49
- <HStack gap="space-8" align="center" wrap={false} asChild>
50
- {/* TODO Should this be a <li>? */}
51
- <div
52
- ref={mergedRef}
53
- {...rest}
54
- className={cl("aksel-data-table__drag-and-drop-item", className)}
55
- data-dragging={isDragging}
56
- data-mouse-dragging={mouseDragging}
57
- data-keyboard-dragging={keyboardDragging}
58
- data-drop-target={mouseDropTarget}
59
- tabIndex={-1}
60
- >
61
- <DataDragAndDropDragHandler
62
- handleRef={handleRef}
63
- keyboardDragging={keyboardDragging}
64
- alt
65
- />
66
- <div>{children}</div>
67
- </div>
68
- </HStack>
69
- );
70
- });
71
-
72
- export default DataDragAndDropItem;
73
- export { DataDragAndDropItem };
74
- export type { DataDragAndDropItemProps };
@@ -1,11 +0,0 @@
1
- import { createContext } from "react";
2
-
3
- interface DataDragAndDropContextType {
4
- inputMethod: "mouse" | "keyboard" | null;
5
- // setInputMethod: (method: "mouse" | "keyboard" | null) => void;
6
- // setItems: React.Dispatch<React.SetStateAction<any[]>>;
7
- }
8
-
9
- export const DataDragAndDropContext = createContext<
10
- DataDragAndDropContextType | undefined
11
- >(undefined);
@@ -1,96 +0,0 @@
1
- import { DragDropProvider, DragOverlay } from "@dnd-kit/react";
2
- import { isSortable } from "@dnd-kit/react/sortable";
3
- import React, { forwardRef, isValidElement } from "react";
4
- import { VStack } from "../../../primitives/stack";
5
- import DataDragAndDropItem, {
6
- DataDragAndDropItemProps,
7
- } from "../item/DataDragAndDropItem";
8
- import { DataDragAndDropContext } from "./DataDragAndDrop.context";
9
-
10
- interface DataDragAndDropProps extends React.HTMLAttributes<HTMLDivElement> {
11
- children: any[];
12
- setItems: React.Dispatch<React.SetStateAction<any[]>>;
13
- }
14
-
15
- interface DataDragAndDropRootComponent extends React.ForwardRefExoticComponent<
16
- DataDragAndDropProps & React.RefAttributes<HTMLDivElement>
17
- > {
18
- /**
19
- * @see 🏷️ {@link DataDragAndDropItemProps}
20
- * * @example
21
- * ```jsx
22
- * <DragAndDrop>
23
- * <DragAndDrop.Item id="1" index={0}>
24
- * ...
25
- * </DragAndDrop.Item>
26
- * </DragAndDrop>
27
- * ```
28
- */
29
- Item: typeof DataDragAndDropItem;
30
- }
31
-
32
- const DataDragAndDrop = forwardRef<HTMLDivElement, DataDragAndDropProps>(
33
- ({ setItems, children, ...rest }, forwardedRef) => {
34
- const [inputMethod, setInputMethod] = React.useState<
35
- "mouse" | "keyboard" | null
36
- >(null);
37
-
38
- const setItemOrder = (initalIndex: number, targetIndex: number) => {
39
- setItems((items) => {
40
- const newItems = [...items];
41
- const [movedItem] = newItems.splice(initalIndex, 1);
42
- newItems.splice(targetIndex, 0, movedItem);
43
- return newItems;
44
- });
45
- };
46
-
47
- return (
48
- <DataDragAndDropContext.Provider value={{ inputMethod }}>
49
- <DragDropProvider
50
- // TODO Look into overriding default keybinds, might eliminate context need
51
- onBeforeDragStart={(event) =>
52
- setInputMethod(
53
- event.operation.activatorEvent?.type === "pointerdown"
54
- ? "mouse"
55
- : "keyboard",
56
- )
57
- }
58
- onDragOver={(event) => {
59
- if (event.operation.activatorEvent?.type === "pointerdown") {
60
- // Prevents items to rearrange while dragging with mouse, but allows keyboard dragging to work as intended
61
- event.preventDefault();
62
- }
63
- }}
64
- onDragEnd={(event) => {
65
- const { source, target } = event.operation;
66
- if (!isSortable(source) || !isSortable(target)) return;
67
- setItemOrder(source.initialIndex, target.index);
68
- }}
69
- >
70
- <VStack asChild gap="space-12">
71
- <div {...rest} ref={forwardedRef}>
72
- {children}
73
- </div>
74
- </VStack>
75
- <DragOverlay dropAnimation={null}>
76
- {(source) => {
77
- // Overlay not needed and causes glitching when using keyboard
78
- if (inputMethod === "keyboard") return null;
79
- if (!isSortable(source)) return null;
80
- if (isValidElement(children[source.initialIndex])) {
81
- return children[source.initialIndex];
82
- }
83
- return null;
84
- }}
85
- </DragOverlay>
86
- </DragDropProvider>
87
- </DataDragAndDropContext.Provider>
88
- );
89
- },
90
- ) as DataDragAndDropRootComponent;
91
-
92
- DataDragAndDrop.Item = DataDragAndDropItem;
93
-
94
- export { DataDragAndDrop, DataDragAndDropItem };
95
- export default DataDragAndDrop;
96
- export type { DataDragAndDropItemProps, DataDragAndDropProps };
@@ -1,66 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import { SelectionSubtreeHelper } from "./SelectionSubtreeHelper";
3
-
4
- const childRowIdsById = new Map<string | number, (string | number)[]>([
5
- ["a", ["a1", "a2"]],
6
- ["a1", []],
7
- ["a2", ["a2a"]],
8
- ["a2a", []],
9
- ["b", []],
10
- ]);
11
-
12
- describe("SelectionSubtreeHelper", () => {
13
- test("returns selectable subtree keys without duplicates", () => {
14
- const helper = new SelectionSubtreeHelper({
15
- childRowIdsById,
16
- disabledKeysSet: new Set(["a2"]),
17
- selectedKeysSet: new Set(),
18
- });
19
-
20
- expect(helper.getSelectableKeys(["a", "a1"])).toEqual(["a", "a1", "a2a"]);
21
- });
22
-
23
- test("returns cached subtree selection stats", () => {
24
- const helper = new SelectionSubtreeHelper({
25
- childRowIdsById,
26
- disabledKeysSet: new Set(["a2"]),
27
- selectedKeysSet: new Set(["a", "a1", "a2a"]),
28
- });
29
-
30
- const firstStats = helper.getSelectionStats("a");
31
- const secondStats = helper.getSelectionStats("a");
32
-
33
- expect(firstStats).toEqual({ selectableCount: 3, selectedCount: 3 });
34
- expect(secondStats).toBe(firstStats);
35
- expect(helper.isFullySelected("a")).toBe(true);
36
- });
37
-
38
- test("handles deep trees iteratively", () => {
39
- const depth = 12000;
40
- const deepChildRowIdsById = new Map<string | number, (string | number)[]>();
41
- const selectedKeysSet = new Set<string | number>();
42
-
43
- for (let index = 0; index < depth; index++) {
44
- const key = `node-${index}`;
45
- const childKey = `node-${index + 1}`;
46
-
47
- deepChildRowIdsById.set(key, index === depth - 1 ? [] : [childKey]);
48
-
49
- if (index % 2 === 0) {
50
- selectedKeysSet.add(key);
51
- }
52
- }
53
-
54
- const helper = new SelectionSubtreeHelper({
55
- childRowIdsById: deepChildRowIdsById,
56
- disabledKeysSet: new Set(),
57
- selectedKeysSet,
58
- });
59
-
60
- expect(helper.getSelectableKeys(["node-0"])).toHaveLength(depth);
61
- expect(helper.getSelectionStats("node-0")).toEqual({
62
- selectableCount: depth,
63
- selectedCount: Math.ceil(depth / 2),
64
- });
65
- });
66
- });
@@ -1,162 +0,0 @@
1
- type SelectionKey = string | number;
2
-
3
- type SelectionStats = {
4
- selectableCount: number;
5
- selectedCount: number;
6
- };
7
-
8
- type SelectionSubtreeHelperArgs = {
9
- childRowIdsById?: Map<SelectionKey, SelectionKey[]>;
10
- disabledKeysSet: Set<SelectionKey>;
11
- selectedKeysSet: Set<SelectionKey>;
12
- };
13
-
14
- /**
15
- * Helper class for managing selection state in a tree structure.
16
- * - It provides methods to get selectable keys in a subtree
17
- * - Compute selection statistics for a subtree
18
- * - Determine if a subtree is fully selected.
19
- *
20
- * Results of selection statistics are cached to optimize performance for repeated calls on the same subtree.
21
- */
22
- class SelectionSubtreeHelper {
23
- private childRowIdsById: Map<SelectionKey, SelectionKey[]>;
24
- private disabledKeysSet: Set<SelectionKey>;
25
- private selectedKeysSet: Set<SelectionKey>;
26
- private selectionStatsCache = new Map<SelectionKey, SelectionStats>();
27
-
28
- constructor({
29
- childRowIdsById,
30
- disabledKeysSet,
31
- selectedKeysSet,
32
- }: SelectionSubtreeHelperArgs) {
33
- this.childRowIdsById = childRowIdsById ?? new Map();
34
- this.disabledKeysSet = disabledKeysSet;
35
- this.selectedKeysSet = selectedKeysSet;
36
- }
37
-
38
- getSelectableKeys(rootIds: SelectionKey[]): SelectionKey[] {
39
- const visitedKeys = new Set<SelectionKey>();
40
- const selectableKeys: SelectionKey[] = [];
41
- const stack = [...rootIds].reverse();
42
-
43
- while (stack.length > 0) {
44
- const key = stack.pop();
45
-
46
- if (key == null || visitedKeys.has(key)) {
47
- continue;
48
- }
49
-
50
- visitedKeys.add(key);
51
-
52
- if (!this.disabledKeysSet.has(key)) {
53
- selectableKeys.push(key);
54
- }
55
-
56
- const childRowIds = this.childRowIdsById.get(key) ?? [];
57
-
58
- for (
59
- let childIndex = childRowIds.length - 1;
60
- childIndex >= 0;
61
- childIndex--
62
- ) {
63
- stack.push(childRowIds[childIndex]);
64
- }
65
- }
66
-
67
- return selectableKeys;
68
- }
69
-
70
- /**
71
- * Returns the number of selectable and selected rows in the subtree of the given root ID.
72
- * Results are cached after the first computation to optimize repeated calls for the same root ID.
73
- *
74
- * The selectable count excludes disabled rows, and the selected count excludes disabled rows that are selected.
75
- * The method is implemented iteratively to handle deep trees without hitting call stack limits.
76
- *
77
- * How it works:
78
- * - Manually add root ID to stack to get processing going. Note that the ready-flag is `false`.
79
- * - Pop stack until empty. For each entry:
80
- * - - If entry is already cached, skip it.
81
- * - - If entry is not ready, push it back as ready and push all its children as not ready.
82
- * - - If entry is ready, compute its stats based on its own state and the stats of its children, then cache the result.
83
- * - Since we add all the children to the stack after pushing element with ready: true, while "popping" the stack we will always encounter the children before their parent is ready, ensuring that the stats for all children are computed and cached before computing the stats for their parent.
84
- * - Finally, return the cached stats for the root ID.
85
- */
86
- getSelectionStats(rootId: SelectionKey): SelectionStats {
87
- const cachedStats = this.selectionStatsCache.get(rootId);
88
-
89
- if (cachedStats) {
90
- return cachedStats;
91
- }
92
-
93
- /* Compute subtree totals iteratively so deep trees do not depend on call stack depth. */
94
- const stack: { key: SelectionKey; ready: boolean }[] = [
95
- { key: rootId, ready: false },
96
- ];
97
-
98
- while (stack.length > 0) {
99
- const entry = stack.pop();
100
-
101
- if (!entry) {
102
- continue;
103
- }
104
-
105
- if (this.selectionStatsCache.has(entry.key)) {
106
- continue;
107
- }
108
-
109
- if (entry.ready) {
110
- let selectableCount = this.disabledKeysSet.has(entry.key) ? 0 : 1;
111
- let selectedCount =
112
- !this.disabledKeysSet.has(entry.key) &&
113
- this.selectedKeysSet.has(entry.key)
114
- ? 1
115
- : 0;
116
-
117
- for (const childKey of this.childRowIdsById.get(entry.key) ?? []) {
118
- const childStats = this.selectionStatsCache.get(childKey);
119
-
120
- if (!childStats) {
121
- continue;
122
- }
123
-
124
- selectableCount += childStats.selectableCount;
125
- selectedCount += childStats.selectedCount;
126
- }
127
-
128
- this.selectionStatsCache.set(entry.key, {
129
- selectableCount,
130
- selectedCount,
131
- });
132
- continue;
133
- }
134
-
135
- stack.push({ key: entry.key, ready: true });
136
-
137
- for (const childKey of this.childRowIdsById.get(entry.key) ?? []) {
138
- if (!this.selectionStatsCache.has(childKey)) {
139
- stack.push({ key: childKey, ready: false });
140
- }
141
- }
142
- }
143
-
144
- return (
145
- this.selectionStatsCache.get(rootId) ?? {
146
- selectableCount: 0,
147
- selectedCount: 0,
148
- }
149
- );
150
- }
151
-
152
- isFullySelected(rootId: SelectionKey): boolean {
153
- const stats = this.getSelectionStats(rootId);
154
-
155
- return (
156
- stats.selectableCount > 0 && stats.selectedCount === stats.selectableCount
157
- );
158
- }
159
- }
160
-
161
- export { SelectionSubtreeHelper };
162
- export type { SelectionKey, SelectionStats, SelectionSubtreeHelperArgs };