@react-aria/table 3.0.0-beta.1 → 3.0.0-nightly-641446f65-240905

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 (239) hide show
  1. package/dist/TableKeyboardDelegate.main.js +132 -0
  2. package/dist/TableKeyboardDelegate.main.js.map +1 -0
  3. package/dist/TableKeyboardDelegate.mjs +127 -0
  4. package/dist/TableKeyboardDelegate.module.js +127 -0
  5. package/dist/TableKeyboardDelegate.module.js.map +1 -0
  6. package/dist/ar-AE.main.js +14 -0
  7. package/dist/ar-AE.main.js.map +1 -0
  8. package/dist/ar-AE.mjs +16 -0
  9. package/dist/ar-AE.module.js +16 -0
  10. package/dist/ar-AE.module.js.map +1 -0
  11. package/dist/bg-BG.main.js +14 -0
  12. package/dist/bg-BG.main.js.map +1 -0
  13. package/dist/bg-BG.mjs +16 -0
  14. package/dist/bg-BG.module.js +16 -0
  15. package/dist/bg-BG.module.js.map +1 -0
  16. package/dist/cs-CZ.main.js +14 -0
  17. package/dist/cs-CZ.main.js.map +1 -0
  18. package/dist/cs-CZ.mjs +16 -0
  19. package/dist/cs-CZ.module.js +16 -0
  20. package/dist/cs-CZ.module.js.map +1 -0
  21. package/dist/da-DK.main.js +14 -0
  22. package/dist/da-DK.main.js.map +1 -0
  23. package/dist/da-DK.mjs +16 -0
  24. package/dist/da-DK.module.js +16 -0
  25. package/dist/da-DK.module.js.map +1 -0
  26. package/dist/de-DE.main.js +14 -0
  27. package/dist/de-DE.main.js.map +1 -0
  28. package/dist/de-DE.mjs +16 -0
  29. package/dist/de-DE.module.js +16 -0
  30. package/dist/de-DE.module.js.map +1 -0
  31. package/dist/el-GR.main.js +14 -0
  32. package/dist/el-GR.main.js.map +1 -0
  33. package/dist/el-GR.mjs +16 -0
  34. package/dist/el-GR.module.js +16 -0
  35. package/dist/el-GR.module.js.map +1 -0
  36. package/dist/en-US.main.js +14 -0
  37. package/dist/en-US.main.js.map +1 -0
  38. package/dist/en-US.mjs +16 -0
  39. package/dist/en-US.module.js +16 -0
  40. package/dist/en-US.module.js.map +1 -0
  41. package/dist/es-ES.main.js +14 -0
  42. package/dist/es-ES.main.js.map +1 -0
  43. package/dist/es-ES.mjs +16 -0
  44. package/dist/es-ES.module.js +16 -0
  45. package/dist/es-ES.module.js.map +1 -0
  46. package/dist/et-EE.main.js +14 -0
  47. package/dist/et-EE.main.js.map +1 -0
  48. package/dist/et-EE.mjs +16 -0
  49. package/dist/et-EE.module.js +16 -0
  50. package/dist/et-EE.module.js.map +1 -0
  51. package/dist/fi-FI.main.js +14 -0
  52. package/dist/fi-FI.main.js.map +1 -0
  53. package/dist/fi-FI.mjs +16 -0
  54. package/dist/fi-FI.module.js +16 -0
  55. package/dist/fi-FI.module.js.map +1 -0
  56. package/dist/fr-FR.main.js +14 -0
  57. package/dist/fr-FR.main.js.map +1 -0
  58. package/dist/fr-FR.mjs +16 -0
  59. package/dist/fr-FR.module.js +16 -0
  60. package/dist/fr-FR.module.js.map +1 -0
  61. package/dist/he-IL.main.js +14 -0
  62. package/dist/he-IL.main.js.map +1 -0
  63. package/dist/he-IL.mjs +16 -0
  64. package/dist/he-IL.module.js +16 -0
  65. package/dist/he-IL.module.js.map +1 -0
  66. package/dist/hr-HR.main.js +14 -0
  67. package/dist/hr-HR.main.js.map +1 -0
  68. package/dist/hr-HR.mjs +16 -0
  69. package/dist/hr-HR.module.js +16 -0
  70. package/dist/hr-HR.module.js.map +1 -0
  71. package/dist/hu-HU.main.js +14 -0
  72. package/dist/hu-HU.main.js.map +1 -0
  73. package/dist/hu-HU.mjs +16 -0
  74. package/dist/hu-HU.module.js +16 -0
  75. package/dist/hu-HU.module.js.map +1 -0
  76. package/dist/import.mjs +34 -0
  77. package/dist/intlStrings.main.js +108 -0
  78. package/dist/intlStrings.main.js.map +1 -0
  79. package/dist/intlStrings.mjs +110 -0
  80. package/dist/intlStrings.module.js +110 -0
  81. package/dist/intlStrings.module.js.map +1 -0
  82. package/dist/it-IT.main.js +14 -0
  83. package/dist/it-IT.main.js.map +1 -0
  84. package/dist/it-IT.mjs +16 -0
  85. package/dist/it-IT.module.js +16 -0
  86. package/dist/it-IT.module.js.map +1 -0
  87. package/dist/ja-JP.main.js +14 -0
  88. package/dist/ja-JP.main.js.map +1 -0
  89. package/dist/ja-JP.mjs +16 -0
  90. package/dist/ja-JP.module.js +16 -0
  91. package/dist/ja-JP.module.js.map +1 -0
  92. package/dist/ko-KR.main.js +14 -0
  93. package/dist/ko-KR.main.js.map +1 -0
  94. package/dist/ko-KR.mjs +16 -0
  95. package/dist/ko-KR.module.js +16 -0
  96. package/dist/ko-KR.module.js.map +1 -0
  97. package/dist/lt-LT.main.js +14 -0
  98. package/dist/lt-LT.main.js.map +1 -0
  99. package/dist/lt-LT.mjs +16 -0
  100. package/dist/lt-LT.module.js +16 -0
  101. package/dist/lt-LT.module.js.map +1 -0
  102. package/dist/lv-LV.main.js +14 -0
  103. package/dist/lv-LV.main.js.map +1 -0
  104. package/dist/lv-LV.mjs +16 -0
  105. package/dist/lv-LV.module.js +16 -0
  106. package/dist/lv-LV.module.js.map +1 -0
  107. package/dist/main.js +26 -499
  108. package/dist/main.js.map +1 -1
  109. package/dist/module.js +12 -454
  110. package/dist/module.js.map +1 -1
  111. package/dist/nb-NO.main.js +14 -0
  112. package/dist/nb-NO.main.js.map +1 -0
  113. package/dist/nb-NO.mjs +16 -0
  114. package/dist/nb-NO.module.js +16 -0
  115. package/dist/nb-NO.module.js.map +1 -0
  116. package/dist/nl-NL.main.js +14 -0
  117. package/dist/nl-NL.main.js.map +1 -0
  118. package/dist/nl-NL.mjs +16 -0
  119. package/dist/nl-NL.module.js +16 -0
  120. package/dist/nl-NL.module.js.map +1 -0
  121. package/dist/pl-PL.main.js +14 -0
  122. package/dist/pl-PL.main.js.map +1 -0
  123. package/dist/pl-PL.mjs +16 -0
  124. package/dist/pl-PL.module.js +16 -0
  125. package/dist/pl-PL.module.js.map +1 -0
  126. package/dist/pt-BR.main.js +14 -0
  127. package/dist/pt-BR.main.js.map +1 -0
  128. package/dist/pt-BR.mjs +16 -0
  129. package/dist/pt-BR.module.js +16 -0
  130. package/dist/pt-BR.module.js.map +1 -0
  131. package/dist/pt-PT.main.js +14 -0
  132. package/dist/pt-PT.main.js.map +1 -0
  133. package/dist/pt-PT.mjs +16 -0
  134. package/dist/pt-PT.module.js +16 -0
  135. package/dist/pt-PT.module.js.map +1 -0
  136. package/dist/ro-RO.main.js +14 -0
  137. package/dist/ro-RO.main.js.map +1 -0
  138. package/dist/ro-RO.mjs +16 -0
  139. package/dist/ro-RO.module.js +16 -0
  140. package/dist/ro-RO.module.js.map +1 -0
  141. package/dist/ru-RU.main.js +14 -0
  142. package/dist/ru-RU.main.js.map +1 -0
  143. package/dist/ru-RU.mjs +16 -0
  144. package/dist/ru-RU.module.js +16 -0
  145. package/dist/ru-RU.module.js.map +1 -0
  146. package/dist/sk-SK.main.js +14 -0
  147. package/dist/sk-SK.main.js.map +1 -0
  148. package/dist/sk-SK.mjs +16 -0
  149. package/dist/sk-SK.module.js +16 -0
  150. package/dist/sk-SK.module.js.map +1 -0
  151. package/dist/sl-SI.main.js +14 -0
  152. package/dist/sl-SI.main.js.map +1 -0
  153. package/dist/sl-SI.mjs +16 -0
  154. package/dist/sl-SI.module.js +16 -0
  155. package/dist/sl-SI.module.js.map +1 -0
  156. package/dist/sr-SP.main.js +14 -0
  157. package/dist/sr-SP.main.js.map +1 -0
  158. package/dist/sr-SP.mjs +16 -0
  159. package/dist/sr-SP.module.js +16 -0
  160. package/dist/sr-SP.module.js.map +1 -0
  161. package/dist/sv-SE.main.js +14 -0
  162. package/dist/sv-SE.main.js.map +1 -0
  163. package/dist/sv-SE.mjs +16 -0
  164. package/dist/sv-SE.module.js +16 -0
  165. package/dist/sv-SE.module.js.map +1 -0
  166. package/dist/tr-TR.main.js +14 -0
  167. package/dist/tr-TR.main.js.map +1 -0
  168. package/dist/tr-TR.mjs +16 -0
  169. package/dist/tr-TR.module.js +16 -0
  170. package/dist/tr-TR.module.js.map +1 -0
  171. package/dist/types.d.ts +87 -25
  172. package/dist/types.d.ts.map +1 -1
  173. package/dist/uk-UA.main.js +14 -0
  174. package/dist/uk-UA.main.js.map +1 -0
  175. package/dist/uk-UA.mjs +16 -0
  176. package/dist/uk-UA.module.js +16 -0
  177. package/dist/uk-UA.module.js.map +1 -0
  178. package/dist/useTable.main.js +113 -0
  179. package/dist/useTable.main.js.map +1 -0
  180. package/dist/useTable.mjs +108 -0
  181. package/dist/useTable.module.js +108 -0
  182. package/dist/useTable.module.js.map +1 -0
  183. package/dist/useTableCell.main.js +36 -0
  184. package/dist/useTableCell.main.js.map +1 -0
  185. package/dist/useTableCell.mjs +31 -0
  186. package/dist/useTableCell.module.js +31 -0
  187. package/dist/useTableCell.module.js.map +1 -0
  188. package/dist/useTableColumnHeader.main.js +93 -0
  189. package/dist/useTableColumnHeader.main.js.map +1 -0
  190. package/dist/useTableColumnHeader.mjs +88 -0
  191. package/dist/useTableColumnHeader.module.js +88 -0
  192. package/dist/useTableColumnHeader.module.js.map +1 -0
  193. package/dist/useTableColumnResize.main.js +216 -0
  194. package/dist/useTableColumnResize.main.js.map +1 -0
  195. package/dist/useTableColumnResize.mjs +211 -0
  196. package/dist/useTableColumnResize.module.js +211 -0
  197. package/dist/useTableColumnResize.module.js.map +1 -0
  198. package/dist/useTableHeaderRow.main.js +32 -0
  199. package/dist/useTableHeaderRow.main.js.map +1 -0
  200. package/dist/useTableHeaderRow.mjs +27 -0
  201. package/dist/useTableHeaderRow.module.js +27 -0
  202. package/dist/useTableHeaderRow.module.js.map +1 -0
  203. package/dist/useTableRow.main.js +81 -0
  204. package/dist/useTableRow.main.js.map +1 -0
  205. package/dist/useTableRow.mjs +76 -0
  206. package/dist/useTableRow.module.js +76 -0
  207. package/dist/useTableRow.module.js.map +1 -0
  208. package/dist/useTableSelectionCheckbox.main.js +56 -0
  209. package/dist/useTableSelectionCheckbox.main.js.map +1 -0
  210. package/dist/useTableSelectionCheckbox.mjs +50 -0
  211. package/dist/useTableSelectionCheckbox.module.js +50 -0
  212. package/dist/useTableSelectionCheckbox.module.js.map +1 -0
  213. package/dist/utils.main.js +43 -0
  214. package/dist/utils.main.js.map +1 -0
  215. package/dist/utils.mjs +35 -0
  216. package/dist/utils.module.js +35 -0
  217. package/dist/utils.module.js.map +1 -0
  218. package/dist/zh-CN.main.js +14 -0
  219. package/dist/zh-CN.main.js.map +1 -0
  220. package/dist/zh-CN.mjs +16 -0
  221. package/dist/zh-CN.module.js +16 -0
  222. package/dist/zh-CN.module.js.map +1 -0
  223. package/dist/zh-TW.main.js +14 -0
  224. package/dist/zh-TW.main.js.map +1 -0
  225. package/dist/zh-TW.mjs +16 -0
  226. package/dist/zh-TW.module.js +16 -0
  227. package/dist/zh-TW.module.js.map +1 -0
  228. package/package.json +25 -17
  229. package/src/TableKeyboardDelegate.ts +18 -10
  230. package/src/index.ts +21 -7
  231. package/src/useTable.ts +62 -42
  232. package/src/useTableCell.ts +19 -9
  233. package/src/useTableColumnHeader.ts +53 -17
  234. package/src/useTableColumnResize.ts +275 -0
  235. package/src/useTableHeaderRow.ts +10 -4
  236. package/src/useTableRow.ts +60 -7
  237. package/src/useTableSelectionCheckbox.ts +17 -22
  238. package/src/utils.ts +1 -1
  239. package/LICENSE +0 -201
package/src/useTable.ts CHANGED
@@ -10,19 +10,38 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
+ import {announce} from '@react-aria/live-announcer';
13
14
  import {GridAria, GridProps, useGrid} from '@react-aria/grid';
14
15
  import {gridIds} from './utils';
15
- import {Layout} from '@react-stately/virtualizer';
16
- import {Node} from '@react-types/shared';
17
- import {RefObject, useMemo} from 'react';
16
+ // @ts-ignore
17
+ import intlMessages from '../intl/*.json';
18
+ import {Key, LayoutDelegate, Rect, RefObject, Size} from '@react-types/shared';
19
+ import {mergeProps, useDescription, useId, useUpdateEffect} from '@react-aria/utils';
18
20
  import {TableKeyboardDelegate} from './TableKeyboardDelegate';
19
- import {TableState} from '@react-stately/table';
20
- import {useCollator, useLocale} from '@react-aria/i18n';
21
- import {useId} from '@react-aria/utils';
21
+ import {tableNestedRows} from '@react-stately/flags';
22
+ import {TableState, TreeGridState} from '@react-stately/table';
23
+ import {useCollator, useLocale, useLocalizedStringFormatter} from '@react-aria/i18n';
24
+ import {useMemo} from 'react';
22
25
 
23
- interface TableProps<T> extends GridProps {
26
+ export interface AriaTableProps extends GridProps {
24
27
  /** The layout object for the table. Computes what content is visible and how to position and style them. */
25
- layout?: Layout<Node<T>>
28
+ layoutDelegate?: LayoutDelegate,
29
+ /** @deprecated - Use layoutDelegate instead. */
30
+ layout?: DeprecatedLayout
31
+ }
32
+
33
+ interface DeprecatedLayout {
34
+ getLayoutInfo(key: Key): DeprecatedLayoutInfo,
35
+ getContentSize(): Size,
36
+ virtualizer: DeprecatedVirtualizer
37
+ }
38
+
39
+ interface DeprecatedLayoutInfo {
40
+ rect: Rect
41
+ }
42
+
43
+ interface DeprecatedVirtualizer {
44
+ visibleRect: Rect
26
45
  }
27
46
 
28
47
  /**
@@ -33,10 +52,11 @@ interface TableProps<T> extends GridProps {
33
52
  * @param state - State for the table, as returned by `useTableState`.
34
53
  * @param ref - The ref attached to the table element.
35
54
  */
36
- export function useTable<T>(props: TableProps<T>, state: TableState<T>, ref: RefObject<HTMLElement>): GridAria {
55
+ export function useTable<T>(props: AriaTableProps, state: TableState<T> | TreeGridState<T>, ref: RefObject<HTMLElement | null>): GridAria {
37
56
  let {
38
57
  keyboardDelegate,
39
58
  isVirtualized,
59
+ layoutDelegate,
40
60
  layout
41
61
  } = props;
42
62
 
@@ -44,50 +64,24 @@ export function useTable<T>(props: TableProps<T>, state: TableState<T>, ref: Ref
44
64
  // When virtualized, the layout object will be passed in as a prop and override this.
45
65
  let collator = useCollator({usage: 'search', sensitivity: 'base'});
46
66
  let {direction} = useLocale();
67
+ let disabledBehavior = state.selectionManager.disabledBehavior;
47
68
  let delegate = useMemo(() => keyboardDelegate || new TableKeyboardDelegate({
48
69
  collection: state.collection,
49
70
  disabledKeys: state.disabledKeys,
71
+ disabledBehavior,
50
72
  ref,
51
73
  direction,
52
74
  collator,
75
+ layoutDelegate,
53
76
  layout
54
- }), [keyboardDelegate, state.collection, state.disabledKeys, ref, direction, collator, layout]);
55
-
56
- let id = useId();
77
+ }), [keyboardDelegate, state.collection, state.disabledKeys, disabledBehavior, ref, direction, collator, layoutDelegate, layout]);
78
+ let id = useId(props.id);
57
79
  gridIds.set(state, id);
58
80
 
59
81
  let {gridProps} = useGrid({
60
82
  ...props,
61
83
  id,
62
- keyboardDelegate: delegate,
63
- getRowText(key) {
64
- let added = state.collection.getItem(key);
65
-
66
- // If the row has a textValue, use that.
67
- if (added.textValue != null) {
68
- return added.textValue;
69
- }
70
-
71
- // Otherwise combine the text of each of the row header columns.
72
- let rowHeaderColumnKeys = state.collection.rowHeaderColumnKeys;
73
- if (rowHeaderColumnKeys) {
74
- let text = [];
75
- for (let cell of added.childNodes) {
76
- let column = state.collection.columns[cell.index];
77
- if (rowHeaderColumnKeys.has(column.key) && cell.textValue) {
78
- text.push(cell.textValue);
79
- }
80
-
81
- if (text.length === rowHeaderColumnKeys.size) {
82
- break;
83
- }
84
- }
85
-
86
- return text.join(' ');
87
- }
88
-
89
- return '';
90
- }
84
+ keyboardDelegate: delegate
91
85
  }, state, ref);
92
86
 
93
87
  // Override to include header rows
@@ -95,7 +89,33 @@ export function useTable<T>(props: TableProps<T>, state: TableState<T>, ref: Ref
95
89
  gridProps['aria-rowcount'] = state.collection.size + state.collection.headerRows.length;
96
90
  }
97
91
 
92
+ if (tableNestedRows() && 'expandedKeys' in state) {
93
+ gridProps.role = 'treegrid';
94
+ }
95
+
96
+ let {column, direction: sortDirection} = state.sortDescriptor || {};
97
+ let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/table');
98
+ let sortDescription = useMemo(() => {
99
+ let columnName = state.collection.columns.find(c => c.key === column)?.textValue;
100
+ return sortDirection && column ? stringFormatter.format(`${sortDirection}Sort`, {columnName}) : undefined;
101
+ // eslint-disable-next-line react-hooks/exhaustive-deps
102
+ }, [sortDirection, column, state.collection.columns]);
103
+
104
+ let descriptionProps = useDescription(sortDescription);
105
+
106
+ // Only announce after initial render, tabbing to the table will tell you the initial sort info already
107
+ useUpdateEffect(() => {
108
+ announce(sortDescription, 'assertive', 500);
109
+ }, [sortDescription]);
110
+
98
111
  return {
99
- gridProps
112
+ gridProps: mergeProps(
113
+ gridProps,
114
+ descriptionProps,
115
+ {
116
+ // merge sort description with long press information
117
+ 'aria-describedby': [descriptionProps['aria-describedby'], gridProps['aria-describedby']].filter(Boolean).join(' ')
118
+ }
119
+ )
100
120
  };
101
121
  }
@@ -10,22 +10,32 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
+ import {DOMAttributes, FocusableElement, RefObject} from '@react-types/shared';
13
14
  import {getCellId} from './utils';
14
15
  import {GridNode} from '@react-types/grid';
15
- import {HTMLAttributes, RefObject} from 'react';
16
16
  import {TableState} from '@react-stately/table';
17
17
  import {useGridCell} from '@react-aria/grid';
18
18
 
19
- interface TableCellProps {
19
+ export interface AriaTableCellProps {
20
20
  /** An object representing the table cell. Contains all the relevant information that makes up the row header. */
21
21
  node: GridNode<unknown>,
22
22
  /** Whether the cell is contained in a virtual scroller. */
23
- isVirtualized?: boolean
23
+ isVirtualized?: boolean,
24
+ /** Whether selection should occur on press up instead of press down. */
25
+ shouldSelectOnPressUp?: boolean,
26
+ /**
27
+ * Handler that is called when a user performs an action on the cell.
28
+ * Please use onCellAction at the collection level instead.
29
+ * @deprecated
30
+ **/
31
+ onAction?: () => void
24
32
  }
25
33
 
26
- interface TableCellAria {
34
+ export interface TableCellAria {
27
35
  /** Props for the table cell element. */
28
- gridCellProps: HTMLAttributes<HTMLElement>
36
+ gridCellProps: DOMAttributes,
37
+ /** Whether the cell is currently in a pressed state. */
38
+ isPressed: boolean
29
39
  }
30
40
 
31
41
  /**
@@ -34,9 +44,8 @@ interface TableCellAria {
34
44
  * @param state - State of the table, as returned by `useTableState`.
35
45
  * @param ref - The ref attached to the cell element.
36
46
  */
37
- export function useTableCell<T>(props: TableCellProps, state: TableState<T>, ref: RefObject<HTMLElement>): TableCellAria {
38
- let {gridCellProps} = useGridCell(props, state, ref);
39
-
47
+ export function useTableCell<T>(props: AriaTableCellProps, state: TableState<T>, ref: RefObject<FocusableElement | null>): TableCellAria {
48
+ let {gridCellProps, isPressed} = useGridCell(props, state, ref);
40
49
  let columnKey = props.node.column.key;
41
50
  if (state.collection.rowHeaderColumnKeys.has(columnKey)) {
42
51
  gridCellProps.role = 'rowheader';
@@ -44,6 +53,7 @@ export function useTableCell<T>(props: TableCellProps, state: TableState<T>, ref
44
53
  }
45
54
 
46
55
  return {
47
- gridCellProps
56
+ gridCellProps,
57
+ isPressed
48
58
  };
49
59
  }
@@ -10,26 +10,29 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
+ import {DOMAttributes, FocusableElement, RefObject} from '@react-types/shared';
13
14
  import {getColumnHeaderId} from './utils';
14
15
  import {GridNode} from '@react-types/grid';
15
- import {HTMLAttributes, RefObject} from 'react';
16
- import {mergeProps} from '@react-aria/utils';
16
+ // @ts-ignore
17
+ import intlMessages from '../intl/*.json';
18
+ import {isAndroid, mergeProps, useDescription} from '@react-aria/utils';
17
19
  import {TableState} from '@react-stately/table';
20
+ import {useEffect} from 'react';
18
21
  import {useFocusable} from '@react-aria/focus';
19
22
  import {useGridCell} from '@react-aria/grid';
23
+ import {useLocalizedStringFormatter} from '@react-aria/i18n';
20
24
  import {usePress} from '@react-aria/interactions';
21
25
 
22
-
23
- interface ColumnHeaderProps {
26
+ export interface AriaTableColumnHeaderProps<T> {
24
27
  /** An object representing the [column header](https://www.w3.org/TR/wai-aria-1.1/#columnheader). Contains all the relevant information that makes up the column header. */
25
- node: GridNode<unknown>,
28
+ node: GridNode<T>,
26
29
  /** Whether the [column header](https://www.w3.org/TR/wai-aria-1.1/#columnheader) is contained in a virtual scroller. */
27
30
  isVirtualized?: boolean
28
31
  }
29
32
 
30
- interface ColumnHeaderAria {
33
+ export interface TableColumnHeaderAria {
31
34
  /** Props for the [column header](https://www.w3.org/TR/wai-aria-1.1/#columnheader) element. */
32
- columnHeaderProps: HTMLAttributes<HTMLElement>
35
+ columnHeaderProps: DOMAttributes
33
36
  }
34
37
 
35
38
  /**
@@ -38,33 +41,66 @@ interface ColumnHeaderAria {
38
41
  * @param state - State of the table, as returned by `useTableState`.
39
42
  * @param ref - The ref attached to the column header element.
40
43
  */
41
- export function useTableColumnHeader<T>(props: ColumnHeaderProps, state: TableState<T>, ref: RefObject<HTMLElement>): ColumnHeaderAria {
44
+ export function useTableColumnHeader<T>(props: AriaTableColumnHeaderProps<T>, state: TableState<T>, ref: RefObject<FocusableElement | null>): TableColumnHeaderAria {
42
45
  let {node} = props;
43
- let {gridCellProps} = useGridCell(props, state, ref);
46
+ let allowsSorting = node.props.allowsSorting;
47
+ // if there are no focusable children, the column header will focus the cell
48
+ let {gridCellProps} = useGridCell({...props, focusMode: 'child'}, state, ref);
44
49
 
45
50
  let isSelectionCellDisabled = node.props.isSelectionCell && state.selectionManager.selectionMode === 'single';
51
+
46
52
  let {pressProps} = usePress({
47
- isDisabled: !node.props.allowsSorting || isSelectionCellDisabled,
53
+ isDisabled: !allowsSorting || isSelectionCellDisabled,
48
54
  onPress() {
49
55
  state.sort(node.key);
50
- }
56
+ },
57
+ ref
51
58
  });
52
59
 
53
60
  // Needed to pick up the focusable context, enabling things like Tooltips for example
54
61
  let {focusableProps} = useFocusable({}, ref);
55
- let ariaSort: HTMLAttributes<HTMLElement>['aria-sort'] = null;
56
- if (node.props.allowsSorting) {
57
- ariaSort = state.sortDescriptor?.column === node.key ? state.sortDescriptor.direction : 'none';
62
+
63
+ let ariaSort: DOMAttributes['aria-sort'] = null;
64
+ let isSortedColumn = state.sortDescriptor?.column === node.key;
65
+ let sortDirection = state.sortDescriptor?.direction;
66
+ // aria-sort not supported in Android Talkback
67
+ if (node.props.allowsSorting && !isAndroid()) {
68
+ ariaSort = isSortedColumn ? sortDirection : 'none';
69
+ }
70
+
71
+ let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/table');
72
+ let sortDescription;
73
+ if (allowsSorting) {
74
+ sortDescription = `${stringFormatter.format('sortable')}`;
75
+ // Android Talkback doesn't support aria-sort so we add sort order details to the aria-described by here
76
+ if (isSortedColumn && sortDirection && isAndroid()) {
77
+ sortDescription = `${sortDescription}, ${stringFormatter.format(sortDirection)}`;
78
+ }
58
79
  }
59
80
 
81
+ let descriptionProps = useDescription(sortDescription);
82
+
83
+ let shouldDisableFocus = state.collection.size === 0;
84
+ useEffect(() => {
85
+ if (shouldDisableFocus && state.selectionManager.focusedKey === node.key) {
86
+ state.selectionManager.setFocusedKey(null);
87
+ }
88
+ }, [shouldDisableFocus, state.selectionManager, node.key]);
89
+
60
90
  return {
61
91
  columnHeaderProps: {
62
- ...mergeProps(gridCellProps, pressProps, focusableProps),
92
+ ...mergeProps(
93
+ gridCellProps,
94
+ pressProps,
95
+ focusableProps,
96
+ descriptionProps,
97
+ // If the table is empty, make all column headers untabbable
98
+ shouldDisableFocus && {tabIndex: -1}
99
+ ),
63
100
  role: 'columnheader',
64
101
  id: getColumnHeaderId(state, node.key),
65
102
  'aria-colspan': node.colspan && node.colspan > 1 ? node.colspan : null,
66
- 'aria-sort': ariaSort,
67
- 'aria-disabled': isSelectionCellDisabled || undefined
103
+ 'aria-sort': ariaSort
68
104
  }
69
105
  };
70
106
  }
@@ -0,0 +1,275 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {ChangeEvent, useCallback, useEffect, useRef} from 'react';
14
+ import {ColumnSize} from '@react-types/table';
15
+ import {DOMAttributes, FocusableElement, Key, RefObject} from '@react-types/shared';
16
+ import {focusSafely} from '@react-aria/focus';
17
+ import {getColumnHeaderId} from './utils';
18
+ import {GridNode} from '@react-types/grid';
19
+ // @ts-ignore
20
+ import intlMessages from '../intl/*.json';
21
+ import {mergeProps, useDescription, useEffectEvent, useId} from '@react-aria/utils';
22
+ import {TableColumnResizeState} from '@react-stately/table';
23
+ import {useInteractionModality, useKeyboard, useMove, usePress} from '@react-aria/interactions';
24
+ import {useLocale, useLocalizedStringFormatter} from '@react-aria/i18n';
25
+ import {useVisuallyHidden} from '@react-aria/visually-hidden';
26
+
27
+ export interface TableColumnResizeAria {
28
+ /** Props for the visually hidden input element. */
29
+ inputProps: DOMAttributes,
30
+ /** Props for the resizer element. */
31
+ resizerProps: DOMAttributes,
32
+ /** Whether this column is currently being resized. */
33
+ isResizing: boolean
34
+ }
35
+
36
+ export interface AriaTableColumnResizeProps<T> {
37
+ /** An object representing the [column header](https://www.w3.org/TR/wai-aria-1.1/#columnheader). Contains all the relevant information that makes up the column header. */
38
+ column: GridNode<T>,
39
+ /** Aria label for the hidden input. Gets read when resizing. */
40
+ 'aria-label': string,
41
+ /**
42
+ * Ref to the trigger if resizing was started from a column header menu. If it's provided,
43
+ * focus will be returned there when resizing is done. If it isn't provided, it is assumed that the resizer is
44
+ * visible at all time and keyboard resizing is started via pressing Enter on the resizer and not on focus.
45
+ * */
46
+ triggerRef?: RefObject<FocusableElement | null>,
47
+ /** If resizing is disabled. */
48
+ isDisabled?: boolean,
49
+ /** Called when resizing starts. */
50
+ onResizeStart?: (widths: Map<Key, ColumnSize>) => void,
51
+ /** Called for every resize event that results in new column sizes. */
52
+ onResize?: (widths: Map<Key, ColumnSize>) => void,
53
+ /** Called when resizing ends. */
54
+ onResizeEnd?: (widths: Map<Key, ColumnSize>) => void
55
+ }
56
+
57
+ /**
58
+ * Provides the behavior and accessibility implementation for a table column resizer element.
59
+ * @param props - Props for the resizer.
60
+ * @param state - State for the table's resizable columns, as returned by `useTableColumnResizeState`.
61
+ * @param ref - The ref attached to the resizer's visually hidden input element.
62
+ */
63
+ export function useTableColumnResize<T>(props: AriaTableColumnResizeProps<T>, state: TableColumnResizeState<T>, ref: RefObject<HTMLInputElement | null>): TableColumnResizeAria {
64
+ let {column: item, triggerRef, isDisabled, onResizeStart, onResize, onResizeEnd, 'aria-label': ariaLabel} = props;
65
+ const stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/table');
66
+ let id = useId();
67
+ let isResizing = state.resizingColumn === item.key;
68
+ let isResizingRef = useRef(isResizing);
69
+ let lastSize = useRef(null);
70
+ let wasFocusedOnResizeStart = useRef(false);
71
+ let editModeEnabled = state.tableState.isKeyboardNavigationDisabled;
72
+
73
+ let {direction} = useLocale();
74
+ let {keyboardProps} = useKeyboard({
75
+ onKeyDown: (e) => {
76
+ if (editModeEnabled) {
77
+ if (e.key === 'Escape' || e.key === 'Enter' || e.key === ' ' || e.key === 'Tab') {
78
+ e.preventDefault();
79
+ endResize(item);
80
+ }
81
+ } else {
82
+ // Continue propagation on keydown events so they still bubbles to useSelectableCollection and are handled there
83
+ e.continuePropagation();
84
+
85
+ if (e.key === 'Enter') {
86
+ startResize(item);
87
+ }
88
+ }
89
+ }
90
+ });
91
+
92
+ let startResize = useEffectEvent((item) => {
93
+ if (!isResizingRef.current) {
94
+ lastSize.current = state.updateResizedColumns(item.key, state.getColumnWidth(item.key));
95
+ state.startResize(item.key);
96
+ state.tableState.setKeyboardNavigationDisabled(true);
97
+ onResizeStart?.(lastSize.current);
98
+ }
99
+ isResizingRef.current = true;
100
+ });
101
+
102
+ let resize = useEffectEvent((item, newWidth) => {
103
+ let sizes = state.updateResizedColumns(item.key, newWidth);
104
+ onResize?.(sizes);
105
+ lastSize.current = sizes;
106
+ });
107
+
108
+ let endResize = useEffectEvent((item) => {
109
+ if (isResizingRef.current) {
110
+ if (lastSize.current == null) {
111
+ lastSize.current = state.updateResizedColumns(item.key, state.getColumnWidth(item.key));
112
+ }
113
+
114
+ state.endResize();
115
+ state.tableState.setKeyboardNavigationDisabled(false);
116
+ onResizeEnd?.(lastSize.current);
117
+ isResizingRef.current = false;
118
+
119
+ if (triggerRef?.current && !wasFocusedOnResizeStart.current) {
120
+ // switch focus back to the column header unless the resizer was already focused when resizing started.
121
+ focusSafely(triggerRef.current);
122
+ }
123
+ }
124
+ lastSize.current = null;
125
+ });
126
+
127
+ const columnResizeWidthRef = useRef<number>(0);
128
+ const {moveProps} = useMove({
129
+ onMoveStart() {
130
+ columnResizeWidthRef.current = state.getColumnWidth(item.key);
131
+ startResize(item);
132
+ },
133
+ onMove(e) {
134
+ let {deltaX, deltaY, pointerType} = e;
135
+ if (direction === 'rtl') {
136
+ deltaX *= -1;
137
+ }
138
+ if (pointerType === 'keyboard') {
139
+ if (deltaY !== 0 && deltaX === 0) {
140
+ deltaX = deltaY * -1;
141
+ }
142
+ deltaX *= 10;
143
+ }
144
+ // if moving up/down only, no need to resize
145
+ if (deltaX !== 0) {
146
+ columnResizeWidthRef.current += deltaX;
147
+ resize(item, columnResizeWidthRef.current);
148
+ }
149
+ },
150
+ onMoveEnd(e) {
151
+ let {pointerType} = e;
152
+ columnResizeWidthRef.current = 0;
153
+ if (pointerType === 'mouse' || (pointerType === 'touch' && wasFocusedOnResizeStart.current)) {
154
+ endResize(item);
155
+ }
156
+ }
157
+ });
158
+
159
+ let onKeyDown = useCallback((e) => {
160
+ if (editModeEnabled) {
161
+ moveProps.onKeyDown(e);
162
+ }
163
+ }, [editModeEnabled, moveProps]);
164
+
165
+
166
+ let min = Math.floor(state.getColumnMinWidth(item.key));
167
+ let max = Math.floor(state.getColumnMaxWidth(item.key));
168
+ if (max === Infinity) {
169
+ max = Number.MAX_SAFE_INTEGER;
170
+ }
171
+ let value = Math.floor(state.getColumnWidth(item.key));
172
+ let modality: string = useInteractionModality();
173
+ if (modality === 'virtual' && (typeof window !== 'undefined' && 'ontouchstart' in window)) {
174
+ modality = 'touch';
175
+ }
176
+ let description = triggerRef?.current == null && (modality === 'keyboard' || modality === 'virtual') && !isResizing ? stringFormatter.format('resizerDescription') : undefined;
177
+ let descriptionProps = useDescription(description);
178
+ let ariaProps = {
179
+ 'aria-label': ariaLabel,
180
+ 'aria-orientation': 'horizontal' as 'horizontal',
181
+ 'aria-labelledby': `${id} ${getColumnHeaderId(state.tableState, item.key)}`,
182
+ 'aria-valuetext': stringFormatter.format('columnSize', {value}),
183
+ 'type': 'range',
184
+ min,
185
+ max,
186
+ value,
187
+ ...descriptionProps
188
+ };
189
+
190
+ const focusInput = useCallback(() => {
191
+ if (ref.current) {
192
+ focusSafely(ref.current);
193
+ }
194
+ }, [ref]);
195
+
196
+ let resizingColumn = state.resizingColumn;
197
+ let prevResizingColumn = useRef(null);
198
+ useEffect(() => {
199
+ if (prevResizingColumn.current !== resizingColumn && resizingColumn != null && resizingColumn === item.key) {
200
+ wasFocusedOnResizeStart.current = document.activeElement === ref.current;
201
+ startResize(item);
202
+ // Delay focusing input until Android Chrome's delayed click after touchend happens: https://bugs.chromium.org/p/chromium/issues/detail?id=1150073
203
+ let timeout = setTimeout(() => focusInput(), 0);
204
+ // VoiceOver on iOS has problems focusing the input from a menu.
205
+ let VOTimeout = setTimeout(focusInput, 400);
206
+ return () => {
207
+ clearTimeout(timeout);
208
+ clearTimeout(VOTimeout);
209
+ };
210
+ }
211
+ prevResizingColumn.current = resizingColumn;
212
+ }, [resizingColumn, item, focusInput, ref, startResize]);
213
+
214
+ let onChange = (e: ChangeEvent<HTMLInputElement>) => {
215
+ let currentWidth = state.getColumnWidth(item.key);
216
+ let nextValue = parseFloat(e.target.value);
217
+
218
+ if (nextValue > currentWidth) {
219
+ nextValue = currentWidth + 10;
220
+ } else {
221
+ nextValue = currentWidth - 10;
222
+ }
223
+ resize(item, nextValue);
224
+ };
225
+
226
+ let {pressProps} = usePress({
227
+ onPressStart: (e) => {
228
+ if (e.ctrlKey || e.altKey || e.metaKey || e.shiftKey || e.pointerType === 'keyboard') {
229
+ return;
230
+ }
231
+ if (e.pointerType === 'virtual' && state.resizingColumn != null) {
232
+ endResize(item);
233
+ return;
234
+ }
235
+
236
+ // Sometimes onPress won't trigger for quick taps on mobile so we want to focus the input so blurring away
237
+ // can cancel resize mode for us.
238
+ focusInput();
239
+
240
+ // If resizer is always visible, mobile screenreader user can access the visually hidden resizer directly and thus we don't need
241
+ // to handle a virtual click to start the resizer.
242
+ if (e.pointerType !== 'virtual') {
243
+ startResize(item);
244
+ }
245
+ },
246
+ onPress: (e) => {
247
+ if (((e.pointerType === 'touch' && wasFocusedOnResizeStart.current) || e.pointerType === 'mouse') && state.resizingColumn != null) {
248
+ endResize(item);
249
+ }
250
+ }
251
+ });
252
+ let {visuallyHiddenProps} = useVisuallyHidden();
253
+
254
+ return {
255
+ resizerProps: mergeProps(
256
+ keyboardProps,
257
+ {...moveProps, onKeyDown},
258
+ pressProps,
259
+ {style: {touchAction: 'none'}}
260
+ ),
261
+ inputProps: mergeProps(
262
+ visuallyHiddenProps,
263
+ {
264
+ id,
265
+ onBlur: () => {
266
+ endResize(item);
267
+ },
268
+ onChange,
269
+ disabled: isDisabled
270
+ },
271
+ ariaProps
272
+ ),
273
+ isResizing
274
+ };
275
+ }
@@ -10,23 +10,29 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {GridRowAria, GridRowProps} from '@react-aria/grid';
14
- import {RefObject} from 'react';
13
+ import {DOMAttributes, RefObject} from '@react-types/shared';
14
+ import {GridRowProps} from '@react-aria/grid';
15
+ import {tableNestedRows} from '@react-stately/flags';
15
16
  import {TableState} from '@react-stately/table';
16
17
 
18
+ export interface TableHeaderRowAria {
19
+ /** Props for the grid row element. */
20
+ rowProps: DOMAttributes
21
+ }
22
+
17
23
  /**
18
24
  * Provides the behavior and accessibility implementation for a header row in a table.
19
25
  * @param props - Props for the row.
20
26
  * @param state - State of the table, as returned by `useTableState`.
21
27
  */
22
28
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
- export function useTableHeaderRow<T>(props: GridRowProps<T>, state: TableState<T>, ref: RefObject<HTMLElement>): GridRowAria {
29
+ export function useTableHeaderRow<T>(props: GridRowProps<T>, state: TableState<T>, ref: RefObject<Element | null>): TableHeaderRowAria {
24
30
  let {node, isVirtualized} = props;
25
31
  let rowProps = {
26
32
  role: 'row'
27
33
  };
28
34
 
29
- if (isVirtualized) {
35
+ if (isVirtualized && !(tableNestedRows() && 'expandedKeys' in state)) {
30
36
  rowProps['aria-rowindex'] = node.index + 1; // aria-rowindex is 1 based
31
37
  }
32
38