@fluentui/react-table 9.0.0-alpha.1 → 9.0.0-alpha.2

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 (195) hide show
  1. package/CHANGELOG.json +135 -4
  2. package/CHANGELOG.md +31 -5
  3. package/dist/index.d.ts +312 -21
  4. package/lib/TableCellActions.js +2 -0
  5. package/lib/TableCellActions.js.map +1 -0
  6. package/lib/TableCellLayout.js +2 -0
  7. package/lib/TableCellLayout.js.map +1 -0
  8. package/lib/TableCellPrimaryLayout.js +2 -0
  9. package/lib/TableCellPrimaryLayout.js.map +1 -0
  10. package/lib/TableSelectionCell.js +2 -0
  11. package/lib/TableSelectionCell.js.map +1 -0
  12. package/lib/components/Table/Table.types.js.map +1 -1
  13. package/lib/components/Table/useTableContextValues.js +7 -5
  14. package/lib/components/Table/useTableContextValues.js.map +1 -1
  15. package/lib/components/Table/useTableStyles.js +7 -2
  16. package/lib/components/Table/useTableStyles.js.map +1 -1
  17. package/lib/components/TableBody/useTableBody.js +3 -1
  18. package/lib/components/TableBody/useTableBody.js.map +1 -1
  19. package/lib/components/TableBody/useTableBodyStyles.js +12 -2
  20. package/lib/components/TableBody/useTableBodyStyles.js.map +1 -1
  21. package/lib/components/TableCell/TableCell.types.js.map +1 -1
  22. package/lib/components/TableCell/renderTableCell.js +2 -4
  23. package/lib/components/TableCell/renderTableCell.js.map +1 -1
  24. package/lib/components/TableCell/useTableCell.js +5 -5
  25. package/lib/components/TableCell/useTableCell.js.map +1 -1
  26. package/lib/components/TableCell/useTableCellStyles.js +6 -20
  27. package/lib/components/TableCell/useTableCellStyles.js.map +1 -1
  28. package/lib/components/TableCellActions/TableCellActions.js +15 -0
  29. package/lib/components/TableCellActions/TableCellActions.js.map +1 -0
  30. package/lib/components/TableCellActions/TableCellActions.types.js +2 -0
  31. package/lib/components/TableCellActions/TableCellActions.types.js.map +1 -0
  32. package/lib/components/TableCellActions/index.js +6 -0
  33. package/lib/components/TableCellActions/index.js.map +1 -0
  34. package/lib/components/TableCellActions/renderTableCellActions.js +16 -0
  35. package/lib/components/TableCellActions/renderTableCellActions.js.map +1 -0
  36. package/lib/components/TableCellActions/useTableCellActions.js +24 -0
  37. package/lib/components/TableCellActions/useTableCellActions.js.map +1 -0
  38. package/lib/components/TableCellActions/useTableCellActionsStyles.js +39 -0
  39. package/lib/components/TableCellActions/useTableCellActionsStyles.js.map +1 -0
  40. package/lib/components/TableCellLayout/TableCellLayout.js +15 -0
  41. package/lib/components/TableCellLayout/TableCellLayout.js.map +1 -0
  42. package/lib/components/TableCellLayout/TableCellLayout.types.js +2 -0
  43. package/lib/components/TableCellLayout/TableCellLayout.types.js.map +1 -0
  44. package/lib/components/TableCellLayout/index.js +6 -0
  45. package/lib/components/TableCellLayout/index.js.map +1 -0
  46. package/lib/components/TableCellLayout/renderTableCellLayout.js +19 -0
  47. package/lib/components/TableCellLayout/renderTableCellLayout.js.map +1 -0
  48. package/lib/components/TableCellLayout/useTableCellLayout.js +36 -0
  49. package/lib/components/TableCellLayout/useTableCellLayout.js.map +1 -0
  50. package/lib/components/TableCellLayout/useTableCellLayoutStyles.js +74 -0
  51. package/lib/components/TableCellLayout/useTableCellLayoutStyles.js.map +1 -0
  52. package/lib/components/TableHeader/useTableHeader.js +4 -2
  53. package/lib/components/TableHeader/useTableHeader.js.map +1 -1
  54. package/lib/components/TableHeader/useTableHeaderStyles.js +12 -2
  55. package/lib/components/TableHeader/useTableHeaderStyles.js.map +1 -1
  56. package/lib/components/TableHeaderCell/useTableHeaderCell.js +4 -2
  57. package/lib/components/TableHeaderCell/useTableHeaderCell.js.map +1 -1
  58. package/lib/components/TableHeaderCell/useTableHeaderCellStyles.js +10 -9
  59. package/lib/components/TableHeaderCell/useTableHeaderCellStyles.js.map +1 -1
  60. package/lib/components/TableRow/useTableRow.js +4 -2
  61. package/lib/components/TableRow/useTableRow.js.map +1 -1
  62. package/lib/components/TableRow/useTableRowStyles.js +9 -7
  63. package/lib/components/TableRow/useTableRowStyles.js.map +1 -1
  64. package/lib/components/TableSelectionCell/TableSelectionCell.js +15 -0
  65. package/lib/components/TableSelectionCell/TableSelectionCell.js.map +1 -0
  66. package/lib/components/TableSelectionCell/TableSelectionCell.types.js +2 -0
  67. package/lib/components/TableSelectionCell/TableSelectionCell.types.js.map +1 -0
  68. package/lib/components/TableSelectionCell/index.js +6 -0
  69. package/lib/components/TableSelectionCell/index.js.map +1 -0
  70. package/lib/components/TableSelectionCell/renderTableSelectionCell.js +17 -0
  71. package/lib/components/TableSelectionCell/renderTableSelectionCell.js.map +1 -0
  72. package/lib/components/TableSelectionCell/useTableSelectionCell.js +42 -0
  73. package/lib/components/TableSelectionCell/useTableSelectionCell.js.map +1 -0
  74. package/lib/components/TableSelectionCell/useTableSelectionCellStyles.js +55 -0
  75. package/lib/components/TableSelectionCell/useTableSelectionCellStyles.js.map +1 -0
  76. package/lib/contexts/tableContext.js +7 -3
  77. package/lib/contexts/tableContext.js.map +1 -1
  78. package/lib/hooks/index.js +3 -0
  79. package/lib/hooks/index.js.map +1 -0
  80. package/lib/hooks/selectionManager.js +89 -0
  81. package/lib/hooks/selectionManager.js.map +1 -0
  82. package/lib/hooks/types.js +2 -0
  83. package/lib/hooks/types.js.map +1 -0
  84. package/lib/hooks/useSelection.js +47 -0
  85. package/lib/hooks/useSelection.js.map +1 -0
  86. package/lib/hooks/useSort.js +72 -0
  87. package/lib/hooks/useSort.js.map +1 -0
  88. package/lib/hooks/useTable.js +87 -0
  89. package/lib/hooks/useTable.js.map +1 -0
  90. package/lib/index.js +4 -0
  91. package/lib/index.js.map +1 -1
  92. package/lib/navigationModes/cell.js +250 -0
  93. package/lib/navigationModes/cell.js.map +1 -0
  94. package/lib/navigationModes/composite.js +208 -0
  95. package/lib/navigationModes/composite.js.map +1 -0
  96. package/lib/navigationModes/index.js +3 -0
  97. package/lib/navigationModes/index.js.map +1 -0
  98. package/lib/navigationModes/useNavigationMode.js +42 -0
  99. package/lib/navigationModes/useNavigationMode.js.map +1 -0
  100. package/lib-commonjs/TableCellActions.js +10 -0
  101. package/lib-commonjs/TableCellActions.js.map +1 -0
  102. package/lib-commonjs/TableCellLayout.js +10 -0
  103. package/lib-commonjs/TableCellLayout.js.map +1 -0
  104. package/lib-commonjs/TableCellPrimaryLayout.js +10 -0
  105. package/lib-commonjs/TableCellPrimaryLayout.js.map +1 -0
  106. package/lib-commonjs/TableSelectionCell.js +10 -0
  107. package/lib-commonjs/TableSelectionCell.js.map +1 -0
  108. package/lib-commonjs/components/Table/useTableContextValues.js +8 -5
  109. package/lib-commonjs/components/Table/useTableContextValues.js.map +1 -1
  110. package/lib-commonjs/components/Table/useTableStyles.js +8 -2
  111. package/lib-commonjs/components/Table/useTableStyles.js.map +1 -1
  112. package/lib-commonjs/components/TableBody/useTableBody.js +3 -1
  113. package/lib-commonjs/components/TableBody/useTableBody.js.map +1 -1
  114. package/lib-commonjs/components/TableBody/useTableBodyStyles.js +10 -1
  115. package/lib-commonjs/components/TableBody/useTableBodyStyles.js.map +1 -1
  116. package/lib-commonjs/components/TableCell/renderTableCell.js +2 -4
  117. package/lib-commonjs/components/TableCell/renderTableCell.js.map +1 -1
  118. package/lib-commonjs/components/TableCell/useTableCell.js +4 -4
  119. package/lib-commonjs/components/TableCell/useTableCell.js.map +1 -1
  120. package/lib-commonjs/components/TableCell/useTableCellStyles.js +6 -20
  121. package/lib-commonjs/components/TableCell/useTableCellStyles.js.map +1 -1
  122. package/lib-commonjs/components/TableCellActions/TableCellActions.js +26 -0
  123. package/lib-commonjs/components/TableCellActions/TableCellActions.js.map +1 -0
  124. package/lib-commonjs/components/TableCellActions/TableCellActions.types.js +6 -0
  125. package/lib-commonjs/components/TableCellActions/TableCellActions.types.js.map +1 -0
  126. package/lib-commonjs/components/TableCellActions/index.js +18 -0
  127. package/lib-commonjs/components/TableCellActions/index.js.map +1 -0
  128. package/lib-commonjs/components/TableCellActions/renderTableCellActions.js +27 -0
  129. package/lib-commonjs/components/TableCellActions/renderTableCellActions.js.map +1 -0
  130. package/lib-commonjs/components/TableCellActions/useTableCellActions.js +35 -0
  131. package/lib-commonjs/components/TableCellActions/useTableCellActions.js.map +1 -0
  132. package/lib-commonjs/components/TableCellActions/useTableCellActionsStyles.js +51 -0
  133. package/lib-commonjs/components/TableCellActions/useTableCellActionsStyles.js.map +1 -0
  134. package/lib-commonjs/components/TableCellLayout/TableCellLayout.js +26 -0
  135. package/lib-commonjs/components/TableCellLayout/TableCellLayout.js.map +1 -0
  136. package/lib-commonjs/components/TableCellLayout/TableCellLayout.types.js +6 -0
  137. package/lib-commonjs/components/TableCellLayout/TableCellLayout.types.js.map +1 -0
  138. package/lib-commonjs/components/TableCellLayout/index.js +18 -0
  139. package/lib-commonjs/components/TableCellLayout/index.js.map +1 -0
  140. package/lib-commonjs/components/TableCellLayout/renderTableCellLayout.js +30 -0
  141. package/lib-commonjs/components/TableCellLayout/renderTableCellLayout.js.map +1 -0
  142. package/lib-commonjs/components/TableCellLayout/useTableCellLayout.js +46 -0
  143. package/lib-commonjs/components/TableCellLayout/useTableCellLayout.js.map +1 -0
  144. package/lib-commonjs/components/TableCellLayout/useTableCellLayoutStyles.js +85 -0
  145. package/lib-commonjs/components/TableCellLayout/useTableCellLayoutStyles.js.map +1 -0
  146. package/lib-commonjs/components/TableHeader/useTableHeader.js +4 -2
  147. package/lib-commonjs/components/TableHeader/useTableHeader.js.map +1 -1
  148. package/lib-commonjs/components/TableHeader/useTableHeaderStyles.js +11 -1
  149. package/lib-commonjs/components/TableHeader/useTableHeaderStyles.js.map +1 -1
  150. package/lib-commonjs/components/TableHeaderCell/useTableHeaderCell.js +4 -2
  151. package/lib-commonjs/components/TableHeaderCell/useTableHeaderCell.js.map +1 -1
  152. package/lib-commonjs/components/TableHeaderCell/useTableHeaderCellStyles.js +10 -9
  153. package/lib-commonjs/components/TableHeaderCell/useTableHeaderCellStyles.js.map +1 -1
  154. package/lib-commonjs/components/TableRow/useTableRow.js +4 -2
  155. package/lib-commonjs/components/TableRow/useTableRow.js.map +1 -1
  156. package/lib-commonjs/components/TableRow/useTableRowStyles.js +10 -7
  157. package/lib-commonjs/components/TableRow/useTableRowStyles.js.map +1 -1
  158. package/lib-commonjs/components/TableSelectionCell/TableSelectionCell.js +26 -0
  159. package/lib-commonjs/components/TableSelectionCell/TableSelectionCell.js.map +1 -0
  160. package/lib-commonjs/components/TableSelectionCell/TableSelectionCell.types.js +6 -0
  161. package/lib-commonjs/components/TableSelectionCell/TableSelectionCell.types.js.map +1 -0
  162. package/lib-commonjs/components/TableSelectionCell/index.js +18 -0
  163. package/lib-commonjs/components/TableSelectionCell/index.js.map +1 -0
  164. package/lib-commonjs/components/TableSelectionCell/renderTableSelectionCell.js +28 -0
  165. package/lib-commonjs/components/TableSelectionCell/renderTableSelectionCell.js.map +1 -0
  166. package/lib-commonjs/components/TableSelectionCell/useTableSelectionCell.js +56 -0
  167. package/lib-commonjs/components/TableSelectionCell/useTableSelectionCell.js.map +1 -0
  168. package/lib-commonjs/components/TableSelectionCell/useTableSelectionCellStyles.js +65 -0
  169. package/lib-commonjs/components/TableSelectionCell/useTableSelectionCellStyles.js.map +1 -0
  170. package/lib-commonjs/contexts/tableContext.js +7 -3
  171. package/lib-commonjs/contexts/tableContext.js.map +1 -1
  172. package/lib-commonjs/hooks/index.js +12 -0
  173. package/lib-commonjs/hooks/index.js.map +1 -0
  174. package/lib-commonjs/hooks/selectionManager.js +98 -0
  175. package/lib-commonjs/hooks/selectionManager.js.map +1 -0
  176. package/lib-commonjs/hooks/types.js +6 -0
  177. package/lib-commonjs/hooks/types.js.map +1 -0
  178. package/lib-commonjs/hooks/useSelection.js +59 -0
  179. package/lib-commonjs/hooks/useSelection.js.map +1 -0
  180. package/lib-commonjs/hooks/useSort.js +82 -0
  181. package/lib-commonjs/hooks/useSort.js.map +1 -0
  182. package/lib-commonjs/hooks/useTable.js +99 -0
  183. package/lib-commonjs/hooks/useTable.js.map +1 -0
  184. package/lib-commonjs/index.js +110 -1
  185. package/lib-commonjs/index.js.map +1 -1
  186. package/lib-commonjs/navigationModes/cell.js +259 -0
  187. package/lib-commonjs/navigationModes/cell.js.map +1 -0
  188. package/lib-commonjs/navigationModes/composite.js +217 -0
  189. package/lib-commonjs/navigationModes/composite.js.map +1 -0
  190. package/lib-commonjs/navigationModes/index.js +16 -0
  191. package/lib-commonjs/navigationModes/index.js.map +1 -0
  192. package/lib-commonjs/navigationModes/useNavigationMode.js +55 -0
  193. package/lib-commonjs/navigationModes/useNavigationMode.js.map +1 -0
  194. package/package.json +9 -9
  195. package/dist/tsdoc-metadata.json +0 -11
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"../src/","sources":["hooks/types.ts"],"names":[],"mappings":"","sourcesContent":["import { SortDirection } from '../components/Table/Table.types';\n\nexport type RowId = string | number;\nexport type ColumnId = string | number;\nexport type GetRowIdInternal<TItem> = (rowId: TItem, index: number) => RowId;\nexport type SelectionMode = 'single' | 'multiselect';\nexport type OnSelectionChangeCallback = (selectedItems: Set<RowId>) => void;\nexport type OnSortChangeCallback = (state: { sortColumn: ColumnId | undefined; sortDirection: SortDirection }) => void;\n\nexport interface SortState {\n sortColumn: ColumnId | undefined;\n sortDirection: SortDirection;\n}\n\nexport interface ColumnDefinition<TItem> {\n columnId: ColumnId;\n compare?: (a: TItem, b: TItem) => number;\n}\n\nexport type RowEnhancer<TItem, TRowState extends RowState<TItem> = RowState<TItem>> = (\n row: RowState<TItem>,\n state: { selection: TableSelectionState; sort: TableSortState },\n) => TRowState;\n\nexport interface TableSortStateInternal<TItem> {\n sortDirection: SortDirection;\n sortColumn: ColumnId | undefined;\n setColumnSort: (columnId: ColumnId, sortDirection: SortDirection) => void;\n toggleColumnSort: (columnId: ColumnId) => void;\n getSortDirection: (columnId: ColumnId) => SortDirection | undefined;\n /**\n * Returns a sorted **shallow** copy of original items\n */\n sort: (items: TItem[]) => TItem[];\n}\n\nexport interface UseTableOptions<TItem, TRowState extends RowState<TItem> = RowState<TItem>> {\n columns: ColumnDefinition<TItem>[];\n items: TItem[];\n selectionMode?: SelectionMode;\n /**\n * Used in uncontrolled mode to set initial selected rows on mount\n */\n defaultSelectedRows?: Set<RowId>;\n /**\n * Used to control row selection\n */\n selectedRows?: Set<RowId>;\n /**\n * Called when selection changes\n */\n onSelectionChange?: OnSelectionChangeCallback;\n /**\n * Used to control sorting\n */\n sortState?: SortState;\n /**\n * Used in uncontrolled mode to set initial sort column and direction on mount\n */\n defaultSortState?: SortState;\n /**\n * Called when sort changes\n */\n onSortChange?: OnSortChangeCallback;\n getRowId?: (item: TItem) => RowId;\n rowEnhancer?: RowEnhancer<TItem, TRowState>;\n}\n\nexport interface TableSelectionStateInternal {\n clearRows: () => void;\n deselectRow: (rowId: RowId) => void;\n selectRow: (rowId: RowId) => void;\n toggleAllRows: () => void;\n toggleRow: (rowId: RowId) => void;\n isRowSelected: (rowId: RowId) => boolean;\n selectedRows: Set<RowId>;\n allRowsSelected: boolean;\n someRowsSelected: boolean;\n}\n\nexport interface TableSortState {\n /**\n * Current sort direction\n */\n sortDirection: SortDirection;\n /**\n * Column id of the currently sorted column\n */\n sortColumn: ColumnId | undefined;\n /**\n * Set the sort direction for the specified column\n */\n setColumnSort: (columnId: ColumnId, sortDirection: SortDirection) => void;\n /**\n * Toggles the sort direction for specified column\n */\n toggleColumnSort: (columnId: ColumnId) => void;\n /**\n * Returns the sort direction if a column is sorted,\n * returns undefined if the column is not sorted\n */\n getSortDirection: (columnId: ColumnId) => SortDirection | undefined;\n}\n\nexport interface TableSelectionState {\n /**\n * Clears all selected rows\n */\n clearRows: () => void;\n /**\n * Selects single row\n */\n selectRow: (rowId: RowId) => void;\n /**\n * De-selects single row\n */\n deselectRow: (rowId: RowId) => void;\n /**\n * Toggle selection of all rows\n */\n toggleAllRows: () => void;\n /**\n * Toggle selection of single row\n */\n toggleRow: (rowId: RowId) => void;\n /**\n * Collection of row ids corresponding to selected rows\n */\n selectedRows: RowId[];\n /**\n * Whether all rows are selected\n */\n allRowsSelected: boolean;\n /**\n * Whether some rows are selected\n */\n someRowsSelected: boolean;\n\n /**\n * Checks if a given rowId is selected\n */\n isRowSelected: (rowId: RowId) => boolean;\n}\n\nexport interface RowState<TItem> {\n /**\n * User provided data\n */\n item: TItem;\n /**\n * The row id, defaults to index position in the collection\n */\n rowId: RowId;\n}\n\nexport interface TableState<TItem, TRowState extends RowState<TItem> = RowState<TItem>> {\n /**\n * The row data for rendering\n */\n rows: TRowState[];\n /**\n * State and actions to manage row selection\n */\n selection: TableSelectionState;\n /**\n * State and actions to manage row sorting\n */\n sort: TableSortState;\n}\n"]}
@@ -0,0 +1,47 @@
1
+ import * as React from 'react';
2
+ import { useControllableState, useEventCallback } from '@fluentui/react-utilities';
3
+ import { createSelectionManager } from './selectionManager';
4
+ export function useSelection(options) {
5
+ const {
6
+ selectionMode,
7
+ items,
8
+ getRowId,
9
+ defaultSelectedItems,
10
+ selectedItems,
11
+ onSelectionChange
12
+ } = options;
13
+ const [selected, setSelected] = useControllableState({
14
+ initialState: new Set(),
15
+ defaultState: defaultSelectedItems,
16
+ state: selectedItems
17
+ });
18
+ const selectionManager = React.useMemo(() => {
19
+ return createSelectionManager(selectionMode, newSelectedItems => {
20
+ setSelected(() => {
21
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange(newSelectedItems);
22
+ return newSelectedItems;
23
+ });
24
+ });
25
+ }, [onSelectionChange, selectionMode, setSelected]);
26
+ const toggleAllRows = useEventCallback(() => {
27
+ selectionManager.toggleAllItems(items.map((item, i) => getRowId(item, i)), selected);
28
+ });
29
+ const toggleRow = useEventCallback(rowId => selectionManager.toggleItem(rowId, selected));
30
+ const deselectRow = useEventCallback(rowId => selectionManager.deselectItem(rowId, selected));
31
+ const selectRow = useEventCallback(rowId => selectionManager.selectItem(rowId, selected));
32
+
33
+ const isRowSelected = rowId => selectionManager.isSelected(rowId, selected);
34
+
35
+ return {
36
+ someRowsSelected: selected.size > 0,
37
+ allRowsSelected: selectionMode === 'single' ? selected.size > 0 : selected.size === items.length,
38
+ selectedRows: selected,
39
+ toggleRow,
40
+ toggleAllRows,
41
+ clearRows: selectionManager.clearItems,
42
+ deselectRow,
43
+ selectRow,
44
+ isRowSelected
45
+ };
46
+ }
47
+ //# sourceMappingURL=useSelection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["hooks/useSelection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AACA,SAAS,oBAAT,EAA+B,gBAA/B,QAAuD,2BAAvD;AACA,SAAS,sBAAT,QAAuC,oBAAvC;AAkBA,OAAM,SAAU,YAAV,CAA8B,OAA9B,EAAiE;EACrE,MAAM;IAAE,aAAF;IAAiB,KAAjB;IAAwB,QAAxB;IAAkC,oBAAlC;IAAwD,aAAxD;IAAuE;EAAvE,IAA6F,OAAnG;EAEA,MAAM,CAAC,QAAD,EAAW,WAAX,IAA0B,oBAAoB,CAAC;IACnD,YAAY,EAAE,IAAI,GAAJ,EADqC;IAEnD,YAAY,EAAE,oBAFqC;IAGnD,KAAK,EAAE;EAH4C,CAAD,CAApD;EAMA,MAAM,gBAAgB,GAAG,KAAK,CAAC,OAAN,CAAc,MAAK;IAC1C,OAAO,sBAAsB,CAAC,aAAD,EAAgB,gBAAgB,IAAG;MAC9D,WAAW,CAAC,MAAK;QACf,iBAAiB,KAAA,IAAjB,IAAA,iBAAiB,KAAA,KAAA,CAAjB,GAAiB,KAAA,CAAjB,GAAA,iBAAiB,CAAG,gBAAH,CAAjB;QACA,OAAO,gBAAP;MACD,CAHU,CAAX;IAID,CAL4B,CAA7B;EAMD,CAPwB,EAOtB,CAAC,iBAAD,EAAoB,aAApB,EAAmC,WAAnC,CAPsB,CAAzB;EASA,MAAM,aAAa,GAAiD,gBAAgB,CAAC,MAAK;IACxF,gBAAgB,CAAC,cAAjB,CACE,KAAK,CAAC,GAAN,CAAU,CAAC,IAAD,EAAO,CAAP,KAAa,QAAQ,CAAC,IAAD,EAAO,CAAP,CAA/B,CADF,EAEE,QAFF;EAID,CALmF,CAApF;EAOA,MAAM,SAAS,GAA6C,gBAAgB,CAAE,KAAD,IAC3E,gBAAgB,CAAC,UAAjB,CAA4B,KAA5B,EAAmC,QAAnC,CAD0E,CAA5E;EAIA,MAAM,WAAW,GAA+C,gBAAgB,CAAE,KAAD,IAC/E,gBAAgB,CAAC,YAAjB,CAA8B,KAA9B,EAAqC,QAArC,CAD8E,CAAhF;EAIA,MAAM,SAAS,GAA6C,gBAAgB,CAAE,KAAD,IAC3E,gBAAgB,CAAC,UAAjB,CAA4B,KAA5B,EAAmC,QAAnC,CAD0E,CAA5E;;EAIA,MAAM,aAAa,GAAkD,KAAD,IAClE,gBAAgB,CAAC,UAAjB,CAA4B,KAA5B,EAAmC,QAAnC,CADF;;EAGA,OAAO;IACL,gBAAgB,EAAE,QAAQ,CAAC,IAAT,GAAgB,CAD7B;IAEL,eAAe,EAAE,aAAa,KAAK,QAAlB,GAA6B,QAAQ,CAAC,IAAT,GAAgB,CAA7C,GAAiD,QAAQ,CAAC,IAAT,KAAkB,KAAK,CAAC,MAFrF;IAGL,YAAY,EAAE,QAHT;IAIL,SAJK;IAKL,aALK;IAML,SAAS,EAAE,gBAAgB,CAAC,UANvB;IAOL,WAPK;IAQL,SARK;IASL;EATK,CAAP;AAWD","sourcesContent":["import * as React from 'react';\nimport { useControllableState, useEventCallback } from '@fluentui/react-utilities';\nimport { createSelectionManager } from './selectionManager';\nimport type {\n GetRowIdInternal,\n OnSelectionChangeCallback,\n RowId,\n SelectionMode,\n TableSelectionStateInternal,\n} from './types';\n\ninterface UseSelectionOptions<TItem> {\n selectionMode: SelectionMode;\n items: TItem[];\n getRowId: GetRowIdInternal<TItem>;\n defaultSelectedItems?: Set<RowId>;\n selectedItems?: Set<RowId>;\n onSelectionChange?: OnSelectionChangeCallback;\n}\n\nexport function useSelection<TItem>(options: UseSelectionOptions<TItem>): TableSelectionStateInternal {\n const { selectionMode, items, getRowId, defaultSelectedItems, selectedItems, onSelectionChange } = options;\n\n const [selected, setSelected] = useControllableState({\n initialState: new Set<RowId>(),\n defaultState: defaultSelectedItems,\n state: selectedItems,\n });\n\n const selectionManager = React.useMemo(() => {\n return createSelectionManager(selectionMode, newSelectedItems => {\n setSelected(() => {\n onSelectionChange?.(newSelectedItems);\n return newSelectedItems;\n });\n });\n }, [onSelectionChange, selectionMode, setSelected]);\n\n const toggleAllRows: TableSelectionStateInternal['toggleAllRows'] = useEventCallback(() => {\n selectionManager.toggleAllItems(\n items.map((item, i) => getRowId(item, i)),\n selected,\n );\n });\n\n const toggleRow: TableSelectionStateInternal['toggleRow'] = useEventCallback((rowId: RowId) =>\n selectionManager.toggleItem(rowId, selected),\n );\n\n const deselectRow: TableSelectionStateInternal['deselectRow'] = useEventCallback((rowId: RowId) =>\n selectionManager.deselectItem(rowId, selected),\n );\n\n const selectRow: TableSelectionStateInternal['selectRow'] = useEventCallback((rowId: RowId) =>\n selectionManager.selectItem(rowId, selected),\n );\n\n const isRowSelected: TableSelectionStateInternal['isRowSelected'] = (rowId: RowId) =>\n selectionManager.isSelected(rowId, selected);\n\n return {\n someRowsSelected: selected.size > 0,\n allRowsSelected: selectionMode === 'single' ? selected.size > 0 : selected.size === items.length,\n selectedRows: selected,\n toggleRow,\n toggleAllRows,\n clearRows: selectionManager.clearItems,\n deselectRow,\n selectRow,\n isRowSelected,\n };\n}\n"],"sourceRoot":"../src/"}
@@ -0,0 +1,72 @@
1
+ import { useControllableState } from '@fluentui/react-utilities';
2
+ export function useSort(options) {
3
+ const {
4
+ columns,
5
+ sortState,
6
+ defaultSortState,
7
+ onSortChange
8
+ } = options;
9
+ const [sorted, setSorted] = useControllableState({
10
+ initialState: {
11
+ sortDirection: 'ascending',
12
+ sortColumn: undefined
13
+ },
14
+ defaultState: defaultSortState,
15
+ state: sortState
16
+ });
17
+ const {
18
+ sortColumn,
19
+ sortDirection
20
+ } = sorted;
21
+
22
+ const toggleColumnSort = columnId => {
23
+ setSorted(s => {
24
+ const newState = { ...s,
25
+ sortColumn: columnId
26
+ };
27
+
28
+ if (s.sortColumn === columnId) {
29
+ newState.sortDirection = s.sortDirection === 'ascending' ? 'descending' : 'ascending';
30
+ } else {
31
+ newState.sortDirection = 'ascending';
32
+ }
33
+
34
+ onSortChange === null || onSortChange === void 0 ? void 0 : onSortChange(newState);
35
+ return newState;
36
+ });
37
+ };
38
+
39
+ const setColumnSort = (nextSortColumn, nextSortDirection) => {
40
+ const newState = {
41
+ sortColumn: nextSortColumn,
42
+ sortDirection: nextSortDirection
43
+ };
44
+ onSortChange === null || onSortChange === void 0 ? void 0 : onSortChange(newState);
45
+ setSorted(newState);
46
+ };
47
+
48
+ const sort = items => items.slice().sort((a, b) => {
49
+ const sortColumnDef = columns.find(column => column.columnId === sortColumn);
50
+
51
+ if (!(sortColumnDef === null || sortColumnDef === void 0 ? void 0 : sortColumnDef.compare)) {
52
+ return 0;
53
+ }
54
+
55
+ const mod = sortDirection === 'ascending' ? 1 : -1;
56
+ return sortColumnDef.compare(a, b) * mod;
57
+ });
58
+
59
+ const getSortDirection = columnId => {
60
+ return sortColumn === columnId ? sortDirection : undefined;
61
+ };
62
+
63
+ return {
64
+ sortColumn,
65
+ sortDirection,
66
+ sort,
67
+ setColumnSort,
68
+ toggleColumnSort,
69
+ getSortDirection
70
+ };
71
+ }
72
+ //# sourceMappingURL=useSort.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["hooks/useSort.ts"],"names":[],"mappings":"AAAA,SAAS,oBAAT,QAAqC,2BAArC;AAUA,OAAM,SAAU,OAAV,CAAyB,OAAzB,EAAuD;EAC3D,MAAM;IAAE,OAAF;IAAW,SAAX;IAAsB,gBAAtB;IAAwC;EAAxC,IAAyD,OAA/D;EAEA,MAAM,CAAC,MAAD,EAAS,SAAT,IAAsB,oBAAoB,CAAY;IAC1D,YAAY,EAAE;MACZ,aAAa,EAAE,WADH;MAEZ,UAAU,EAAE;IAFA,CAD4C;IAK1D,YAAY,EAA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B,OAAO,CAAP;IACD;;IAED,MAAM,GAAG,GAAG,aAAa,KAAK,WAAlB,GAAgC,CAAhC,GAAoC,CAAC,CAAjD;IACA,OAAO,aAAa,CAAC,OAAd,CAAsB,CAAtB,EAAyB,CAAzB,IAA8B,GAArC;EACD,CARD,CADF;;EAWA,MAAM,gBAAgB,GAAuD,QAAD,IAAuB;IACjG,OAAO,UAAU,KAAK,QAAf,GAA0B,aAA1B,GAA0C,SAAjD;EACD,CAFD;;EAIA,OAAO;IACL,UADK;IAEL,aAFK;IAGL,IAHK;IAIL,aAJK;IAKL,gBALK;IAML;EANK,CAAP;AAQD","sourcesContent":["import { useControllableState } from '@fluentui/react-utilities';\nimport type { ColumnDefinition, ColumnId, OnSortChangeCallback, SortState, TableSortStateInternal } from './types';\n\ninterface UseSortOptions<TItem> {\n columns: ColumnDefinition<TItem>[];\n sortState?: SortState;\n defaultSortState?: SortState;\n onSortChange?: OnSortChangeCallback;\n}\n\nexport function useSort<TItem>(options: UseSortOptions<TItem>): TableSortStateInternal<TItem> {\n const { columns, sortState, defaultSortState, onSortChange } = options;\n\n const [sorted, setSorted] = useControllableState<SortState>({\n initialState: {\n sortDirection: 'ascending' as const,\n sortColumn: undefined,\n },\n defaultState: defaultSortState,\n state: sortState,\n });\n\n const { sortColumn, sortDirection } = sorted;\n\n const toggleColumnSort = (columnId: ColumnId | undefined) => {\n setSorted(s => {\n const newState = { ...s, sortColumn: columnId };\n if (s.sortColumn === columnId) {\n newState.sortDirection = s.sortDirection === 'ascending' ? 'descending' : 'ascending';\n } else {\n newState.sortDirection = 'ascending';\n }\n\n onSortChange?.(newState);\n return newState;\n });\n };\n\n const setColumnSort: TableSortStateInternal<TItem>['setColumnSort'] = (nextSortColumn, nextSortDirection) => {\n const newState = { sortColumn: nextSortColumn, sortDirection: nextSortDirection };\n onSortChange?.(newState);\n setSorted(newState);\n };\n\n const sort = (items: TItem[]) =>\n items.slice().sort((a, b) => {\n const sortColumnDef = columns.find(column => column.columnId === sortColumn);\n if (!sortColumnDef?.compare) {\n return 0;\n }\n\n const mod = sortDirection === 'ascending' ? 1 : -1;\n return sortColumnDef.compare(a, b) * mod;\n });\n\n const getSortDirection: TableSortStateInternal<TItem>['getSortDirection'] = (columnId: ColumnId) => {\n return sortColumn === columnId ? sortDirection : undefined;\n };\n\n return {\n sortColumn,\n sortDirection,\n sort,\n setColumnSort,\n toggleColumnSort,\n getSortDirection,\n };\n}\n"],"sourceRoot":"../src/"}
@@ -0,0 +1,87 @@
1
+ import * as React from 'react';
2
+ import { useSelection } from './useSelection';
3
+ import { useSort } from './useSort';
4
+ export function useTable(options) {
5
+ const {
6
+ items: baseItems,
7
+ columns,
8
+ getRowId: getUserRowId = () => undefined,
9
+ selectionMode = 'multiselect',
10
+ rowEnhancer = row => row,
11
+ defaultSelectedRows,
12
+ selectedRows: userSelectedRows,
13
+ onSelectionChange,
14
+ sortState: userSortState,
15
+ defaultSortState,
16
+ onSortChange
17
+ } = options;
18
+ const getRowId = React.useCallback((item, index) => {
19
+ var _a;
20
+
21
+ return (_a = getUserRowId(item)) !== null && _a !== void 0 ? _a : index;
22
+ }, [getUserRowId]);
23
+ const {
24
+ sortColumn,
25
+ sortDirection,
26
+ toggleColumnSort,
27
+ setColumnSort,
28
+ getSortDirection,
29
+ sort
30
+ } = useSort({
31
+ columns,
32
+ sortState: userSortState,
33
+ defaultSortState,
34
+ onSortChange
35
+ });
36
+ const sortState = React.useMemo(() => ({
37
+ sortColumn,
38
+ sortDirection,
39
+ setColumnSort,
40
+ toggleColumnSort,
41
+ getSortDirection
42
+ }), [sortColumn, sortDirection, setColumnSort, toggleColumnSort, getSortDirection]);
43
+ const {
44
+ isRowSelected,
45
+ toggleRow,
46
+ toggleAllRows,
47
+ clearRows,
48
+ selectedRows,
49
+ allRowsSelected,
50
+ someRowsSelected,
51
+ selectRow,
52
+ deselectRow
53
+ } = useSelection({
54
+ selectionMode,
55
+ items: baseItems,
56
+ getRowId,
57
+ defaultSelectedItems: defaultSelectedRows,
58
+ selectedItems: userSelectedRows,
59
+ onSelectionChange
60
+ });
61
+ const selectionState = React.useMemo(() => ({
62
+ isRowSelected,
63
+ clearRows,
64
+ deselectRow,
65
+ selectRow,
66
+ toggleAllRows,
67
+ toggleRow,
68
+ selectedRows: Array.from(selectedRows),
69
+ allRowsSelected,
70
+ someRowsSelected
71
+ }), [isRowSelected, clearRows, deselectRow, selectRow, toggleAllRows, toggleRow, selectedRows, allRowsSelected, someRowsSelected]);
72
+ const rows = React.useMemo(() => sort(baseItems).map((item, i) => {
73
+ return rowEnhancer({
74
+ item,
75
+ rowId: getRowId(item, i)
76
+ }, {
77
+ selection: selectionState,
78
+ sort: sortState
79
+ });
80
+ }), [baseItems, getRowId, sort, rowEnhancer, selectionState, sortState]);
81
+ return {
82
+ rows,
83
+ selection: selectionState,
84
+ sort: sortState
85
+ };
86
+ }
87
+ //# sourceMappingURL=useTable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["hooks/useTable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AASA,SAAS,YAAT,QAA6B,gBAA7B;AACA,SAAS,OAAT,QAAwB,WAAxB;AAEA,OAAM,SAAU,QAAV,CACJ,OADI,EACsC;EAE1C,MAAM;IACJ,KAAK,EAAE,SADH;IAEJ,OAFI;IAGJ,QAAQ,EAAE,YAAY,GAAG,MAAM,SAH3B;IAIJ,aAAa,GAAG,aAJZ;IAKJ,WAAW,GAAI,GAAD,IAA0B,GALpC;IAMJ,mBANI;IAOJ,YAAY,EAAE,gBAPV;IAQJ,iBARI;IASJ,SAAS,EAAE,aATP;IAUJ,gBAVI;IAWJ;EAXI,IAYF,OAZJ;EAcA,MAAM,QAAQ,GAA4B,KAAK,CAAC,WAAN,CACxC,CAAC,IAAD,EAAc,KAAd,KAA+B;IAAA,IAAA,EAAA;;IAAC,OAAA,CAAA,EAAA,GAAA,YAAY,CAAC,IAAD,CAAZ,MAAkB,IAAlB,IAAkB,EAA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AAY;IAC9B,OAAO,WAAW,CAChB;MACE,IADF;MAEE,KAAK,EAAE,QAAQ,CAAC,IAAD,EAAO,CAAP;IAFjB,CADgB,EAKhB;MAAE,SAAS,EAAE,cAAb;MAA6B,IAAI,EAAE;IAAnC,CALgB,CAAlB;EAOD,CARD,CAFS,EAWX,CAAC,SAAD,EAAY,QAAZ,EAAsB,IAAtB,EAA4B,WAA5B,EAAyC,cAAzC,EAAyD,SAAzD,CAXW,CAAb;EAcA,OAAO;IACL,IADK;IAEL,SAAS,EAAE,cAFN;IAGL,IAAI,EAAE;EAHD,CAAP;AAKD","sourcesContent":["import * as React from 'react';\nimport type {\n UseTableOptions,\n TableState,\n RowState,\n TableSelectionState,\n TableSortState,\n GetRowIdInternal,\n} from './types';\nimport { useSelection } from './useSelection';\nimport { useSort } from './useSort';\n\nexport function useTable<TItem, TRowState extends RowState<TItem> = RowState<TItem>>(\n options: UseTableOptions<TItem, TRowState>,\n): TableState<TItem, TRowState> {\n const {\n items: baseItems,\n columns,\n getRowId: getUserRowId = () => undefined,\n selectionMode = 'multiselect',\n rowEnhancer = (row: RowState<TItem>) => row as TRowState,\n defaultSelectedRows,\n selectedRows: userSelectedRows,\n onSelectionChange,\n sortState: userSortState,\n defaultSortState,\n onSortChange,\n } = options;\n\n const getRowId: GetRowIdInternal<TItem> = React.useCallback(\n (item: TItem, index: number) => getUserRowId(item) ?? index,\n [getUserRowId],\n );\n const { sortColumn, sortDirection, toggleColumnSort, setColumnSort, getSortDirection, sort } = useSort({\n columns,\n sortState: userSortState,\n defaultSortState,\n onSortChange,\n });\n const sortState: TableSortState = React.useMemo(\n () => ({\n sortColumn,\n sortDirection,\n setColumnSort,\n toggleColumnSort,\n getSortDirection,\n }),\n [sortColumn, sortDirection, setColumnSort, toggleColumnSort, getSortDirection],\n );\n\n const {\n isRowSelected,\n toggleRow,\n toggleAllRows,\n clearRows,\n selectedRows,\n allRowsSelected,\n someRowsSelected,\n selectRow,\n deselectRow,\n } = useSelection({\n selectionMode,\n items: baseItems,\n getRowId,\n defaultSelectedItems: defaultSelectedRows,\n selectedItems: userSelectedRows,\n onSelectionChange,\n });\n\n const selectionState: TableSelectionState = React.useMemo(\n () => ({\n isRowSelected,\n clearRows,\n deselectRow,\n selectRow,\n toggleAllRows,\n toggleRow,\n selectedRows: Array.from(selectedRows),\n allRowsSelected,\n someRowsSelected,\n }),\n [\n isRowSelected,\n clearRows,\n deselectRow,\n selectRow,\n toggleAllRows,\n toggleRow,\n selectedRows,\n allRowsSelected,\n someRowsSelected,\n ],\n );\n\n const rows = React.useMemo(\n () =>\n sort(baseItems).map((item, i) => {\n return rowEnhancer(\n {\n item,\n rowId: getRowId(item, i),\n },\n { selection: selectionState, sort: sortState },\n );\n }),\n [baseItems, getRowId, sort, rowEnhancer, selectionState, sortState],\n );\n\n return {\n rows,\n selection: selectionState,\n sort: sortState,\n };\n}\n"],"sourceRoot":"../src/"}
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ export { useTable } from './hooks';
1
2
  export { TableCell, tableCellClassNames, tableCellClassName, useTableCellStyles_unstable, useTableCell_unstable, renderTableCell_unstable } from './TableCell';
2
3
  export { TableRow, tableRowClassNames, tableRowClassName, useTableRowStyles_unstable, useTableRow_unstable, renderTableRow_unstable } from './TableRow';
3
4
  export { TableBody, tableBodyClassName, tableBodyClassNames, useTableBodyStyles_unstable, useTableBody_unstable, renderTableBody_unstable } from './TableBody';
@@ -5,4 +6,7 @@ export { Table, tableClassName, tableClassNames, useTableStyles_unstable, useTab
5
6
  export { TableHeader, tableHeaderClassNames, tableHeaderClassName, useTableHeaderStyles_unstable, useTableHeader_unstable, renderTableHeader_unstable } from './TableHeader';
6
7
  export { TableHeaderCell, tableHeaderCellClassName, tableHeaderCellClassNames, useTableHeaderCellStyles_unstable, useTableHeaderCell_unstable, renderTableHeaderCell_unstable } from './TableHeaderCell';
7
8
  export { TableContextProvider, useTableContext } from './contexts/tableContext';
9
+ export { TableSelectionCell, useTableSelectionCellStyles_unstable, useTableSelectionCell_unstable, renderTableSelectionCell_unstable, tableSelectionCellClassNames } from './TableSelectionCell';
10
+ export { TableCellActions, tableCellActionsClassNames, useTableCellActionsStyles_unstable, useTableCellActions_unstable, renderTableCellActions_unstable } from './TableCellActions';
11
+ export { TableCellLayout, tableCellLayoutClassNames, useTableCellLayoutStyles_unstable, useTableCellLayout_unstable, renderTableCellLayout_unstable } from './TableCellLayout';
8
12
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["index.ts"],"names":[],"mappings":"AAAA,SACE,SADF,EAEE,mBAFF,EAGE,kBAHF,EAIE,2BAJF,EAKE,qBALF,EAME,wBANF,QAOO,aAPP;AAUA,SACE,QADF,EAEE,kBAFF,EAGE,iBAHF,EAIE,0BAJF,EAKE,oBALF,EAME,uBANF,QAOO,YAPP;AAUA,SACE,SADF,EAEE,kBAFF,EAGE,mBAHF,EAIE,2BAJF,EAKE,qBALF,EAME,wBANF,QAOO,aAPP;AAUA,SACE,KADF,EAEE,cAFF,EAGE,eAHF,EAIE,uBAJF,EAKE,iBALF,EAME,oBANF,QAOO,SAPP;AAUA,SACE,WADF,EAEE,qBAFF,EAGE,oBAHF,EAIE,6BAJF,EAKE,uBALF,EAME,0BANF,QAOO,eAPP;AAUA,SACE,eADF,EAEE,wBAFF,EAGE,yBAHF,EAIE,iCAJF,EAKE,2BALF,EAME,8BANF,QAOO,mBAPP;AAUA,SAAS,oBAAT,EAA+B,eAA/B,QAAsD,yBAAtD","sourcesContent":["export {\n TableCell,\n tableCellClassNames,\n tableCellClassName,\n useTableCellStyles_unstable,\n useTableCell_unstable,\n renderTableCell_unstable,\n} from './TableCell';\nexport type { TableCellProps, TableCellState, TableCellSlots } from './TableCell';\n\nexport {\n TableRow,\n tableRowClassNames,\n tableRowClassName,\n useTableRowStyles_unstable,\n useTableRow_unstable,\n renderTableRow_unstable,\n} from './TableRow';\nexport type { TableRowProps, TableRowState, TableRowSlots } from './TableRow';\n\nexport {\n TableBody,\n tableBodyClassName,\n tableBodyClassNames,\n useTableBodyStyles_unstable,\n useTableBody_unstable,\n renderTableBody_unstable,\n} from './TableBody';\nexport type { TableBodyProps, TableBodyState, TableBodySlots } from './TableBody';\n\nexport {\n Table,\n tableClassName,\n tableClassNames,\n useTableStyles_unstable,\n useTable_unstable,\n renderTable_unstable,\n} from './Table';\nexport type { TableProps, TableSlots, TableState, TableContextValue, TableContextValues, SortDirection } from './Table';\n\nexport {\n TableHeader,\n tableHeaderClassNames,\n tableHeaderClassName,\n useTableHeaderStyles_unstable,\n useTableHeader_unstable,\n renderTableHeader_unstable,\n} from './TableHeader';\nexport type { TableHeaderProps, TableHeaderSlots, TableHeaderState } from './TableHeader';\n\nexport {\n TableHeaderCell,\n tableHeaderCellClassName,\n tableHeaderCellClassNames,\n useTableHeaderCellStyles_unstable,\n useTableHeaderCell_unstable,\n renderTableHeaderCell_unstable,\n} from './TableHeaderCell';\nexport type { TableHeaderCellProps, TableHeaderCellSlots, TableHeaderCellState } from './TableHeaderCell';\n\nexport { TableContextProvider, useTableContext } from './contexts/tableContext';\n"],"sourceRoot":"../src/"}
1
+ {"version":3,"sources":["index.ts"],"names":[],"mappings":"AAAA,SAAS,QAAT,QAAyB,SAAzB;AAWA,SACE,SADF,EAEE,mBAFF,EAGE,kBAHF,EAIE,2BAJF,EAKE,qBALF,EAME,wBANF,QAOO,aAPP;AAUA,SACE,QADF,EAEE,kBAFF,EAGE,iBAHF,EAIE,0BAJF,EAKE,oBALF,EAME,uBANF,QAOO,YAPP;AAUA,SACE,SADF,EAEE,kBAFF,EAGE,mBAHF,EAIE,2BAJF,EAKE,qBALF,EAME,wBANF,QAOO,aAPP;AAUA,SACE,KADF,EAEE,cAFF,EAGE,eAHF,EAIE,uBAJF,EAKE,iBALF,EAME,oBANF,QAOO,SAPP;AAUA,SACE,WADF,EAEE,qBAFF,EAGE,oBAHF,EAIE,6BAJF,EAKE,uBALF,EAME,0BANF,QAOO,eAPP;AAUA,SACE,eADF,EAEE,wBAFF,EAGE,yBAHF,EAIE,iCAJF,EAKE,2BALF,EAME,8BANF,QAOO,mBAPP;AAUA,SAAS,oBAAT,EAA+B,eAA/B,QAAsD,yBAAtD;AACA,SACE,kBADF,EAEE,oCAFF,EAGE,8BAHF,EAIE,iCAJF,EAKE,4BALF,QAMO,sBANP;AASA,SACE,gBADF,EAEE,0BAFF,EAGE,kCAHF,EAIE,4BAJF,EAKE,+BALF,QAMO,oBANP;AASA,SACE,eADF,EAEE,yBAFF,EAGE,iCAHF,EAIE,2BAJF,EAKE,8BALF,QAMO,mBANP","sourcesContent":["export { useTable } from './hooks';\nexport type {\n UseTableOptions,\n TableSelectionState,\n TableSortState,\n ColumnDefinition,\n RowState,\n RowId,\n ColumnId,\n} from './hooks';\n\nexport {\n TableCell,\n tableCellClassNames,\n tableCellClassName,\n useTableCellStyles_unstable,\n useTableCell_unstable,\n renderTableCell_unstable,\n} from './TableCell';\nexport type { TableCellProps, TableCellState, TableCellSlots } from './TableCell';\n\nexport {\n TableRow,\n tableRowClassNames,\n tableRowClassName,\n useTableRowStyles_unstable,\n useTableRow_unstable,\n renderTableRow_unstable,\n} from './TableRow';\nexport type { TableRowProps, TableRowState, TableRowSlots } from './TableRow';\n\nexport {\n TableBody,\n tableBodyClassName,\n tableBodyClassNames,\n useTableBodyStyles_unstable,\n useTableBody_unstable,\n renderTableBody_unstable,\n} from './TableBody';\nexport type { TableBodyProps, TableBodyState, TableBodySlots } from './TableBody';\n\nexport {\n Table,\n tableClassName,\n tableClassNames,\n useTableStyles_unstable,\n useTable_unstable,\n renderTable_unstable,\n} from './Table';\nexport type { TableProps, TableSlots, TableState, TableContextValue, TableContextValues, SortDirection } from './Table';\n\nexport {\n TableHeader,\n tableHeaderClassNames,\n tableHeaderClassName,\n useTableHeaderStyles_unstable,\n useTableHeader_unstable,\n renderTableHeader_unstable,\n} from './TableHeader';\nexport type { TableHeaderProps, TableHeaderSlots, TableHeaderState } from './TableHeader';\n\nexport {\n TableHeaderCell,\n tableHeaderCellClassName,\n tableHeaderCellClassNames,\n useTableHeaderCellStyles_unstable,\n useTableHeaderCell_unstable,\n renderTableHeaderCell_unstable,\n} from './TableHeaderCell';\nexport type { TableHeaderCellProps, TableHeaderCellSlots, TableHeaderCellState } from './TableHeaderCell';\n\nexport { TableContextProvider, useTableContext } from './contexts/tableContext';\nexport {\n TableSelectionCell,\n useTableSelectionCellStyles_unstable,\n useTableSelectionCell_unstable,\n renderTableSelectionCell_unstable,\n tableSelectionCellClassNames,\n} from './TableSelectionCell';\n\nexport type { TableSelectionCellProps, TableSelectionCellState, TableSelectionCellSlots } from './TableSelectionCell';\nexport {\n TableCellActions,\n tableCellActionsClassNames,\n useTableCellActionsStyles_unstable,\n useTableCellActions_unstable,\n renderTableCellActions_unstable,\n} from './TableCellActions';\n\nexport type { TableCellActionsProps, TableCellActionsSlots, TableCellActionsState } from './TableCellActions';\nexport {\n TableCellLayout,\n tableCellLayoutClassNames,\n useTableCellLayoutStyles_unstable,\n useTableCellLayout_unstable,\n renderTableCellLayout_unstable,\n} from './TableCellLayout';\nexport type { TableCellLayoutProps, TableCellLayoutSlots, TableCellLayoutState } from './TableCellLayout';\n"],"sourceRoot":"../src/"}
@@ -0,0 +1,250 @@
1
+ export function applyCellNavigation(element) {
2
+ const container = element;
3
+ let column = 0;
4
+ let mode = undefined;
5
+ let movingOut = false;
6
+ const pre = document.createElement('div');
7
+ const post = document.createElement('div');
8
+
9
+ if (!pre || !post) {
10
+ return;
11
+ }
12
+
13
+ pre.tabIndex = 0;
14
+ post.tabIndex = 0;
15
+ pre.addEventListener('focus', () => {
16
+ if (movingOut) {
17
+ movingOut = false;
18
+ return;
19
+ } else {
20
+ treeWalker.currentNode = container;
21
+ const candidate = treeWalker.nextNode();
22
+
23
+ if (isHTMLElement(candidate)) {
24
+ column = 0;
25
+ candidate.focus();
26
+ }
27
+ }
28
+ });
29
+ post.addEventListener('focus', () => {
30
+ if (movingOut) {
31
+ movingOut = false;
32
+ return;
33
+ } else {
34
+ treeWalker.currentNode = post;
35
+ const candidate = treeWalker.previousNode();
36
+
37
+ if (isHTMLElement(candidate)) {
38
+ column = findColumnCount(candidate);
39
+ candidate.focus();
40
+ }
41
+ }
42
+ });
43
+ container.before(pre);
44
+ container.after(post);
45
+
46
+ const acceptNode = node => {
47
+ if (!isHTMLElement(node)) {
48
+ return NodeFilter.FILTER_SKIP;
49
+ }
50
+
51
+ if (mode === 'column') {
52
+ return acceptCell(node);
53
+ }
54
+
55
+ if (mode === 'row') {
56
+ return acceptRow(node);
57
+ }
58
+
59
+ if (node.tabIndex >= 0) {
60
+ return NodeFilter.FILTER_ACCEPT;
61
+ }
62
+
63
+ return NodeFilter.FILTER_SKIP;
64
+ };
65
+
66
+ const acceptCell = node => {
67
+ if (isRow(node)) {
68
+ return NodeFilter.FILTER_REJECT;
69
+ }
70
+
71
+ if (node.tabIndex < 0) {
72
+ return NodeFilter.FILTER_SKIP;
73
+ }
74
+
75
+ return NodeFilter.FILTER_ACCEPT;
76
+ };
77
+
78
+ const acceptRow = node => {
79
+ if (isRow(node)) {
80
+ return NodeFilter.FILTER_ACCEPT;
81
+ }
82
+
83
+ return NodeFilter.FILTER_SKIP;
84
+ };
85
+
86
+ const treeWalker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
87
+ acceptNode
88
+ });
89
+
90
+ const up = current => {
91
+ mode = 'row';
92
+ treeWalker.currentNode = current;
93
+ treeWalker.previousNode(); // current row
94
+
95
+ const row = treeWalker.previousNode();
96
+
97
+ if (!row) {
98
+ return null;
99
+ }
100
+
101
+ mode = 'column';
102
+ treeWalker.currentNode = row;
103
+ let curCol = 0;
104
+ let node = null;
105
+
106
+ while (curCol <= column) {
107
+ node = treeWalker.nextNode();
108
+ curCol++;
109
+ }
110
+
111
+ if (isHTMLElement(node)) {
112
+ return node;
113
+ }
114
+
115
+ return null;
116
+ };
117
+
118
+ const down = current => {
119
+ mode = 'row';
120
+ treeWalker.currentNode = current;
121
+ const row = treeWalker.nextNode();
122
+
123
+ if (!row) {
124
+ return null;
125
+ }
126
+
127
+ mode = 'column';
128
+ treeWalker.currentNode = row;
129
+ let curCol = 0;
130
+ let node = null;
131
+
132
+ while (curCol <= column) {
133
+ node = treeWalker.nextNode();
134
+ curCol++;
135
+ }
136
+
137
+ if (isHTMLElement(node)) {
138
+ return node;
139
+ }
140
+
141
+ return null;
142
+ };
143
+
144
+ const left = current => {
145
+ treeWalker.currentNode = current;
146
+ mode = 'column';
147
+ const node = treeWalker.previousNode();
148
+
149
+ if (isHTMLElement(node)) {
150
+ column--;
151
+ return node;
152
+ }
153
+
154
+ return null;
155
+ };
156
+
157
+ const right = current => {
158
+ mode = 'column';
159
+ treeWalker.currentNode = current;
160
+ const node = treeWalker.nextNode();
161
+
162
+ if (isHTMLElement(node)) {
163
+ column++;
164
+ return node;
165
+ }
166
+
167
+ return null;
168
+ };
169
+
170
+ const onKeyDown = e => {
171
+ const target = e.target;
172
+
173
+ if (!target || !isHTMLElement(target)) {
174
+ return;
175
+ }
176
+
177
+ let next = null;
178
+
179
+ switch (e.key) {
180
+ case 'ArrowDown':
181
+ next = down(target);
182
+ break;
183
+
184
+ case 'ArrowUp':
185
+ next = up(target);
186
+ break;
187
+
188
+ case 'ArrowLeft':
189
+ next = left(target);
190
+ break;
191
+
192
+ case 'ArrowRight':
193
+ next = right(target);
194
+ break;
195
+
196
+ case 'Tab':
197
+ movingOut = true;
198
+
199
+ if (e.shiftKey) {
200
+ pre.focus();
201
+ } else {
202
+ post.focus();
203
+ }
204
+
205
+ break;
206
+
207
+ default:
208
+ return;
209
+ }
210
+
211
+ if (next) {
212
+ e.preventDefault();
213
+ next.focus();
214
+ }
215
+ };
216
+
217
+ const onFocusOut = () => {
218
+ mode = undefined;
219
+ };
220
+
221
+ container.addEventListener('keydown', onKeyDown);
222
+ container.addEventListener('focusout', onFocusOut);
223
+ return () => {
224
+ container.removeEventListener('focusout', onFocusOut);
225
+ container.removeEventListener('keydown', onKeyDown);
226
+ };
227
+ }
228
+
229
+ function isHTMLElement(node) {
230
+ return node instanceof HTMLElement;
231
+ }
232
+
233
+ function isRow(element) {
234
+ if (element.getAttribute('role') === 'row' || element.tagName === 'TR') {
235
+ return true;
236
+ }
237
+
238
+ return false;
239
+ }
240
+
241
+ function findColumnCount(node) {
242
+ let cur = node;
243
+
244
+ while (cur.parentElement && !isRow(cur)) {
245
+ cur = cur.parentElement;
246
+ }
247
+
248
+ return cur.querySelectorAll('[role="cell"], [role="gridcell"], td').length - 1;
249
+ }
250
+ //# sourceMappingURL=cell.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["navigationModes/cell.ts"],"names":[],"mappings":"AAAA,OAAM,SAAU,mBAAV,CAA8B,OAA9B,EAAkD;EACtD,MAAM,SAAS,GAAG,OAAlB;EACA,IAAI,MAAM,GAAG,CAAb;EACA,IAAI,IAAI,GAAiC,SAAzC;EACA,IAAI,SAAS,GAAG,KAAhB;EAEA,MAAM,GAAG,GAAG,QAAQ,CAAC,aAAT,CAAuB,KAAvB,CAAZ;EACA,MAAM,IAAI,GAAG,QAAQ,CAAC,aAAT,CAAuB,KAAvB,CAAb;;EAEA,IAAI,CAAC,GAAD,IAAQ,CAAC,IAAb,EAAmB;IACjB;EACD;;EAED,GAAG,CAAC,QAAJ,GAAe,CAAf;EACA,IAAI,CAAC,QAAL,GAAgB,CAAhB;EAEA,GAAG,CAAC,gBAAJ,CAAqB,OAArB,EAA8B,MAAK;IACjC,IAAI,SAAJ,EAAe;MACb,SAAS,GAAG,KAAZ;MACA;IACD,CAHD,MAGO;MACL,UAAU,CAAC,WAAX,GAAyB,SAAzB;MACA,MAAM,SAAS,GAAG,UAAU,CAAC,QAAX,EAAlB;;MACA,IAAI,aAAa,CAAC,SAAD,CAAjB,EAA8B;QAC5B,MAAM,GAAG,CAAT;QACA,SAAS,CAAC,KAAV;MACD;IACF;EACF,CAZD;EAcA,IAAI,CAAC,gBAAL,CAAsB,OAAt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EACD,CAFD;;EAIA,SAAS,CAAC,gBAAV,CAA2B,SAA3B,EAAsC,SAAtC;EACA,SAAS,CAAC,gBAAV,CAA2B,UAA3B,EAAuC,UAAvC;EACA,OAAO,MAAK;IACV,SAAS,CAAC,mBAAV,CAA8B,UAA9B,EAA0C,UAA1C;IACA,SAAS,CAAC,mBAAV,CAA8B,SAA9B,EAAyC,SAAzC;EACD,CAHD;AAID;;AAED,SAAS,aAAT,CAAuB,IAAvB,EAAoC;EAClC,OAAO,IAAI,YAAY,WAAvB;AACD;;AAED,SAAS,KAAT,CAAe,OAAf,EAAmC;EACjC,IAAI,OAAO,CAAC,YAAR,CAAqB,MAArB,MAAiC,KAAjC,IAA0C,OAAO,CAAC,OAAR,KAAoB,IAAlE,EAAwE;IACtE,OAAO,IAAP;EACD;;EAED,OAAO,KAAP;AACD;;AAED,SAAS,eAAT,CAAyB,IAAzB,EAA0C;EACxC,IAAI,GAAG,GAAG,IAAV;;EACA,OAAO,GAAG,CAAC,aAAJ,IAAqB,CAAC,KAAK,CAAC,GAAD,CAAlC,EAAyC;IACvC,GAAG,GAAG,GAAG,CAAC,aAAV;EACD;;EAED,OAAO,GAAG,CAAC,gBAAJ,CAAqB,sCAArB,EAA6D,MAA7D,GAAsE,CAA7E;AACD","sourcesContent":["export function applyCellNavigation(element: HTMLElement) {\n const container = element;\n let column = 0;\n let mode: 'row' | 'column' | undefined = undefined;\n let movingOut = false;\n\n const pre = document.createElement('div');\n const post = document.createElement('div');\n\n if (!pre || !post) {\n return;\n }\n\n pre.tabIndex = 0;\n post.tabIndex = 0;\n\n pre.addEventListener('focus', () => {\n if (movingOut) {\n movingOut = false;\n return;\n } else {\n treeWalker.currentNode = container;\n const candidate = treeWalker.nextNode();\n if (isHTMLElement(candidate)) {\n column = 0;\n candidate.focus();\n }\n }\n });\n\n post.addEventListener('focus', () => {\n if (movingOut) {\n movingOut = false;\n return;\n } else {\n treeWalker.currentNode = post;\n const candidate = treeWalker.previousNode();\n if (isHTMLElement(candidate)) {\n column = findColumnCount(candidate);\n candidate.focus();\n }\n }\n });\n\n container.before(pre);\n container.after(post);\n\n const acceptNode = (node: Node) => {\n if (!isHTMLElement(node)) {\n return NodeFilter.FILTER_SKIP;\n }\n\n if (mode === 'column') {\n return acceptCell(node);\n }\n\n if (mode === 'row') {\n return acceptRow(node);\n }\n\n if (node.tabIndex >= 0) {\n return NodeFilter.FILTER_ACCEPT;\n }\n\n return NodeFilter.FILTER_SKIP;\n };\n\n const acceptCell = (node: HTMLElement) => {\n if (isRow(node)) {\n return NodeFilter.FILTER_REJECT;\n }\n\n if (node.tabIndex < 0) {\n return NodeFilter.FILTER_SKIP;\n }\n\n return NodeFilter.FILTER_ACCEPT;\n };\n\n const acceptRow = (node: HTMLElement) => {\n if (isRow(node)) {\n return NodeFilter.FILTER_ACCEPT;\n }\n\n return NodeFilter.FILTER_SKIP;\n };\n\n const treeWalker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, { acceptNode });\n\n const up = (current: HTMLElement) => {\n mode = 'row';\n treeWalker.currentNode = current;\n treeWalker.previousNode(); // current row\n const row = treeWalker.previousNode();\n if (!row) {\n return null;\n }\n\n mode = 'column';\n treeWalker.currentNode = row;\n let curCol = 0;\n let node: Node | null = null;\n while (curCol <= column) {\n node = treeWalker.nextNode();\n curCol++;\n }\n if (isHTMLElement(node)) {\n return node;\n }\n return null;\n };\n\n const down = (current: HTMLElement) => {\n mode = 'row';\n treeWalker.currentNode = current;\n const row = treeWalker.nextNode();\n if (!row) {\n return null;\n }\n\n mode = 'column';\n treeWalker.currentNode = row;\n let curCol = 0;\n let node: Node | null = null;\n while (curCol <= column) {\n node = treeWalker.nextNode();\n curCol++;\n }\n if (isHTMLElement(node)) {\n return node;\n }\n return null;\n };\n\n const left = (current: HTMLElement) => {\n treeWalker.currentNode = current;\n mode = 'column';\n const node = treeWalker.previousNode();\n\n if (isHTMLElement(node)) {\n column--;\n return node;\n }\n return null;\n };\n\n const right = (current: HTMLElement) => {\n mode = 'column';\n treeWalker.currentNode = current;\n const node = treeWalker.nextNode();\n if (isHTMLElement(node)) {\n column++;\n return node;\n }\n return null;\n };\n\n const onKeyDown = (e: KeyboardEvent) => {\n const target = e.target;\n if (!target || !isHTMLElement(target)) {\n return;\n }\n\n let next: HTMLElement | null = null;\n\n switch (e.key) {\n case 'ArrowDown':\n next = down(target);\n break;\n case 'ArrowUp':\n next = up(target);\n break;\n case 'ArrowLeft':\n next = left(target);\n break;\n case 'ArrowRight':\n next = right(target);\n break;\n case 'Tab':\n movingOut = true;\n if (e.shiftKey) {\n pre.focus();\n } else {\n post.focus();\n }\n\n break;\n default:\n return;\n }\n\n if (next) {\n e.preventDefault();\n next.focus();\n }\n };\n\n const onFocusOut = () => {\n mode = undefined;\n };\n\n container.addEventListener('keydown', onKeyDown);\n container.addEventListener('focusout', onFocusOut);\n return () => {\n container.removeEventListener('focusout', onFocusOut);\n container.removeEventListener('keydown', onKeyDown);\n };\n}\n\nfunction isHTMLElement(node: unknown): node is HTMLElement {\n return node instanceof HTMLElement;\n}\n\nfunction isRow(element: HTMLElement) {\n if (element.getAttribute('role') === 'row' || element.tagName === 'TR') {\n return true;\n }\n\n return false;\n}\n\nfunction findColumnCount(node: HTMLElement) {\n let cur = node;\n while (cur.parentElement && !isRow(cur)) {\n cur = cur.parentElement;\n }\n\n return cur.querySelectorAll('[role=\"cell\"], [role=\"gridcell\"], td').length - 1;\n}\n"],"sourceRoot":"../src/"}