@react-aria/grid 3.14.8 → 3.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (239) hide show
  1. package/dist/import.mjs +9 -9
  2. package/dist/main.js +16 -16
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +9 -9
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +16 -0
  7. package/package.json +15 -23
  8. package/src/index.ts +16 -16
  9. package/dist/GridKeyboardDelegate.main.js +0 -318
  10. package/dist/GridKeyboardDelegate.main.js.map +0 -1
  11. package/dist/GridKeyboardDelegate.mjs +0 -313
  12. package/dist/GridKeyboardDelegate.module.js +0 -313
  13. package/dist/GridKeyboardDelegate.module.js.map +0 -1
  14. package/dist/ar-AE.main.js +0 -15
  15. package/dist/ar-AE.main.js.map +0 -1
  16. package/dist/ar-AE.mjs +0 -17
  17. package/dist/ar-AE.module.js +0 -17
  18. package/dist/ar-AE.module.js.map +0 -1
  19. package/dist/bg-BG.main.js +0 -15
  20. package/dist/bg-BG.main.js.map +0 -1
  21. package/dist/bg-BG.mjs +0 -17
  22. package/dist/bg-BG.module.js +0 -17
  23. package/dist/bg-BG.module.js.map +0 -1
  24. package/dist/cs-CZ.main.js +0 -15
  25. package/dist/cs-CZ.main.js.map +0 -1
  26. package/dist/cs-CZ.mjs +0 -17
  27. package/dist/cs-CZ.module.js +0 -17
  28. package/dist/cs-CZ.module.js.map +0 -1
  29. package/dist/da-DK.main.js +0 -15
  30. package/dist/da-DK.main.js.map +0 -1
  31. package/dist/da-DK.mjs +0 -17
  32. package/dist/da-DK.module.js +0 -17
  33. package/dist/da-DK.module.js.map +0 -1
  34. package/dist/de-DE.main.js +0 -15
  35. package/dist/de-DE.main.js.map +0 -1
  36. package/dist/de-DE.mjs +0 -17
  37. package/dist/de-DE.module.js +0 -17
  38. package/dist/de-DE.module.js.map +0 -1
  39. package/dist/el-GR.main.js +0 -15
  40. package/dist/el-GR.main.js.map +0 -1
  41. package/dist/el-GR.mjs +0 -17
  42. package/dist/el-GR.module.js +0 -17
  43. package/dist/el-GR.module.js.map +0 -1
  44. package/dist/en-US.main.js +0 -15
  45. package/dist/en-US.main.js.map +0 -1
  46. package/dist/en-US.mjs +0 -17
  47. package/dist/en-US.module.js +0 -17
  48. package/dist/en-US.module.js.map +0 -1
  49. package/dist/es-ES.main.js +0 -15
  50. package/dist/es-ES.main.js.map +0 -1
  51. package/dist/es-ES.mjs +0 -17
  52. package/dist/es-ES.module.js +0 -17
  53. package/dist/es-ES.module.js.map +0 -1
  54. package/dist/et-EE.main.js +0 -15
  55. package/dist/et-EE.main.js.map +0 -1
  56. package/dist/et-EE.mjs +0 -17
  57. package/dist/et-EE.module.js +0 -17
  58. package/dist/et-EE.module.js.map +0 -1
  59. package/dist/fi-FI.main.js +0 -15
  60. package/dist/fi-FI.main.js.map +0 -1
  61. package/dist/fi-FI.mjs +0 -17
  62. package/dist/fi-FI.module.js +0 -17
  63. package/dist/fi-FI.module.js.map +0 -1
  64. package/dist/fr-FR.main.js +0 -15
  65. package/dist/fr-FR.main.js.map +0 -1
  66. package/dist/fr-FR.mjs +0 -17
  67. package/dist/fr-FR.module.js +0 -17
  68. package/dist/fr-FR.module.js.map +0 -1
  69. package/dist/he-IL.main.js +0 -15
  70. package/dist/he-IL.main.js.map +0 -1
  71. package/dist/he-IL.mjs +0 -17
  72. package/dist/he-IL.module.js +0 -17
  73. package/dist/he-IL.module.js.map +0 -1
  74. package/dist/hr-HR.main.js +0 -15
  75. package/dist/hr-HR.main.js.map +0 -1
  76. package/dist/hr-HR.mjs +0 -17
  77. package/dist/hr-HR.module.js +0 -17
  78. package/dist/hr-HR.module.js.map +0 -1
  79. package/dist/hu-HU.main.js +0 -15
  80. package/dist/hu-HU.main.js.map +0 -1
  81. package/dist/hu-HU.mjs +0 -17
  82. package/dist/hu-HU.module.js +0 -17
  83. package/dist/hu-HU.module.js.map +0 -1
  84. package/dist/intlStrings.main.js +0 -108
  85. package/dist/intlStrings.main.js.map +0 -1
  86. package/dist/intlStrings.mjs +0 -110
  87. package/dist/intlStrings.module.js +0 -110
  88. package/dist/intlStrings.module.js.map +0 -1
  89. package/dist/it-IT.main.js +0 -15
  90. package/dist/it-IT.main.js.map +0 -1
  91. package/dist/it-IT.mjs +0 -17
  92. package/dist/it-IT.module.js +0 -17
  93. package/dist/it-IT.module.js.map +0 -1
  94. package/dist/ja-JP.main.js +0 -15
  95. package/dist/ja-JP.main.js.map +0 -1
  96. package/dist/ja-JP.mjs +0 -17
  97. package/dist/ja-JP.module.js +0 -17
  98. package/dist/ja-JP.module.js.map +0 -1
  99. package/dist/ko-KR.main.js +0 -15
  100. package/dist/ko-KR.main.js.map +0 -1
  101. package/dist/ko-KR.mjs +0 -17
  102. package/dist/ko-KR.module.js +0 -17
  103. package/dist/ko-KR.module.js.map +0 -1
  104. package/dist/lt-LT.main.js +0 -15
  105. package/dist/lt-LT.main.js.map +0 -1
  106. package/dist/lt-LT.mjs +0 -17
  107. package/dist/lt-LT.module.js +0 -17
  108. package/dist/lt-LT.module.js.map +0 -1
  109. package/dist/lv-LV.main.js +0 -15
  110. package/dist/lv-LV.main.js.map +0 -1
  111. package/dist/lv-LV.mjs +0 -17
  112. package/dist/lv-LV.module.js +0 -17
  113. package/dist/lv-LV.module.js.map +0 -1
  114. package/dist/nb-NO.main.js +0 -15
  115. package/dist/nb-NO.main.js.map +0 -1
  116. package/dist/nb-NO.mjs +0 -17
  117. package/dist/nb-NO.module.js +0 -17
  118. package/dist/nb-NO.module.js.map +0 -1
  119. package/dist/nl-NL.main.js +0 -15
  120. package/dist/nl-NL.main.js.map +0 -1
  121. package/dist/nl-NL.mjs +0 -17
  122. package/dist/nl-NL.module.js +0 -17
  123. package/dist/nl-NL.module.js.map +0 -1
  124. package/dist/pl-PL.main.js +0 -15
  125. package/dist/pl-PL.main.js.map +0 -1
  126. package/dist/pl-PL.mjs +0 -17
  127. package/dist/pl-PL.module.js +0 -17
  128. package/dist/pl-PL.module.js.map +0 -1
  129. package/dist/pt-BR.main.js +0 -15
  130. package/dist/pt-BR.main.js.map +0 -1
  131. package/dist/pt-BR.mjs +0 -17
  132. package/dist/pt-BR.module.js +0 -17
  133. package/dist/pt-BR.module.js.map +0 -1
  134. package/dist/pt-PT.main.js +0 -15
  135. package/dist/pt-PT.main.js.map +0 -1
  136. package/dist/pt-PT.mjs +0 -17
  137. package/dist/pt-PT.module.js +0 -17
  138. package/dist/pt-PT.module.js.map +0 -1
  139. package/dist/ro-RO.main.js +0 -15
  140. package/dist/ro-RO.main.js.map +0 -1
  141. package/dist/ro-RO.mjs +0 -17
  142. package/dist/ro-RO.module.js +0 -17
  143. package/dist/ro-RO.module.js.map +0 -1
  144. package/dist/ru-RU.main.js +0 -15
  145. package/dist/ru-RU.main.js.map +0 -1
  146. package/dist/ru-RU.mjs +0 -17
  147. package/dist/ru-RU.module.js +0 -17
  148. package/dist/ru-RU.module.js.map +0 -1
  149. package/dist/sk-SK.main.js +0 -15
  150. package/dist/sk-SK.main.js.map +0 -1
  151. package/dist/sk-SK.mjs +0 -17
  152. package/dist/sk-SK.module.js +0 -17
  153. package/dist/sk-SK.module.js.map +0 -1
  154. package/dist/sl-SI.main.js +0 -15
  155. package/dist/sl-SI.main.js.map +0 -1
  156. package/dist/sl-SI.mjs +0 -17
  157. package/dist/sl-SI.module.js +0 -17
  158. package/dist/sl-SI.module.js.map +0 -1
  159. package/dist/sr-SP.main.js +0 -15
  160. package/dist/sr-SP.main.js.map +0 -1
  161. package/dist/sr-SP.mjs +0 -17
  162. package/dist/sr-SP.module.js +0 -17
  163. package/dist/sr-SP.module.js.map +0 -1
  164. package/dist/sv-SE.main.js +0 -15
  165. package/dist/sv-SE.main.js.map +0 -1
  166. package/dist/sv-SE.mjs +0 -17
  167. package/dist/sv-SE.module.js +0 -17
  168. package/dist/sv-SE.module.js.map +0 -1
  169. package/dist/tr-TR.main.js +0 -15
  170. package/dist/tr-TR.main.js.map +0 -1
  171. package/dist/tr-TR.mjs +0 -17
  172. package/dist/tr-TR.module.js +0 -17
  173. package/dist/tr-TR.module.js.map +0 -1
  174. package/dist/types.d.ts +0 -213
  175. package/dist/types.d.ts.map +0 -1
  176. package/dist/uk-UA.main.js +0 -15
  177. package/dist/uk-UA.main.js.map +0 -1
  178. package/dist/uk-UA.mjs +0 -17
  179. package/dist/uk-UA.module.js +0 -17
  180. package/dist/uk-UA.module.js.map +0 -1
  181. package/dist/useGrid.main.js +0 -135
  182. package/dist/useGrid.main.js.map +0 -1
  183. package/dist/useGrid.mjs +0 -130
  184. package/dist/useGrid.module.js +0 -130
  185. package/dist/useGrid.module.js.map +0 -1
  186. package/dist/useGridCell.main.js +0 -231
  187. package/dist/useGridCell.main.js.map +0 -1
  188. package/dist/useGridCell.mjs +0 -226
  189. package/dist/useGridCell.module.js +0 -226
  190. package/dist/useGridCell.module.js.map +0 -1
  191. package/dist/useGridRow.main.js +0 -56
  192. package/dist/useGridRow.main.js.map +0 -1
  193. package/dist/useGridRow.mjs +0 -51
  194. package/dist/useGridRow.module.js +0 -51
  195. package/dist/useGridRow.module.js.map +0 -1
  196. package/dist/useGridRowGroup.main.js +0 -26
  197. package/dist/useGridRowGroup.main.js.map +0 -1
  198. package/dist/useGridRowGroup.mjs +0 -21
  199. package/dist/useGridRowGroup.module.js +0 -21
  200. package/dist/useGridRowGroup.module.js.map +0 -1
  201. package/dist/useGridSelectionAnnouncement.main.js +0 -118
  202. package/dist/useGridSelectionAnnouncement.main.js.map +0 -1
  203. package/dist/useGridSelectionAnnouncement.mjs +0 -113
  204. package/dist/useGridSelectionAnnouncement.module.js +0 -113
  205. package/dist/useGridSelectionAnnouncement.module.js.map +0 -1
  206. package/dist/useGridSelectionCheckbox.main.js +0 -39
  207. package/dist/useGridSelectionCheckbox.main.js.map +0 -1
  208. package/dist/useGridSelectionCheckbox.mjs +0 -34
  209. package/dist/useGridSelectionCheckbox.module.js +0 -34
  210. package/dist/useGridSelectionCheckbox.module.js.map +0 -1
  211. package/dist/useHighlightSelectionDescription.main.js +0 -55
  212. package/dist/useHighlightSelectionDescription.main.js.map +0 -1
  213. package/dist/useHighlightSelectionDescription.mjs +0 -50
  214. package/dist/useHighlightSelectionDescription.module.js +0 -50
  215. package/dist/useHighlightSelectionDescription.module.js.map +0 -1
  216. package/dist/utils.main.js +0 -20
  217. package/dist/utils.main.js.map +0 -1
  218. package/dist/utils.mjs +0 -15
  219. package/dist/utils.module.js +0 -15
  220. package/dist/utils.module.js.map +0 -1
  221. package/dist/zh-CN.main.js +0 -15
  222. package/dist/zh-CN.main.js.map +0 -1
  223. package/dist/zh-CN.mjs +0 -17
  224. package/dist/zh-CN.module.js +0 -17
  225. package/dist/zh-CN.module.js.map +0 -1
  226. package/dist/zh-TW.main.js +0 -15
  227. package/dist/zh-TW.main.js.map +0 -1
  228. package/dist/zh-TW.mjs +0 -17
  229. package/dist/zh-TW.module.js +0 -17
  230. package/dist/zh-TW.module.js.map +0 -1
  231. package/src/GridKeyboardDelegate.ts +0 -484
  232. package/src/useGrid.ts +0 -186
  233. package/src/useGridCell.ts +0 -300
  234. package/src/useGridRow.ts +0 -84
  235. package/src/useGridRowGroup.ts +0 -29
  236. package/src/useGridSelectionAnnouncement.ts +0 -140
  237. package/src/useGridSelectionCheckbox.ts +0 -48
  238. package/src/useHighlightSelectionDescription.ts +0 -52
  239. package/src/utils.ts +0 -29
@@ -1,484 +0,0 @@
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 {Direction, DisabledBehavior, Key, KeyboardDelegate, LayoutDelegate, Node, Rect, RefObject, Size} from '@react-types/shared';
14
- import {DOMLayoutDelegate} from '@react-aria/selection';
15
- import {getChildNodes, getFirstItem, getLastItem, getNthItem} from '@react-stately/collections';
16
- import {GridCollection, GridNode} from '@react-types/grid';
17
-
18
- export interface GridKeyboardDelegateOptions<C> {
19
- collection: C,
20
- disabledKeys: Set<Key>,
21
- disabledBehavior?: DisabledBehavior,
22
- ref?: RefObject<HTMLElement | null>,
23
- direction: Direction,
24
- collator?: Intl.Collator,
25
- layoutDelegate?: LayoutDelegate,
26
- /** @deprecated - Use layoutDelegate instead. */
27
- layout?: DeprecatedLayout,
28
- focusMode?: 'row' | 'cell'
29
- }
30
-
31
- export class GridKeyboardDelegate<T, C extends GridCollection<T>> implements KeyboardDelegate {
32
- collection: C;
33
- protected disabledKeys: Set<Key>;
34
- protected disabledBehavior: DisabledBehavior;
35
- protected direction: Direction;
36
- protected collator: Intl.Collator | undefined;
37
- protected layoutDelegate: LayoutDelegate;
38
- protected focusMode: 'row' | 'cell';
39
-
40
- constructor(options: GridKeyboardDelegateOptions<C>) {
41
- this.collection = options.collection;
42
- this.disabledKeys = options.disabledKeys;
43
- this.disabledBehavior = options.disabledBehavior || 'all';
44
- this.direction = options.direction;
45
- this.collator = options.collator;
46
- if (!options.layout && !options.ref) {
47
- throw new Error('Either a layout or a ref must be specified.');
48
- }
49
- this.layoutDelegate = options.layoutDelegate || (options.layout ? new DeprecatedLayoutDelegate(options.layout) : new DOMLayoutDelegate(options.ref!));
50
- this.focusMode = options.focusMode ?? 'row';
51
- }
52
-
53
- protected isCell(node: Node<T>): boolean {
54
- return node.type === 'cell';
55
- }
56
-
57
- protected isRow(node: Node<T>): boolean {
58
- return node.type === 'row' || node.type === 'item';
59
- }
60
-
61
- private isDisabled(item: Node<unknown>) {
62
- return this.disabledBehavior === 'all' && (item.props?.isDisabled || this.disabledKeys.has(item.key));
63
- }
64
-
65
- protected findPreviousKey(fromKey?: Key, pred?: (item: Node<T>) => boolean): Key | null {
66
- let key = fromKey != null
67
- ? this.collection.getKeyBefore(fromKey)
68
- : this.collection.getLastKey();
69
-
70
- while (key != null) {
71
- let item = this.collection.getItem(key);
72
- if (!item) {
73
- return null;
74
- }
75
- if (!this.isDisabled(item) && (!pred || pred(item))) {
76
- return key;
77
- }
78
-
79
- key = this.collection.getKeyBefore(key);
80
- }
81
- return null;
82
- }
83
-
84
- protected findNextKey(fromKey?: Key, pred?: (item: Node<T>) => boolean): Key | null {
85
- let key = fromKey != null
86
- ? this.collection.getKeyAfter(fromKey)
87
- : this.collection.getFirstKey();
88
-
89
- while (key != null) {
90
- let item = this.collection.getItem(key);
91
- if (!item) {
92
- return null;
93
- }
94
- if (!this.isDisabled(item) && (!pred || pred(item))) {
95
- return key;
96
- }
97
-
98
- key = this.collection.getKeyAfter(key);
99
- if (key == null) {
100
- return null;
101
- }
102
- }
103
- return null;
104
- }
105
-
106
- protected getKeyForItemInRowByIndex(key: Key, index: number = 0): Key | null {
107
- if (index < 0) {
108
- return null;
109
- }
110
-
111
- let item = this.collection.getItem(key);
112
- if (!item) {
113
- return null;
114
- }
115
-
116
- let i = 0;
117
- for (let child of getChildNodes(item, this.collection) as Iterable<GridNode<T>>) {
118
- if (child.colSpan && child.colSpan + i > index) {
119
- return child.key ?? null;
120
- }
121
-
122
- if (child.colSpan) {
123
- i = i + child.colSpan - 1;
124
- }
125
-
126
- if (i === index) {
127
- return child.key ?? null;
128
- }
129
-
130
- i++;
131
- }
132
- return null;
133
- }
134
-
135
- getKeyBelow(fromKey: Key): Key | null {
136
- let key: Key | null = fromKey;
137
- let startItem = this.collection.getItem(key);
138
- if (!startItem) {
139
- return null;
140
- }
141
-
142
- // If focus was on a cell, start searching from the parent row
143
- if (this.isCell(startItem)) {
144
- key = startItem.parentKey ?? null;
145
- }
146
- if (key == null) {
147
- return null;
148
- }
149
-
150
- // Find the next item
151
- key = this.findNextKey(key, (item => item.type === 'item'));
152
- if (key != null) {
153
- // If focus was on a cell, focus the cell with the same index in the next row.
154
- if (this.isCell(startItem)) {
155
- let startIndex = startItem.colIndex ? startItem.colIndex : startItem.index;
156
- return this.getKeyForItemInRowByIndex(key, startIndex);
157
- }
158
-
159
- // Otherwise, focus the next row
160
- if (this.focusMode === 'row') {
161
- return key;
162
- }
163
- }
164
- return null;
165
- }
166
-
167
- getKeyAbove(fromKey: Key): Key | null {
168
- let key: Key | null = fromKey;
169
- let startItem = this.collection.getItem(key);
170
- if (!startItem) {
171
- return null;
172
- }
173
-
174
- // If focus is on a cell, start searching from the parent row
175
- if (this.isCell(startItem)) {
176
- key = startItem.parentKey ?? null;
177
- }
178
- if (key == null) {
179
- return null;
180
- }
181
-
182
- // Find the previous item
183
- key = this.findPreviousKey(key, item => item.type === 'item');
184
- if (key != null) {
185
- // If focus was on a cell, focus the cell with the same index in the previous row.
186
- if (this.isCell(startItem)) {
187
- let startIndex = startItem.colIndex ? startItem.colIndex : startItem.index;
188
- return this.getKeyForItemInRowByIndex(key, startIndex);
189
- }
190
-
191
- // Otherwise, focus the previous row
192
- if (this.focusMode === 'row') {
193
- return key;
194
- }
195
- }
196
- return null;
197
- }
198
-
199
- getKeyRightOf(key: Key): Key | null {
200
- let item = this.collection.getItem(key);
201
- if (!item) {
202
- return null;
203
- }
204
-
205
- // If focus is on a row, focus the first child cell.
206
- if (this.isRow(item)) {
207
- let children = getChildNodes(item, this.collection);
208
- return (this.direction === 'rtl'
209
- ? getLastItem(children)?.key
210
- : getFirstItem(children)?.key) ?? null;
211
- }
212
-
213
- // If focus is on a cell, focus the next cell if any,
214
- // otherwise focus the parent row.
215
- if (this.isCell(item) && item.parentKey != null) {
216
- let parent = this.collection.getItem(item.parentKey);
217
- if (!parent) {
218
- return null;
219
- }
220
- let children = getChildNodes(parent, this.collection);
221
- let next = (this.direction === 'rtl'
222
- ? getNthItem(children, item.index - 1)
223
- : getNthItem(children, item.index + 1)) ?? null;
224
-
225
- if (next) {
226
- return next.key ?? null;
227
- }
228
-
229
- // focus row only if focusMode is set to row
230
- if (this.focusMode === 'row') {
231
- return item.parentKey ?? null;
232
- }
233
-
234
- return (this.direction === 'rtl' ? this.getFirstKey(key) : this.getLastKey(key)) ?? null;
235
- }
236
- return null;
237
- }
238
-
239
- getKeyLeftOf(key: Key): Key | null {
240
- let item = this.collection.getItem(key);
241
- if (!item) {
242
- return null;
243
- }
244
-
245
- // If focus is on a row, focus the last child cell.
246
- if (this.isRow(item)) {
247
- let children = getChildNodes(item, this.collection);
248
- return (this.direction === 'rtl'
249
- ? getFirstItem(children)?.key
250
- : getLastItem(children)?.key) ?? null;
251
- }
252
-
253
- // If focus is on a cell, focus the previous cell if any,
254
- // otherwise focus the parent row.
255
- if (this.isCell(item) && item.parentKey != null) {
256
- let parent = this.collection.getItem(item.parentKey);
257
- if (!parent) {
258
- return null;
259
- }
260
- let children = getChildNodes(parent, this.collection);
261
- let prev = (this.direction === 'rtl'
262
- ? getNthItem(children, item.index + 1)
263
- : getNthItem(children, item.index - 1)) ?? null;
264
-
265
- if (prev) {
266
- return prev.key ?? null;
267
- }
268
-
269
- // focus row only if focusMode is set to row
270
- if (this.focusMode === 'row') {
271
- return item.parentKey ?? null;
272
- }
273
-
274
- return (this.direction === 'rtl' ? this.getLastKey(key) : this.getFirstKey(key)) ?? null;
275
- }
276
- return null;
277
- }
278
-
279
- getFirstKey(fromKey?: Key, global?: boolean): Key | null {
280
- let key: Key | null = fromKey ?? null;
281
- let item: Node<T> | undefined | null;
282
- if (key != null) {
283
- item = this.collection.getItem(key);
284
- if (!item) {
285
- return null;
286
- }
287
-
288
- // If global flag is not set, and a cell is currently focused,
289
- // move focus to the first cell in the parent row.
290
- if (this.isCell(item) && !global && item.parentKey != null) {
291
- let parent = this.collection.getItem(item.parentKey);
292
- if (!parent) {
293
- return null;
294
- }
295
- return getFirstItem(getChildNodes(parent, this.collection))?.key ?? null;
296
- }
297
- }
298
-
299
- // Find the first row
300
- key = this.findNextKey(undefined, item => item.type === 'item');
301
-
302
- // If global flag is set (or if focus mode is cell), focus the first cell in the first row.
303
- if (key != null && ((item && this.isCell(item) && global) || this.focusMode === 'cell')) {
304
- let item = this.collection.getItem(key);
305
- if (!item) {
306
- return null;
307
- }
308
- key = getFirstItem(getChildNodes(item, this.collection))?.key ?? null;
309
- }
310
-
311
- // Otherwise, focus the row itself.
312
- return key;
313
- }
314
-
315
- getLastKey(fromKey?: Key, global?: boolean): Key | null {
316
- let key: Key | null = fromKey ?? null;
317
- let item: Node<T> | undefined | null;
318
- if (key != null) {
319
- item = this.collection.getItem(key);
320
- if (!item) {
321
- return null;
322
- }
323
-
324
- // If global flag is not set, and a cell is currently focused,
325
- // move focus to the last cell in the parent row.
326
- if (this.isCell(item) && !global && item.parentKey != null) {
327
- let parent = this.collection.getItem(item.parentKey);
328
- if (!parent) {
329
- return null;
330
- }
331
- let children = getChildNodes(parent, this.collection);
332
- return getLastItem(children)?.key ?? null;
333
- }
334
- }
335
-
336
- // Find the last row
337
- key = this.findPreviousKey(undefined, item => item.type === 'item');
338
-
339
- // If global flag is set (or if focus mode is cell), focus the last cell in the last row.
340
- if (key != null && ((item && this.isCell(item) && global) || this.focusMode === 'cell')) {
341
- let item = this.collection.getItem(key);
342
- if (!item) {
343
- return null;
344
- }
345
- let children = getChildNodes(item, this.collection);
346
- key = getLastItem(children)?.key ?? null;
347
- }
348
-
349
- // Otherwise, focus the row itself.
350
- return key;
351
- }
352
-
353
- getKeyPageAbove(fromKey: Key): Key | null {
354
- let key: Key | null = fromKey;
355
- let itemRect = this.layoutDelegate.getItemRect(key);
356
- if (!itemRect) {
357
- return null;
358
- }
359
-
360
- let pageY = Math.max(0, itemRect.y + itemRect.height - this.layoutDelegate.getVisibleRect().height);
361
-
362
- while (itemRect && itemRect.y > pageY && key != null) {
363
- key = this.getKeyAbove(key) ?? null;
364
- if (key == null) {
365
- break;
366
- }
367
- itemRect = this.layoutDelegate.getItemRect(key);
368
- }
369
-
370
- return key;
371
- }
372
-
373
- getKeyPageBelow(fromKey: Key): Key | null {
374
- let key: Key | null = fromKey;
375
- let itemRect = this.layoutDelegate.getItemRect(key);
376
-
377
- if (!itemRect) {
378
- return null;
379
- }
380
-
381
- let pageHeight = this.layoutDelegate.getVisibleRect().height;
382
- let pageY = Math.min(this.layoutDelegate.getContentSize().height, itemRect.y + pageHeight);
383
-
384
- while (itemRect && (itemRect.y + itemRect.height) < pageY) {
385
- let nextKey = this.getKeyBelow(key);
386
- // If nextKey is undefined, we've reached the last row already
387
- if (nextKey == null) {
388
- break;
389
- }
390
-
391
- itemRect = this.layoutDelegate.getItemRect(nextKey);
392
- key = nextKey;
393
- }
394
-
395
- return key;
396
- }
397
-
398
- getKeyForSearch(search: string, fromKey?: Key): Key | null {
399
- let key: Key | null = fromKey ?? null;
400
- if (!this.collator) {
401
- return null;
402
- }
403
-
404
- let collection = this.collection;
405
- key = fromKey ?? this.getFirstKey();
406
- if (key == null) {
407
- return null;
408
- }
409
-
410
- // If the starting key is a cell, search from its parent row.
411
- let startItem = collection.getItem(key);
412
- if (!startItem) {
413
- return null;
414
- }
415
- if (startItem.type === 'cell') {
416
- key = startItem.parentKey ?? null;
417
- }
418
-
419
- let hasWrapped = false;
420
- while (key != null) {
421
- let item = collection.getItem(key);
422
- if (!item) {
423
- return null;
424
- }
425
-
426
- // check row text value for match
427
- if (item.textValue) {
428
- let substring = item.textValue.slice(0, search.length);
429
- if (this.collator.compare(substring, search) === 0) {
430
- if (this.isRow(item) && this.focusMode === 'cell') {
431
- return getFirstItem(getChildNodes(item, this.collection))?.key ?? null;
432
- }
433
-
434
- return item.key;
435
- }
436
- }
437
-
438
- key = this.findNextKey(key, item => item.type === 'item');
439
-
440
- // Wrap around when reaching the end of the collection
441
- if (key == null && !hasWrapped) {
442
- key = this.getFirstKey();
443
- hasWrapped = true;
444
- }
445
- }
446
-
447
- return null;
448
- }
449
- }
450
-
451
- /* Backward compatibility for old Virtualizer Layout interface. */
452
- interface DeprecatedLayout {
453
- getLayoutInfo(key: Key): DeprecatedLayoutInfo,
454
- getContentSize(): Size,
455
- virtualizer: DeprecatedVirtualizer
456
- }
457
-
458
- interface DeprecatedLayoutInfo {
459
- rect: Rect
460
- }
461
-
462
- interface DeprecatedVirtualizer {
463
- visibleRect: Rect
464
- }
465
-
466
- class DeprecatedLayoutDelegate implements LayoutDelegate {
467
- layout: DeprecatedLayout;
468
-
469
- constructor(layout: DeprecatedLayout) {
470
- this.layout = layout;
471
- }
472
-
473
- getContentSize(): Size {
474
- return this.layout.getContentSize();
475
- }
476
-
477
- getItemRect(key: Key): Rect | null {
478
- return this.layout.getLayoutInfo(key)?.rect || null;
479
- }
480
-
481
- getVisibleRect(): Rect {
482
- return this.layout.virtualizer.visibleRect;
483
- }
484
- }
package/src/useGrid.ts DELETED
@@ -1,186 +0,0 @@
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 {AriaLabelingProps, DOMAttributes, DOMProps, Key, KeyboardDelegate, RefObject} from '@react-types/shared';
14
- import {filterDOMProps, getEventTarget, mergeProps, nodeContains, useId} from '@react-aria/utils';
15
- import {FocusEventHandler, useCallback, useMemo} from 'react';
16
- import {GridCollection} from '@react-types/grid';
17
- import {GridKeyboardDelegate} from './GridKeyboardDelegate';
18
- import {gridMap} from './utils';
19
- import {GridState} from '@react-stately/grid';
20
- import {useCollator, useLocale} from '@react-aria/i18n';
21
- import {useGridSelectionAnnouncement} from './useGridSelectionAnnouncement';
22
- import {useHasTabbableChild} from '@react-aria/focus';
23
- import {useHighlightSelectionDescription} from './useHighlightSelectionDescription';
24
- import {useSelectableCollection} from '@react-aria/selection';
25
-
26
- export interface GridProps extends DOMProps, AriaLabelingProps {
27
- /** Whether the grid uses virtual scrolling. */
28
- isVirtualized?: boolean,
29
- /**
30
- * Whether typeahead navigation is disabled.
31
- * @default false
32
- */
33
- disallowTypeAhead?: boolean,
34
- /**
35
- * An optional keyboard delegate implementation for type to select,
36
- * to override the default.
37
- */
38
- keyboardDelegate?: KeyboardDelegate,
39
- /**
40
- * Whether initial grid focus should be placed on the grid row or grid cell.
41
- * @default 'row'
42
- */
43
- focusMode?: 'row' | 'cell',
44
- /**
45
- * A function that returns the text that should be announced by assistive technology when a row is added or removed from selection.
46
- * @default (key) => state.collection.getItem(key)?.textValue
47
- */
48
- getRowText?: (key: Key) => string,
49
- /**
50
- * The ref attached to the scrollable body. Used to provided automatic scrolling on item focus for non-virtualized grids.
51
- */
52
- scrollRef?: RefObject<HTMLElement | null>,
53
- /** Handler that is called when a user performs an action on the row. */
54
- onRowAction?: (key: Key) => void,
55
- /** Handler that is called when a user performs an action on the cell. */
56
- onCellAction?: (key: Key) => void,
57
- /**
58
- * Whether pressing the escape key should clear selection in the grid or not.
59
- *
60
- * Most experiences should not modify this option as it eliminates a keyboard user's ability to
61
- * easily clear selection. Only use if the escape key is being handled externally or should not
62
- * trigger selection clearing contextually.
63
- * @default 'clearSelection'
64
- */
65
- escapeKeyBehavior?: 'clearSelection' | 'none',
66
- /** Whether selection should occur on press up instead of press down. */
67
- shouldSelectOnPressUp?: boolean
68
- }
69
-
70
- export interface GridAria {
71
- /** Props for the grid element. */
72
- gridProps: DOMAttributes
73
- }
74
-
75
- /**
76
- * Provides the behavior and accessibility implementation for a grid component.
77
- * A grid displays data in one or more rows and columns and enables a user to navigate its contents via directional navigation keys.
78
- * @param props - Props for the grid.
79
- * @param state - State for the grid, as returned by `useGridState`.
80
- * @param ref - The ref attached to the grid element.
81
- */
82
- export function useGrid<T>(props: GridProps, state: GridState<T, GridCollection<T>>, ref: RefObject<HTMLElement | null>): GridAria {
83
- let {
84
- isVirtualized,
85
- disallowTypeAhead,
86
- keyboardDelegate,
87
- focusMode,
88
- scrollRef,
89
- getRowText,
90
- onRowAction,
91
- onCellAction,
92
- escapeKeyBehavior = 'clearSelection',
93
- shouldSelectOnPressUp
94
- } = props;
95
- let {selectionManager: manager} = state;
96
-
97
- if (!props['aria-label'] && !props['aria-labelledby']) {
98
- console.warn('An aria-label or aria-labelledby prop is required for accessibility.');
99
- }
100
-
101
- // By default, a KeyboardDelegate is provided which uses the DOM to query layout information (e.g. for page up/page down).
102
- // When virtualized, the layout object will be passed in as a prop and override this.
103
- let collator = useCollator({usage: 'search', sensitivity: 'base'});
104
- let {direction} = useLocale();
105
- let disabledBehavior = state.selectionManager.disabledBehavior;
106
- let delegate = useMemo(() => keyboardDelegate || new GridKeyboardDelegate({
107
- collection: state.collection,
108
- disabledKeys: state.disabledKeys,
109
- disabledBehavior,
110
- ref,
111
- direction,
112
- collator,
113
- focusMode
114
- }), [keyboardDelegate, state.collection, state.disabledKeys, disabledBehavior, ref, direction, collator, focusMode]);
115
-
116
- let {collectionProps} = useSelectableCollection({
117
- ref,
118
- selectionManager: manager,
119
- keyboardDelegate: delegate,
120
- isVirtualized,
121
- scrollRef,
122
- disallowTypeAhead,
123
- escapeKeyBehavior
124
- });
125
-
126
- let id = useId(props.id);
127
- gridMap.set(state, {keyboardDelegate: delegate, actions: {onRowAction, onCellAction}, shouldSelectOnPressUp});
128
-
129
- let descriptionProps = useHighlightSelectionDescription({
130
- selectionManager: manager,
131
- hasItemActions: !!(onRowAction || onCellAction)
132
- });
133
-
134
- let domProps = filterDOMProps(props, {labelable: true});
135
-
136
- let onFocus: FocusEventHandler = useCallback((e) => {
137
- if (manager.isFocused) {
138
- // If a focus event bubbled through a portal, reset focus state.
139
- if (!nodeContains(e.currentTarget, getEventTarget(e))) {
140
- manager.setFocused(false);
141
- }
142
-
143
- return;
144
- }
145
-
146
- // Focus events can bubble through portals. Ignore these events.
147
- if (!nodeContains(e.currentTarget, getEventTarget(e))) {
148
- return;
149
- }
150
-
151
- manager.setFocused(true);
152
- }, [manager]);
153
-
154
- // Continue to track collection focused state even if keyboard navigation is disabled
155
- let navDisabledHandlers = useMemo(() => ({
156
- onBlur: collectionProps.onBlur,
157
- onFocus
158
- }), [onFocus, collectionProps.onBlur]);
159
-
160
- let hasTabbableChild = useHasTabbableChild(ref, {
161
- isDisabled: state.collection.size !== 0
162
- });
163
-
164
- let gridProps: DOMAttributes = mergeProps(
165
- domProps,
166
- {
167
- role: 'grid',
168
- id,
169
- 'aria-multiselectable': manager.selectionMode === 'multiple' ? 'true' : undefined
170
- },
171
- state.isKeyboardNavigationDisabled ? navDisabledHandlers : collectionProps,
172
- // If collection is empty, make sure the grid is tabbable unless there is a child tabbable element.
173
- (state.collection.size === 0 && {tabIndex: hasTabbableChild ? -1 : 0}) || undefined,
174
- descriptionProps
175
- );
176
-
177
- if (isVirtualized) {
178
- gridProps['aria-rowcount'] = state.collection.size;
179
- gridProps['aria-colcount'] = state.collection.columnCount;
180
- }
181
-
182
- useGridSelectionAnnouncement({getRowText}, state);
183
- return {
184
- gridProps
185
- };
186
- }