@lumx/react 4.3.2-alpha.9 → 4.4.1-alpha.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.
- package/_internal/DpdvhbTO.js +159 -0
- package/_internal/DpdvhbTO.js.map +1 -0
- package/index.d.ts +263 -647
- package/index.js +2964 -4567
- package/index.js.map +1 -1
- package/package.json +16 -7
- package/utils/index.d.ts +1 -2
- package/utils/index.js +1324 -5
- package/utils/index.js.map +1 -1
- package/_internal/BcRzrT9Y.js +0 -1480
- package/_internal/BcRzrT9Y.js.map +0 -1
package/utils/index.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import { M as MovingFocusContext, N as NAV_KEYS, g as getPointerTypeFromEvent } from '../_internal/BcRzrT9Y.js';
|
|
2
|
-
export { A as A11YLiveMessage, C as ClickAwayProvider, D as DisabledStateProvider, c as MovingFocusProvider, P as Portal, d as PortalProvider, u as useDisabledStateContext, b as useVirtualFocus, a as useVirtualFocusParent } from '../_internal/BcRzrT9Y.js';
|
|
3
1
|
import React__default, { useEffect } from 'react';
|
|
2
|
+
import { join, visuallyHidden } from '@lumx/core/js/utils/classNames';
|
|
4
3
|
import { jsx } from 'react/jsx-runtime';
|
|
5
|
-
|
|
6
|
-
import
|
|
4
|
+
export { C as ClickAwayProvider, D as DisabledStateProvider, P as Portal, a as PortalProvider, u as useDisabledStateContext } from '../_internal/DpdvhbTO.js';
|
|
5
|
+
import noop from 'lodash/noop.js';
|
|
6
|
+
import findLast from 'lodash/findLast.js';
|
|
7
|
+
import find from 'lodash/find.js';
|
|
8
|
+
import findLastIndex from 'lodash/findLastIndex.js';
|
|
9
|
+
import isNil from 'lodash/isNil.js';
|
|
10
|
+
import groupBy from 'lodash/groupBy.js';
|
|
11
|
+
import uniqueId from 'lodash/uniqueId.js';
|
|
7
12
|
|
|
8
13
|
// The error margin in px we want to have for triggering infinite scroll
|
|
9
14
|
const CLASSNAME = 'lumx-infinite-scroll-anchor';
|
|
@@ -58,6 +63,1320 @@ const InfiniteScroll = ({
|
|
|
58
63
|
});
|
|
59
64
|
};
|
|
60
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Live region to read a message to screen readers.
|
|
68
|
+
* Messages can be "polite" and be read when possible or
|
|
69
|
+
* "assertive" and interrupt any message currently be read. (To be used sparingly)
|
|
70
|
+
*
|
|
71
|
+
* More information here: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions
|
|
72
|
+
*
|
|
73
|
+
* @family A11Y
|
|
74
|
+
* @param A11YLiveMessageProps
|
|
75
|
+
* @returns A11YLiveMessage
|
|
76
|
+
*/
|
|
77
|
+
const A11YLiveMessage = ({
|
|
78
|
+
type = 'polite',
|
|
79
|
+
atomic,
|
|
80
|
+
role,
|
|
81
|
+
hidden,
|
|
82
|
+
relevant,
|
|
83
|
+
children,
|
|
84
|
+
className,
|
|
85
|
+
...forwardedProps
|
|
86
|
+
}) => {
|
|
87
|
+
return /*#__PURE__*/jsx("div", {
|
|
88
|
+
...forwardedProps,
|
|
89
|
+
className: join(hidden ? visuallyHidden() : undefined, className),
|
|
90
|
+
role: role,
|
|
91
|
+
"aria-live": type,
|
|
92
|
+
"aria-atomic": atomic,
|
|
93
|
+
"aria-relevant": relevant,
|
|
94
|
+
children: children
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Create a map from the given tab stop to sort them by rowKey;
|
|
100
|
+
*/
|
|
101
|
+
function createGridMap(tabStops) {
|
|
102
|
+
/** Group all tabStop by rows to easily access them by their row keys */
|
|
103
|
+
const tabStopsByRowKey = groupBy(tabStops, 'rowKey');
|
|
104
|
+
/**
|
|
105
|
+
* An array with each row key in the order set in the tabStops array.
|
|
106
|
+
* Each rowKey will only appear once.
|
|
107
|
+
*/
|
|
108
|
+
const rowKeys = tabStops.reduce((acc, {
|
|
109
|
+
rowKey
|
|
110
|
+
}) => {
|
|
111
|
+
if (!isNil(rowKey) && !acc.includes(rowKey)) {
|
|
112
|
+
return [...acc, rowKey];
|
|
113
|
+
}
|
|
114
|
+
return acc;
|
|
115
|
+
}, []);
|
|
116
|
+
return {
|
|
117
|
+
tabStopsByRowKey,
|
|
118
|
+
rowKeys
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Check if the given tab stop is enabled */
|
|
123
|
+
const tabStopIsEnabled = tabStop => !tabStop.disabled;
|
|
124
|
+
|
|
125
|
+
const LOOP_AROUND_TYPES = {
|
|
126
|
+
/**
|
|
127
|
+
* Will continue navigation to the next row or column and loop back to the start
|
|
128
|
+
* when the last tab stop is reached
|
|
129
|
+
*/
|
|
130
|
+
nextLoop: 'next-loop',
|
|
131
|
+
/**
|
|
132
|
+
* Will continue navigation to the next row or column until
|
|
133
|
+
* the last tab stop is reached
|
|
134
|
+
*/
|
|
135
|
+
nextEnd: 'next-end',
|
|
136
|
+
/**
|
|
137
|
+
* Will loop within the current row or column
|
|
138
|
+
*/
|
|
139
|
+
inside: 'inside'
|
|
140
|
+
};
|
|
141
|
+
const CELL_SEARCH_DIRECTION = {
|
|
142
|
+
/** Look ahead of the given position */
|
|
143
|
+
asc: 'asc',
|
|
144
|
+
/** Look before the given position */
|
|
145
|
+
desc: 'desc'
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Build a loopAround configuration to ensure both row and col behavior are set.
|
|
150
|
+
*
|
|
151
|
+
* Setting a boolean will set the following behaviors:
|
|
152
|
+
*
|
|
153
|
+
* * true => { row: 'next-loop', col: 'next-loop' }
|
|
154
|
+
* * false => { row: 'next-end', col: 'next-end' }
|
|
155
|
+
*/
|
|
156
|
+
function buildLoopAroundObject(loopAround) {
|
|
157
|
+
if (typeof loopAround === 'boolean' || loopAround === undefined) {
|
|
158
|
+
const newLoopAround = loopAround ? {
|
|
159
|
+
row: LOOP_AROUND_TYPES.nextLoop,
|
|
160
|
+
col: LOOP_AROUND_TYPES.nextLoop
|
|
161
|
+
} : {
|
|
162
|
+
row: LOOP_AROUND_TYPES.nextEnd,
|
|
163
|
+
col: LOOP_AROUND_TYPES.nextEnd
|
|
164
|
+
};
|
|
165
|
+
return newLoopAround;
|
|
166
|
+
}
|
|
167
|
+
return loopAround;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Check that the given coordinate is a simple number
|
|
172
|
+
*/
|
|
173
|
+
const isNumberCoords = coords => typeof coords === 'number';
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Check that the given coordinate is a direction
|
|
177
|
+
*/
|
|
178
|
+
function isDirectionCoords(coords) {
|
|
179
|
+
return Boolean(typeof coords !== 'number' && typeof coords?.from === 'number');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Search the given column of a grid map for a cell.
|
|
184
|
+
*/
|
|
185
|
+
function findCellInCol(gridMap, col, rowCoords, cellSelector = tabStopIsEnabled) {
|
|
186
|
+
/** The rowIndex might not be strictly successive, so we need to use the actual row index keys. */
|
|
187
|
+
const {
|
|
188
|
+
rowKeys,
|
|
189
|
+
tabStopsByRowKey
|
|
190
|
+
} = gridMap;
|
|
191
|
+
const lastIndex = rowKeys.length - 1;
|
|
192
|
+
/**
|
|
193
|
+
* If the rowCoords.from is set at -1, it means we should search from the start/end.
|
|
194
|
+
*/
|
|
195
|
+
let searchIndex = rowCoords.from;
|
|
196
|
+
if (searchIndex === -1) {
|
|
197
|
+
searchIndex = rowCoords.direction === CELL_SEARCH_DIRECTION.desc ? lastIndex : 0;
|
|
198
|
+
}
|
|
199
|
+
const searchCellFunc = rowCoords.direction === CELL_SEARCH_DIRECTION.desc ? findLast : find;
|
|
200
|
+
const rowKeyWithEnabledCell = searchCellFunc(rowKeys, (rowKey, index) => {
|
|
201
|
+
const row = tabStopsByRowKey[rowKey];
|
|
202
|
+
const cell = row[col];
|
|
203
|
+
const hasCell = Boolean(cell);
|
|
204
|
+
const cellRowIndex = index;
|
|
205
|
+
|
|
206
|
+
/** Check that the target row index is in the right direction of the search */
|
|
207
|
+
const correctRowIndex = rowCoords.direction === CELL_SEARCH_DIRECTION.desc ? cellRowIndex <= searchIndex : cellRowIndex >= searchIndex;
|
|
208
|
+
if (cell && correctRowIndex) {
|
|
209
|
+
return cellSelector ? hasCell && cellSelector(cell) : hasCell;
|
|
210
|
+
}
|
|
211
|
+
return false;
|
|
212
|
+
});
|
|
213
|
+
const row = rowKeyWithEnabledCell !== undefined ? tabStopsByRowKey[rowKeyWithEnabledCell] : undefined;
|
|
214
|
+
return row?.[col];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Search the given column of a grid map for a cell.
|
|
219
|
+
*/
|
|
220
|
+
function findCellInRow(gridMap, row, colCoords, cellSelector = tabStopIsEnabled) {
|
|
221
|
+
const {
|
|
222
|
+
direction,
|
|
223
|
+
from
|
|
224
|
+
} = colCoords || {};
|
|
225
|
+
const {
|
|
226
|
+
rowKeys,
|
|
227
|
+
tabStopsByRowKey
|
|
228
|
+
} = gridMap;
|
|
229
|
+
const rowKey = rowKeys[row];
|
|
230
|
+
const currentRow = tabStopsByRowKey[rowKey];
|
|
231
|
+
if (!currentRow) {
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
const searchCellFunc = direction === CELL_SEARCH_DIRECTION.desc ? findLast : find;
|
|
235
|
+
const cell = searchCellFunc(currentRow, cellSelector, from);
|
|
236
|
+
return cell;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Parse each column of the given gridMap to find the first cell matching the selector.
|
|
241
|
+
* The direction and starting point of the search can be set using the coordinates attribute.
|
|
242
|
+
*/
|
|
243
|
+
function parseColsForCell(/** The gridMap to search */
|
|
244
|
+
gridMap, /** The coordinate to search */
|
|
245
|
+
{
|
|
246
|
+
direction = CELL_SEARCH_DIRECTION.asc,
|
|
247
|
+
from
|
|
248
|
+
}, cellSelector = tabStopIsEnabled) {
|
|
249
|
+
if (from === undefined) {
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
const {
|
|
253
|
+
rowKeys,
|
|
254
|
+
tabStopsByRowKey
|
|
255
|
+
} = gridMap;
|
|
256
|
+
|
|
257
|
+
/** As we cannot know for certain when to stop, we need to know which column is the last column */
|
|
258
|
+
const maxColIndex = rowKeys.reduce((maxLength, rowIndex) => {
|
|
259
|
+
const rowLength = tabStopsByRowKey[rowIndex].length;
|
|
260
|
+
return rowLength > maxLength ? rowLength - 1 : maxLength;
|
|
261
|
+
}, 0);
|
|
262
|
+
|
|
263
|
+
/** If "from" is set as -1, start from the end. */
|
|
264
|
+
const fromIndex = from === -1 ? maxColIndex : from || 0;
|
|
265
|
+
for (let index = fromIndex; direction === CELL_SEARCH_DIRECTION.desc ? index > -1 : index <= maxColIndex; direction === CELL_SEARCH_DIRECTION.desc ? index -= 1 : index += 1) {
|
|
266
|
+
const rowWithEnabledCed = findCellInCol(gridMap, index, {
|
|
267
|
+
direction,
|
|
268
|
+
from: direction === CELL_SEARCH_DIRECTION.desc ? -1 : 0
|
|
269
|
+
}, cellSelector);
|
|
270
|
+
if (rowWithEnabledCed) {
|
|
271
|
+
return rowWithEnabledCed;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Search for a cell in a gridMap
|
|
279
|
+
*
|
|
280
|
+
* This allows you to
|
|
281
|
+
* * Select a cell at a specific coordinate
|
|
282
|
+
* * Search for a cell from a specific column in any direction
|
|
283
|
+
* * Search for a cell from a specific row in any direction
|
|
284
|
+
*
|
|
285
|
+
* If no cell is found, returns undefined
|
|
286
|
+
*/
|
|
287
|
+
function getCell(/** The gridMap object to search in. */
|
|
288
|
+
gridMap, /** The coordinates of the cell to select */
|
|
289
|
+
coords,
|
|
290
|
+
/**
|
|
291
|
+
* A selector function to select the cell.
|
|
292
|
+
* Selects enabled cells by default.
|
|
293
|
+
*/
|
|
294
|
+
cellSelector = tabStopIsEnabled) {
|
|
295
|
+
const {
|
|
296
|
+
row,
|
|
297
|
+
col
|
|
298
|
+
} = coords || {};
|
|
299
|
+
const {
|
|
300
|
+
rowKeys,
|
|
301
|
+
tabStopsByRowKey
|
|
302
|
+
} = gridMap || {};
|
|
303
|
+
|
|
304
|
+
/** Defined row and col */
|
|
305
|
+
if (isNumberCoords(row) && isNumberCoords(col)) {
|
|
306
|
+
const rowKey = rowKeys[row];
|
|
307
|
+
return tabStopsByRowKey[rowKey][col];
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Defined row but variable col */
|
|
311
|
+
if (isDirectionCoords(col) && isNumberCoords(row)) {
|
|
312
|
+
return findCellInRow(gridMap, row, col, cellSelector);
|
|
313
|
+
}
|
|
314
|
+
if (isDirectionCoords(row)) {
|
|
315
|
+
if (isDirectionCoords(col)) {
|
|
316
|
+
return parseColsForCell(gridMap, col, cellSelector);
|
|
317
|
+
}
|
|
318
|
+
return findCellInCol(gridMap, col, row, cellSelector);
|
|
319
|
+
}
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function getCellCoordinates(gridMap, tabStop) {
|
|
324
|
+
const currentRowKey = tabStop.rowKey;
|
|
325
|
+
if (isNil(currentRowKey)) {
|
|
326
|
+
return undefined;
|
|
327
|
+
}
|
|
328
|
+
const {
|
|
329
|
+
rowKeys,
|
|
330
|
+
tabStopsByRowKey
|
|
331
|
+
} = gridMap;
|
|
332
|
+
const rowIndex = rowKeys.findIndex(rowKey => rowKey === currentRowKey);
|
|
333
|
+
const row = tabStopsByRowKey[currentRowKey];
|
|
334
|
+
const columnOffset = row.findIndex(ts => ts.id === tabStop.id);
|
|
335
|
+
return {
|
|
336
|
+
rowIndex,
|
|
337
|
+
row,
|
|
338
|
+
columnOffset
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** Check whether the list should vertically loop with the given configuration */
|
|
343
|
+
function shouldLoopListVertically(direction, loopAround) {
|
|
344
|
+
return direction === 'vertical' && loopAround?.col !== 'next-end' || direction === 'both' && loopAround?.col !== 'next-end';
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** Check whether the list should horizontally loop with the given configuration */
|
|
348
|
+
function shouldLoopListHorizontally(direction, loopAround) {
|
|
349
|
+
return direction === 'horizontal' && loopAround?.row !== 'next-end' || direction === 'both' && loopAround?.row !== 'next-end';
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Get the correct pointer type from the given event.
|
|
354
|
+
* This is used when a tab stop is selected, to check if is has been selected using a keyboard or a pointer
|
|
355
|
+
* (pen / mouse / touch)
|
|
356
|
+
*/
|
|
357
|
+
function getPointerTypeFromEvent(event) {
|
|
358
|
+
return event && 'pointerType' in event && Boolean(event.pointerType) ? 'pointer' : 'keyboard';
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Event keys used for keyboard navigation.
|
|
362
|
+
const VERTICAL_NAV_KEYS = ['ArrowUp', 'ArrowDown', 'Home', 'End'];
|
|
363
|
+
const HORIZONTAL_NAV_KEYS = ['ArrowLeft', 'ArrowRight', 'Home', 'End'];
|
|
364
|
+
const KEY_NAV_KEYS = [...HORIZONTAL_NAV_KEYS, ...VERTICAL_NAV_KEYS];
|
|
365
|
+
const NAV_KEYS = {
|
|
366
|
+
both: KEY_NAV_KEYS,
|
|
367
|
+
vertical: VERTICAL_NAV_KEYS,
|
|
368
|
+
horizontal: HORIZONTAL_NAV_KEYS
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
// Event keys union type
|
|
372
|
+
|
|
373
|
+
// Handle all navigation moves resulting in a new state.
|
|
374
|
+
const MOVES = {
|
|
375
|
+
// Move to the next item.
|
|
376
|
+
// The grid is flatten so the item after the last of a row will be the
|
|
377
|
+
// first item of the next row.
|
|
378
|
+
NEXT(state, _, index) {
|
|
379
|
+
for (let i = index + 1; i < state.tabStops.length; ++i) {
|
|
380
|
+
const tabStop = state.tabStops[i];
|
|
381
|
+
if (!tabStop.disabled) {
|
|
382
|
+
return {
|
|
383
|
+
...state,
|
|
384
|
+
allowFocusing: true,
|
|
385
|
+
selectedId: tabStop.id
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return state;
|
|
390
|
+
},
|
|
391
|
+
// Move to the previous item.
|
|
392
|
+
// The grid is flatten so the item before the first of a row will be the
|
|
393
|
+
// last item of the previous row.
|
|
394
|
+
PREVIOUS(state, _, index) {
|
|
395
|
+
for (let i = index - 1; i >= 0; --i) {
|
|
396
|
+
const tabStop = state.tabStops[i];
|
|
397
|
+
if (!tabStop.disabled) {
|
|
398
|
+
return {
|
|
399
|
+
...state,
|
|
400
|
+
allowFocusing: true,
|
|
401
|
+
selectedId: tabStop.id
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return state;
|
|
406
|
+
},
|
|
407
|
+
// Moving to the next row
|
|
408
|
+
// We move to the next row, and we stay in the same column.
|
|
409
|
+
// If we are in the last row, then we move to the first not disabled item of the next column.
|
|
410
|
+
NEXT_ROW(state, currentTabStop) {
|
|
411
|
+
const currentRowKey = currentTabStop.rowKey;
|
|
412
|
+
if (isNil(currentRowKey)) {
|
|
413
|
+
return state;
|
|
414
|
+
}
|
|
415
|
+
const gridMap = state.gridMap || createGridMap(state.tabStops);
|
|
416
|
+
const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
|
|
417
|
+
if (!cellCoordinates) {
|
|
418
|
+
return state;
|
|
419
|
+
}
|
|
420
|
+
const {
|
|
421
|
+
rowIndex,
|
|
422
|
+
columnOffset
|
|
423
|
+
} = cellCoordinates;
|
|
424
|
+
const nextRow = rowIndex + 1;
|
|
425
|
+
|
|
426
|
+
/** First try to get the next cell in the current column */
|
|
427
|
+
let tabStop = getCell(gridMap, {
|
|
428
|
+
row: {
|
|
429
|
+
from: nextRow,
|
|
430
|
+
direction: 'asc'
|
|
431
|
+
},
|
|
432
|
+
col: columnOffset
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
// If none were found, search for the next cell depending on the loop around parameter
|
|
436
|
+
if (!tabStop) {
|
|
437
|
+
switch (state.loopAround.col) {
|
|
438
|
+
/**
|
|
439
|
+
* If columns are configured to be looped inside,
|
|
440
|
+
* get the first enabled cell of the current column
|
|
441
|
+
*/
|
|
442
|
+
case LOOP_AROUND_TYPES.inside:
|
|
443
|
+
tabStop = getCell(gridMap, {
|
|
444
|
+
col: columnOffset,
|
|
445
|
+
row: {
|
|
446
|
+
from: 0,
|
|
447
|
+
direction: 'asc'
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
break;
|
|
451
|
+
/**
|
|
452
|
+
* If columns are configured to be go to the next,
|
|
453
|
+
* search for the next enabled cell from the next column
|
|
454
|
+
*/
|
|
455
|
+
case LOOP_AROUND_TYPES.nextEnd:
|
|
456
|
+
case LOOP_AROUND_TYPES.nextLoop:
|
|
457
|
+
default:
|
|
458
|
+
tabStop = getCell(gridMap, {
|
|
459
|
+
row: {
|
|
460
|
+
from: 0,
|
|
461
|
+
direction: 'asc'
|
|
462
|
+
},
|
|
463
|
+
col: {
|
|
464
|
+
from: columnOffset + 1,
|
|
465
|
+
direction: 'asc'
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
break;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* If still none is found and the columns are configured to loop
|
|
474
|
+
* search starting from the start
|
|
475
|
+
*/
|
|
476
|
+
if (!tabStop && state.loopAround.col === LOOP_AROUND_TYPES.nextLoop) {
|
|
477
|
+
tabStop = getCell(gridMap, {
|
|
478
|
+
row: {
|
|
479
|
+
from: 0,
|
|
480
|
+
direction: 'asc'
|
|
481
|
+
},
|
|
482
|
+
col: {
|
|
483
|
+
from: 0,
|
|
484
|
+
direction: 'asc'
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
if (tabStop) {
|
|
489
|
+
return {
|
|
490
|
+
...state,
|
|
491
|
+
allowFocusing: true,
|
|
492
|
+
selectedId: tabStop.id,
|
|
493
|
+
gridMap
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
return {
|
|
497
|
+
...state,
|
|
498
|
+
allowFocusing: true,
|
|
499
|
+
gridMap
|
|
500
|
+
};
|
|
501
|
+
},
|
|
502
|
+
// Moving to the previous row
|
|
503
|
+
// We move to the previous row, and we stay in the same column.
|
|
504
|
+
// If we are in the first row, then we move to the last not disabled item of the previous column.
|
|
505
|
+
PREVIOUS_ROW(state, currentTabStop) {
|
|
506
|
+
const currentRowKey = currentTabStop.rowKey;
|
|
507
|
+
if (isNil(currentRowKey)) {
|
|
508
|
+
return state;
|
|
509
|
+
}
|
|
510
|
+
const gridMap = state.gridMap || createGridMap(state.tabStops);
|
|
511
|
+
const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
|
|
512
|
+
if (!cellCoordinates) {
|
|
513
|
+
return state;
|
|
514
|
+
}
|
|
515
|
+
const {
|
|
516
|
+
rowIndex,
|
|
517
|
+
columnOffset
|
|
518
|
+
} = cellCoordinates;
|
|
519
|
+
const previousRow = rowIndex - 1;
|
|
520
|
+
let tabStop;
|
|
521
|
+
/** Search for the previous enabled cell in the current column */
|
|
522
|
+
if (previousRow >= 0) {
|
|
523
|
+
tabStop = getCell(gridMap, {
|
|
524
|
+
row: {
|
|
525
|
+
from: previousRow,
|
|
526
|
+
direction: 'desc'
|
|
527
|
+
},
|
|
528
|
+
col: columnOffset
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// If none were found, search for the previous cell depending on the loop around parameter
|
|
533
|
+
if (!tabStop) {
|
|
534
|
+
switch (state.loopAround.col) {
|
|
535
|
+
/**
|
|
536
|
+
* If columns are configured to be looped inside,
|
|
537
|
+
* get the last enabled cell of the current column
|
|
538
|
+
*/
|
|
539
|
+
case LOOP_AROUND_TYPES.inside:
|
|
540
|
+
tabStop = getCell(gridMap, {
|
|
541
|
+
col: columnOffset,
|
|
542
|
+
row: {
|
|
543
|
+
from: -1,
|
|
544
|
+
direction: 'desc'
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
break;
|
|
548
|
+
/**
|
|
549
|
+
* If columns are configured to be go to the previous,
|
|
550
|
+
* search for the last enabled cell from the previous column
|
|
551
|
+
*/
|
|
552
|
+
case LOOP_AROUND_TYPES.nextEnd:
|
|
553
|
+
case LOOP_AROUND_TYPES.nextLoop:
|
|
554
|
+
default:
|
|
555
|
+
if (columnOffset - 1 >= 0) {
|
|
556
|
+
tabStop = getCell(gridMap, {
|
|
557
|
+
row: {
|
|
558
|
+
from: -1,
|
|
559
|
+
direction: 'desc'
|
|
560
|
+
},
|
|
561
|
+
col: {
|
|
562
|
+
from: columnOffset - 1,
|
|
563
|
+
direction: 'desc'
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
break;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* If still none is found and the columns are configured to loop
|
|
572
|
+
* search starting from the end
|
|
573
|
+
*/
|
|
574
|
+
if (!tabStop && state.loopAround.col === LOOP_AROUND_TYPES.nextLoop) {
|
|
575
|
+
tabStop = getCell(gridMap, {
|
|
576
|
+
row: {
|
|
577
|
+
from: -1,
|
|
578
|
+
direction: 'desc'
|
|
579
|
+
},
|
|
580
|
+
col: {
|
|
581
|
+
from: -1,
|
|
582
|
+
direction: 'desc'
|
|
583
|
+
}
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
if (tabStop) {
|
|
587
|
+
return {
|
|
588
|
+
...state,
|
|
589
|
+
allowFocusing: true,
|
|
590
|
+
selectedId: tabStop.id,
|
|
591
|
+
gridMap
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
return {
|
|
595
|
+
...state,
|
|
596
|
+
allowFocusing: true,
|
|
597
|
+
gridMap
|
|
598
|
+
};
|
|
599
|
+
},
|
|
600
|
+
// Moving to the very first not disabled item of the list
|
|
601
|
+
VERY_FIRST(state) {
|
|
602
|
+
// The very first not disabled item' index.
|
|
603
|
+
const tabStop = state.tabStops.find(tabStopIsEnabled);
|
|
604
|
+
if (tabStop) {
|
|
605
|
+
return {
|
|
606
|
+
...state,
|
|
607
|
+
allowFocusing: true,
|
|
608
|
+
selectedId: tabStop.id
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
return state;
|
|
612
|
+
},
|
|
613
|
+
// Moving to the very last not disabled item of the list
|
|
614
|
+
VERY_LAST(state) {
|
|
615
|
+
// The very last not disabled item' index.
|
|
616
|
+
const tabStop = findLast(state.tabStops, tabStopIsEnabled);
|
|
617
|
+
if (tabStop) {
|
|
618
|
+
return {
|
|
619
|
+
...state,
|
|
620
|
+
allowFocusing: true,
|
|
621
|
+
selectedId: tabStop.id
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
return state;
|
|
625
|
+
},
|
|
626
|
+
NEXT_COLUMN(state, currentTabStop, index) {
|
|
627
|
+
const currentRowKey = currentTabStop.rowKey;
|
|
628
|
+
if (isNil(currentRowKey)) {
|
|
629
|
+
return state;
|
|
630
|
+
}
|
|
631
|
+
const gridMap = state.gridMap || createGridMap(state.tabStops);
|
|
632
|
+
const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
|
|
633
|
+
if (!cellCoordinates) {
|
|
634
|
+
return state;
|
|
635
|
+
}
|
|
636
|
+
const {
|
|
637
|
+
rowIndex,
|
|
638
|
+
columnOffset
|
|
639
|
+
} = cellCoordinates;
|
|
640
|
+
// Parse the current row and look for the next enabled cell
|
|
641
|
+
let tabStop = getCell(gridMap, {
|
|
642
|
+
row: rowIndex,
|
|
643
|
+
col: {
|
|
644
|
+
from: columnOffset + 1,
|
|
645
|
+
direction: 'asc'
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
// If none were found, search for the next cell depending on the loop around parameter
|
|
650
|
+
if (!tabStop) {
|
|
651
|
+
switch (state.loopAround.row) {
|
|
652
|
+
/**
|
|
653
|
+
* If rows are configured to be looped inside,
|
|
654
|
+
* get the first enabled cell of the current rows
|
|
655
|
+
*/
|
|
656
|
+
case LOOP_AROUND_TYPES.inside:
|
|
657
|
+
tabStop = getCell(gridMap, {
|
|
658
|
+
row: rowIndex,
|
|
659
|
+
col: {
|
|
660
|
+
from: 0,
|
|
661
|
+
direction: 'asc'
|
|
662
|
+
}
|
|
663
|
+
});
|
|
664
|
+
break;
|
|
665
|
+
/**
|
|
666
|
+
* If rows are configured to be go to the next,
|
|
667
|
+
* search for the next enabled cell from the next row
|
|
668
|
+
*/
|
|
669
|
+
case LOOP_AROUND_TYPES.nextEnd:
|
|
670
|
+
case LOOP_AROUND_TYPES.nextLoop:
|
|
671
|
+
default:
|
|
672
|
+
tabStop = find(state.tabStops, tabStopIsEnabled, index + 1);
|
|
673
|
+
break;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* If still none is found and the row are configured to loop
|
|
678
|
+
* search starting from the start
|
|
679
|
+
*/
|
|
680
|
+
if (!tabStop && state.loopAround.row === LOOP_AROUND_TYPES.nextLoop) {
|
|
681
|
+
tabStop = find(state.tabStops, tabStopIsEnabled);
|
|
682
|
+
}
|
|
683
|
+
return {
|
|
684
|
+
...state,
|
|
685
|
+
allowFocusing: true,
|
|
686
|
+
selectedId: tabStop?.id || state.selectedId,
|
|
687
|
+
gridMap
|
|
688
|
+
};
|
|
689
|
+
},
|
|
690
|
+
PREVIOUS_COLUMN(state, currentTabStop, index) {
|
|
691
|
+
const currentRowKey = currentTabStop.rowKey;
|
|
692
|
+
if (isNil(currentRowKey)) {
|
|
693
|
+
return state;
|
|
694
|
+
}
|
|
695
|
+
const gridMap = state.gridMap || createGridMap(state.tabStops);
|
|
696
|
+
const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
|
|
697
|
+
if (!cellCoordinates) {
|
|
698
|
+
return state;
|
|
699
|
+
}
|
|
700
|
+
const {
|
|
701
|
+
rowIndex,
|
|
702
|
+
columnOffset
|
|
703
|
+
} = cellCoordinates;
|
|
704
|
+
const previousColumn = columnOffset - 1;
|
|
705
|
+
let tabStop;
|
|
706
|
+
if (previousColumn >= 0) {
|
|
707
|
+
// Parse the current row and look for the next enable cell
|
|
708
|
+
tabStop = getCell(gridMap, {
|
|
709
|
+
row: rowIndex,
|
|
710
|
+
col: {
|
|
711
|
+
from: previousColumn,
|
|
712
|
+
direction: 'desc'
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
if (!tabStop) {
|
|
717
|
+
switch (state.loopAround.row) {
|
|
718
|
+
/**
|
|
719
|
+
* If rows are configured to be looped inside,
|
|
720
|
+
* get the last enabled cell of the current row
|
|
721
|
+
*/
|
|
722
|
+
case LOOP_AROUND_TYPES.inside:
|
|
723
|
+
tabStop = getCell(gridMap, {
|
|
724
|
+
row: rowIndex,
|
|
725
|
+
col: {
|
|
726
|
+
from: -1,
|
|
727
|
+
direction: 'desc'
|
|
728
|
+
}
|
|
729
|
+
});
|
|
730
|
+
break;
|
|
731
|
+
/**
|
|
732
|
+
* If rows are configured to be go to the next,
|
|
733
|
+
* search for the previous enabled cell from the previous row
|
|
734
|
+
*/
|
|
735
|
+
case LOOP_AROUND_TYPES.nextEnd:
|
|
736
|
+
case LOOP_AROUND_TYPES.nextLoop:
|
|
737
|
+
default:
|
|
738
|
+
if (index - 1 >= 0) {
|
|
739
|
+
tabStop = findLast(state.tabStops, tabStopIsEnabled, index - 1);
|
|
740
|
+
}
|
|
741
|
+
break;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* If still none is found and the rows are configured to loop
|
|
746
|
+
* search starting from the end
|
|
747
|
+
*/
|
|
748
|
+
if (!tabStop && state.loopAround.row === LOOP_AROUND_TYPES.nextLoop) {
|
|
749
|
+
tabStop = findLast(state.tabStops, tabStopIsEnabled);
|
|
750
|
+
}
|
|
751
|
+
return {
|
|
752
|
+
...state,
|
|
753
|
+
allowFocusing: true,
|
|
754
|
+
selectedId: tabStop?.id || state.selectedId,
|
|
755
|
+
gridMap
|
|
756
|
+
};
|
|
757
|
+
},
|
|
758
|
+
FIRST_IN_COLUMN(state, currentTabStop) {
|
|
759
|
+
const currentRowKey = currentTabStop.rowKey;
|
|
760
|
+
if (isNil(currentRowKey)) {
|
|
761
|
+
return state;
|
|
762
|
+
}
|
|
763
|
+
const gridMap = state.gridMap || createGridMap(state.tabStops);
|
|
764
|
+
const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
|
|
765
|
+
if (!cellCoordinates) {
|
|
766
|
+
return state;
|
|
767
|
+
}
|
|
768
|
+
const {
|
|
769
|
+
columnOffset
|
|
770
|
+
} = cellCoordinates;
|
|
771
|
+
const tabStop = getCell(gridMap, {
|
|
772
|
+
col: columnOffset,
|
|
773
|
+
row: {
|
|
774
|
+
from: 0,
|
|
775
|
+
direction: 'asc'
|
|
776
|
+
}
|
|
777
|
+
});
|
|
778
|
+
return {
|
|
779
|
+
...state,
|
|
780
|
+
allowFocusing: true,
|
|
781
|
+
selectedId: tabStop?.id || state.selectedId,
|
|
782
|
+
gridMap
|
|
783
|
+
};
|
|
784
|
+
},
|
|
785
|
+
LAST_IN_COLUMN(state, currentTabStop) {
|
|
786
|
+
const currentRowKey = currentTabStop.rowKey;
|
|
787
|
+
if (isNil(currentRowKey)) {
|
|
788
|
+
return state;
|
|
789
|
+
}
|
|
790
|
+
const gridMap = state.gridMap || createGridMap(state.tabStops);
|
|
791
|
+
const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
|
|
792
|
+
if (!cellCoordinates) {
|
|
793
|
+
return state;
|
|
794
|
+
}
|
|
795
|
+
const {
|
|
796
|
+
columnOffset
|
|
797
|
+
} = cellCoordinates;
|
|
798
|
+
const tabStop = getCell(gridMap, {
|
|
799
|
+
col: columnOffset,
|
|
800
|
+
row: {
|
|
801
|
+
from: -1,
|
|
802
|
+
direction: 'desc'
|
|
803
|
+
}
|
|
804
|
+
});
|
|
805
|
+
return {
|
|
806
|
+
...state,
|
|
807
|
+
allowFocusing: true,
|
|
808
|
+
selectedId: tabStop?.id || state.selectedId,
|
|
809
|
+
gridMap
|
|
810
|
+
};
|
|
811
|
+
},
|
|
812
|
+
// Moving to the first item in row
|
|
813
|
+
FIRST_IN_ROW(state, currentTabStop) {
|
|
814
|
+
const currentRowKey = currentTabStop.rowKey;
|
|
815
|
+
if (isNil(currentRowKey)) {
|
|
816
|
+
return state;
|
|
817
|
+
}
|
|
818
|
+
const gridMap = state.gridMap || createGridMap(state.tabStops);
|
|
819
|
+
const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
|
|
820
|
+
if (!cellCoordinates) {
|
|
821
|
+
return state;
|
|
822
|
+
}
|
|
823
|
+
const {
|
|
824
|
+
rowIndex
|
|
825
|
+
} = cellCoordinates;
|
|
826
|
+
const tabStop = getCell(gridMap, {
|
|
827
|
+
row: rowIndex,
|
|
828
|
+
col: {
|
|
829
|
+
from: 0,
|
|
830
|
+
direction: 'asc'
|
|
831
|
+
}
|
|
832
|
+
});
|
|
833
|
+
return {
|
|
834
|
+
...state,
|
|
835
|
+
allowFocusing: true,
|
|
836
|
+
selectedId: tabStop?.id || state.selectedId,
|
|
837
|
+
gridMap
|
|
838
|
+
};
|
|
839
|
+
},
|
|
840
|
+
// Moving to the last item in row
|
|
841
|
+
LAST_IN_ROW(state, currentTabStop) {
|
|
842
|
+
const currentRowKey = currentTabStop.rowKey;
|
|
843
|
+
if (isNil(currentRowKey)) {
|
|
844
|
+
return state;
|
|
845
|
+
}
|
|
846
|
+
const gridMap = state.gridMap || createGridMap(state.tabStops);
|
|
847
|
+
const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
|
|
848
|
+
if (!cellCoordinates) {
|
|
849
|
+
return state;
|
|
850
|
+
}
|
|
851
|
+
const {
|
|
852
|
+
rowIndex
|
|
853
|
+
} = cellCoordinates;
|
|
854
|
+
const tabStop = getCell(gridMap, {
|
|
855
|
+
row: rowIndex,
|
|
856
|
+
col: {
|
|
857
|
+
from: -1,
|
|
858
|
+
direction: 'desc'
|
|
859
|
+
}
|
|
860
|
+
});
|
|
861
|
+
return {
|
|
862
|
+
...state,
|
|
863
|
+
allowFocusing: true,
|
|
864
|
+
selectedId: tabStop?.id || state.selectedId,
|
|
865
|
+
gridMap
|
|
866
|
+
};
|
|
867
|
+
}
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
/** Handle `KEY_NAV` action to update */
|
|
871
|
+
const KEY_NAV = (state, action) => {
|
|
872
|
+
const {
|
|
873
|
+
id = state.selectedId || state.tabStops[0]?.id,
|
|
874
|
+
key,
|
|
875
|
+
ctrlKey
|
|
876
|
+
} = action.payload;
|
|
877
|
+
const index = state.tabStops.findIndex(tabStop => tabStop.id === id);
|
|
878
|
+
if (index === -1) {
|
|
879
|
+
// tab stop not registered
|
|
880
|
+
return state;
|
|
881
|
+
}
|
|
882
|
+
const currentTabStop = state.tabStops[index];
|
|
883
|
+
if (currentTabStop.disabled) {
|
|
884
|
+
return state;
|
|
885
|
+
}
|
|
886
|
+
const isGrid = currentTabStop.rowKey !== null;
|
|
887
|
+
const isFirst = index === state.tabStops.findIndex(tabStopIsEnabled);
|
|
888
|
+
const isLast = index === findLastIndex(state.tabStops, tabStopIsEnabled);
|
|
889
|
+
// Translates the user key down event info into a navigation instruction.
|
|
890
|
+
let navigation = null;
|
|
891
|
+
// eslint-disable-next-line default-case
|
|
892
|
+
switch (key) {
|
|
893
|
+
case 'ArrowLeft':
|
|
894
|
+
if (isGrid) {
|
|
895
|
+
navigation = 'PREVIOUS_COLUMN';
|
|
896
|
+
} else if (state.direction === 'horizontal' || state.direction === 'both') {
|
|
897
|
+
navigation = shouldLoopListHorizontally(state.direction, state.loopAround) && isFirst ? 'VERY_LAST' : 'PREVIOUS';
|
|
898
|
+
}
|
|
899
|
+
break;
|
|
900
|
+
case 'ArrowRight':
|
|
901
|
+
if (isGrid) {
|
|
902
|
+
navigation = 'NEXT_COLUMN';
|
|
903
|
+
} else if (state.direction === 'horizontal' || state.direction === 'both') {
|
|
904
|
+
navigation = shouldLoopListHorizontally(state.direction, state.loopAround) && isLast ? 'VERY_FIRST' : 'NEXT';
|
|
905
|
+
}
|
|
906
|
+
break;
|
|
907
|
+
case 'ArrowUp':
|
|
908
|
+
if (isGrid) {
|
|
909
|
+
navigation = 'PREVIOUS_ROW';
|
|
910
|
+
} else if (state.direction === 'vertical' || state.direction === 'both') {
|
|
911
|
+
navigation = shouldLoopListVertically(state.direction, state.loopAround) && isFirst ? 'VERY_LAST' : 'PREVIOUS';
|
|
912
|
+
}
|
|
913
|
+
break;
|
|
914
|
+
case 'ArrowDown':
|
|
915
|
+
if (isGrid) {
|
|
916
|
+
navigation = 'NEXT_ROW';
|
|
917
|
+
} else if (state.direction === 'vertical' || state.direction === 'both') {
|
|
918
|
+
navigation = shouldLoopListVertically(state.direction, state.loopAround) && isLast ? 'VERY_FIRST' : 'NEXT';
|
|
919
|
+
}
|
|
920
|
+
break;
|
|
921
|
+
case 'Home':
|
|
922
|
+
if (isGrid && !ctrlKey) {
|
|
923
|
+
navigation = state.gridJumpToShortcutDirection === 'vertical' ? 'FIRST_IN_COLUMN' : 'FIRST_IN_ROW';
|
|
924
|
+
} else {
|
|
925
|
+
navigation = 'VERY_FIRST';
|
|
926
|
+
}
|
|
927
|
+
break;
|
|
928
|
+
case 'End':
|
|
929
|
+
if (isGrid && !ctrlKey) {
|
|
930
|
+
navigation = state.gridJumpToShortcutDirection === 'vertical' ? 'LAST_IN_COLUMN' : 'LAST_IN_ROW';
|
|
931
|
+
} else {
|
|
932
|
+
navigation = 'VERY_LAST';
|
|
933
|
+
}
|
|
934
|
+
break;
|
|
935
|
+
}
|
|
936
|
+
if (!navigation) {
|
|
937
|
+
return state;
|
|
938
|
+
}
|
|
939
|
+
const newState = MOVES[navigation](state, currentTabStop, index);
|
|
940
|
+
return {
|
|
941
|
+
...newState,
|
|
942
|
+
isUsingKeyboard: true
|
|
943
|
+
};
|
|
944
|
+
};
|
|
945
|
+
|
|
946
|
+
/** Determine the updated value for selectedId: */
|
|
947
|
+
const getUpdatedSelectedId = (tabStops, currentSelectedId, defaultSelectedId = null) => {
|
|
948
|
+
// Get tab stop by id
|
|
949
|
+
const tabStop = currentSelectedId && tabStops.find(ts => ts.id === currentSelectedId && !ts.disabled);
|
|
950
|
+
if (!tabStop) {
|
|
951
|
+
// Fallback to default selected id if available, or first enabled tab stop if not
|
|
952
|
+
return tabStops.find(ts => ts.id === defaultSelectedId)?.id || tabStops.find(tabStopIsEnabled)?.id || null;
|
|
953
|
+
}
|
|
954
|
+
return tabStop?.id || defaultSelectedId;
|
|
955
|
+
};
|
|
956
|
+
|
|
957
|
+
/** Handle `REGISTER_TAB_STOP` action registering a new tab stop. */
|
|
958
|
+
const REGISTER_TAB_STOP = (state, action) => {
|
|
959
|
+
const newTabStop = action.payload;
|
|
960
|
+
const newTabStopElement = newTabStop.domElementRef.current;
|
|
961
|
+
if (!newTabStopElement) {
|
|
962
|
+
return state;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
// Find index of tab stop that
|
|
966
|
+
const indexToInsertAt = findLastIndex(state.tabStops, tabStop => {
|
|
967
|
+
if (tabStop.id === newTabStop.id) {
|
|
968
|
+
// tab stop already registered
|
|
969
|
+
return false;
|
|
970
|
+
}
|
|
971
|
+
const domTabStop = tabStop.domElementRef.current;
|
|
972
|
+
|
|
973
|
+
// New tab stop is following the current tab stop
|
|
974
|
+
return domTabStop?.compareDocumentPosition(newTabStopElement) === Node.DOCUMENT_POSITION_FOLLOWING;
|
|
975
|
+
});
|
|
976
|
+
const insertIndex = indexToInsertAt + 1;
|
|
977
|
+
|
|
978
|
+
// Insert new tab stop at position `indexToInsertAt`.
|
|
979
|
+
const newTabStops = [...state.tabStops];
|
|
980
|
+
newTabStops.splice(insertIndex, 0, newTabStop);
|
|
981
|
+
|
|
982
|
+
// Handle autofocus if needed
|
|
983
|
+
let {
|
|
984
|
+
selectedId,
|
|
985
|
+
allowFocusing
|
|
986
|
+
} = state;
|
|
987
|
+
if (state.autofocus === 'first' && insertIndex === 0 || state.autofocus === 'last' && insertIndex === newTabStops.length - 1 || newTabStop.autofocus) {
|
|
988
|
+
allowFocusing = true;
|
|
989
|
+
selectedId = newTabStop.id;
|
|
990
|
+
}
|
|
991
|
+
const newSelectedId = newTabStop.id === state.defaultSelectedId && !newTabStop.disabled ? newTabStop.id : getUpdatedSelectedId(newTabStops, selectedId, state.defaultSelectedId);
|
|
992
|
+
return {
|
|
993
|
+
...state,
|
|
994
|
+
/**
|
|
995
|
+
* If the tab currently being registered is enabled and set as default selected,
|
|
996
|
+
* set as selected.
|
|
997
|
+
*
|
|
998
|
+
*/
|
|
999
|
+
selectedId: newSelectedId,
|
|
1000
|
+
tabStops: newTabStops,
|
|
1001
|
+
gridMap: null,
|
|
1002
|
+
allowFocusing
|
|
1003
|
+
};
|
|
1004
|
+
};
|
|
1005
|
+
|
|
1006
|
+
/** Handle `UNREGISTER_TAB_STOP` action un-registering a new tab stop. */
|
|
1007
|
+
const UNREGISTER_TAB_STOP = (state, action) => {
|
|
1008
|
+
const {
|
|
1009
|
+
id
|
|
1010
|
+
} = action.payload;
|
|
1011
|
+
const newTabStops = state.tabStops.filter(tabStop => tabStop.id !== id);
|
|
1012
|
+
if (newTabStops.length === state.tabStops.length) {
|
|
1013
|
+
// tab stop already unregistered
|
|
1014
|
+
return state;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
/** Get the previous enabled tab stop */
|
|
1018
|
+
const previousTabStopIndex = state.tabStops.findIndex(tabStop => tabStop.id === state.selectedId && tabStopIsEnabled(tabStop));
|
|
1019
|
+
const newLocal = previousTabStopIndex - 1 > -1;
|
|
1020
|
+
const previousTabStop = newLocal ? findLast(newTabStops, tabStopIsEnabled, previousTabStopIndex - 1) : undefined;
|
|
1021
|
+
return {
|
|
1022
|
+
...state,
|
|
1023
|
+
/** Set the focus on either the previous tab stop if found or the one set as default */
|
|
1024
|
+
selectedId: getUpdatedSelectedId(newTabStops, state.selectedId, previousTabStop?.id || state.defaultSelectedId),
|
|
1025
|
+
tabStops: newTabStops,
|
|
1026
|
+
gridMap: null
|
|
1027
|
+
};
|
|
1028
|
+
};
|
|
1029
|
+
|
|
1030
|
+
/** Handle `UPDATE_TAB_STOP` action updating properties of a tab stop. */
|
|
1031
|
+
const UPDATE_TAB_STOP = (state, action) => {
|
|
1032
|
+
const {
|
|
1033
|
+
id,
|
|
1034
|
+
rowKey,
|
|
1035
|
+
disabled
|
|
1036
|
+
} = action.payload;
|
|
1037
|
+
const index = state.tabStops.findIndex(tabStop => tabStop.id === id);
|
|
1038
|
+
if (index === -1) {
|
|
1039
|
+
// tab stop not registered
|
|
1040
|
+
return state;
|
|
1041
|
+
}
|
|
1042
|
+
const tabStop = state.tabStops[index];
|
|
1043
|
+
if (tabStop.disabled === disabled && tabStop.rowKey === rowKey) {
|
|
1044
|
+
// Nothing to do so short-circuit.
|
|
1045
|
+
return state;
|
|
1046
|
+
}
|
|
1047
|
+
const newTabStop = {
|
|
1048
|
+
...tabStop,
|
|
1049
|
+
rowKey,
|
|
1050
|
+
disabled
|
|
1051
|
+
};
|
|
1052
|
+
const newTabStops = [...state.tabStops];
|
|
1053
|
+
newTabStops.splice(index, 1, newTabStop);
|
|
1054
|
+
return {
|
|
1055
|
+
...state,
|
|
1056
|
+
selectedId: getUpdatedSelectedId(newTabStops, state.selectedId, state.defaultSelectedId),
|
|
1057
|
+
tabStops: newTabStops,
|
|
1058
|
+
gridMap: null
|
|
1059
|
+
};
|
|
1060
|
+
};
|
|
1061
|
+
|
|
1062
|
+
/** Handle `SELECT_TAB_STOP` action selecting a tab stop. */
|
|
1063
|
+
const SELECT_TAB_STOP = (state, action) => {
|
|
1064
|
+
const {
|
|
1065
|
+
id,
|
|
1066
|
+
type
|
|
1067
|
+
} = action.payload;
|
|
1068
|
+
const tabStop = state.tabStops.find(ts => ts.id === id);
|
|
1069
|
+
if (!tabStop || tabStop.disabled) {
|
|
1070
|
+
return state;
|
|
1071
|
+
}
|
|
1072
|
+
return {
|
|
1073
|
+
...state,
|
|
1074
|
+
allowFocusing: true,
|
|
1075
|
+
selectedId: tabStop.id,
|
|
1076
|
+
isUsingKeyboard: type === 'keyboard'
|
|
1077
|
+
};
|
|
1078
|
+
};
|
|
1079
|
+
const SET_ALLOW_FOCUSING = (state, action) => {
|
|
1080
|
+
return {
|
|
1081
|
+
...state,
|
|
1082
|
+
selectedId: getUpdatedSelectedId(state.tabStops, null, state.defaultSelectedId),
|
|
1083
|
+
allowFocusing: action.payload.allow,
|
|
1084
|
+
isUsingKeyboard: Boolean(action.payload.isKeyboardNavigation)
|
|
1085
|
+
};
|
|
1086
|
+
};
|
|
1087
|
+
|
|
1088
|
+
/** Handle `RESET_SELECTED_TAB_STOP` action reseting the selected tab stop. */
|
|
1089
|
+
const RESET_SELECTED_TAB_STOP = state => {
|
|
1090
|
+
return {
|
|
1091
|
+
...state,
|
|
1092
|
+
allowFocusing: false,
|
|
1093
|
+
selectedId: getUpdatedSelectedId(state.tabStops, null, state.defaultSelectedId),
|
|
1094
|
+
defaultSelectedId: getUpdatedSelectedId(state.tabStops, null, state.defaultSelectedId),
|
|
1095
|
+
isUsingKeyboard: false
|
|
1096
|
+
};
|
|
1097
|
+
};
|
|
1098
|
+
|
|
1099
|
+
const INITIAL_STATE = {
|
|
1100
|
+
selectedId: null,
|
|
1101
|
+
allowFocusing: false,
|
|
1102
|
+
tabStops: [],
|
|
1103
|
+
direction: 'horizontal',
|
|
1104
|
+
loopAround: buildLoopAroundObject(false),
|
|
1105
|
+
gridMap: null,
|
|
1106
|
+
defaultSelectedId: null,
|
|
1107
|
+
autofocus: undefined,
|
|
1108
|
+
isUsingKeyboard: false
|
|
1109
|
+
};
|
|
1110
|
+
const OPTIONS_UPDATED = (state, action) => {
|
|
1111
|
+
const {
|
|
1112
|
+
autofocus
|
|
1113
|
+
} = action.payload;
|
|
1114
|
+
let {
|
|
1115
|
+
selectedId,
|
|
1116
|
+
allowFocusing
|
|
1117
|
+
} = state;
|
|
1118
|
+
|
|
1119
|
+
// Update selectedId when updating the `autofocus` option.
|
|
1120
|
+
if (!state.autofocus && autofocus) {
|
|
1121
|
+
if (autofocus === 'first') {
|
|
1122
|
+
selectedId = state.tabStops.find(tabStopIsEnabled)?.id || null;
|
|
1123
|
+
} else if (autofocus === 'last') {
|
|
1124
|
+
selectedId = findLast(state.tabStops, tabStopIsEnabled)?.id || null;
|
|
1125
|
+
}
|
|
1126
|
+
allowFocusing = true;
|
|
1127
|
+
}
|
|
1128
|
+
return {
|
|
1129
|
+
...state,
|
|
1130
|
+
...action.payload,
|
|
1131
|
+
selectedId,
|
|
1132
|
+
allowFocusing: action.payload.allowFocusing || allowFocusing,
|
|
1133
|
+
loopAround: buildLoopAroundObject(action.payload.loopAround)
|
|
1134
|
+
};
|
|
1135
|
+
};
|
|
1136
|
+
|
|
1137
|
+
/** Reducers for each action type: */
|
|
1138
|
+
const REDUCERS = {
|
|
1139
|
+
REGISTER_TAB_STOP,
|
|
1140
|
+
UNREGISTER_TAB_STOP,
|
|
1141
|
+
UPDATE_TAB_STOP,
|
|
1142
|
+
SELECT_TAB_STOP,
|
|
1143
|
+
OPTIONS_UPDATED,
|
|
1144
|
+
KEY_NAV,
|
|
1145
|
+
SET_ALLOW_FOCUSING,
|
|
1146
|
+
RESET_SELECTED_TAB_STOP
|
|
1147
|
+
};
|
|
1148
|
+
|
|
1149
|
+
/** Main reducer */
|
|
1150
|
+
const reducer = (state, action) => {
|
|
1151
|
+
return REDUCERS[action.type]?.(state, action) || state;
|
|
1152
|
+
};
|
|
1153
|
+
|
|
1154
|
+
const MovingFocusContext = /*#__PURE__*/React__default.createContext({
|
|
1155
|
+
state: INITIAL_STATE,
|
|
1156
|
+
dispatch: noop
|
|
1157
|
+
});
|
|
1158
|
+
|
|
1159
|
+
/**
|
|
1160
|
+
* Creates a roving tabindex context.
|
|
1161
|
+
*/
|
|
1162
|
+
const MovingFocusProvider = ({
|
|
1163
|
+
children,
|
|
1164
|
+
options
|
|
1165
|
+
}) => {
|
|
1166
|
+
const [state, dispatch] = React__default.useReducer(reducer, INITIAL_STATE, st => ({
|
|
1167
|
+
...st,
|
|
1168
|
+
...options,
|
|
1169
|
+
direction: options?.direction || st.direction,
|
|
1170
|
+
loopAround: buildLoopAroundObject(options?.loopAround),
|
|
1171
|
+
selectedId: options?.defaultSelectedId || INITIAL_STATE.selectedId
|
|
1172
|
+
}));
|
|
1173
|
+
const isMounted = React__default.useRef(false);
|
|
1174
|
+
|
|
1175
|
+
// Update the options whenever they change:
|
|
1176
|
+
React__default.useEffect(() => {
|
|
1177
|
+
// Skip update on mount (already up to date)
|
|
1178
|
+
if (!isMounted.current) {
|
|
1179
|
+
isMounted.current = true;
|
|
1180
|
+
return;
|
|
1181
|
+
}
|
|
1182
|
+
dispatch({
|
|
1183
|
+
type: 'OPTIONS_UPDATED',
|
|
1184
|
+
payload: {
|
|
1185
|
+
direction: options?.direction || INITIAL_STATE.direction,
|
|
1186
|
+
loopAround: buildLoopAroundObject(options?.loopAround || INITIAL_STATE.loopAround),
|
|
1187
|
+
defaultSelectedId: options?.defaultSelectedId || INITIAL_STATE.defaultSelectedId,
|
|
1188
|
+
autofocus: options?.autofocus || INITIAL_STATE.autofocus,
|
|
1189
|
+
allowFocusing: options?.allowFocusing || INITIAL_STATE.allowFocusing,
|
|
1190
|
+
listKey: options?.listKey || INITIAL_STATE.listKey,
|
|
1191
|
+
firstFocusDirection: options?.firstFocusDirection || INITIAL_STATE.firstFocusDirection,
|
|
1192
|
+
gridJumpToShortcutDirection: options?.gridJumpToShortcutDirection || INITIAL_STATE.gridJumpToShortcutDirection
|
|
1193
|
+
}
|
|
1194
|
+
});
|
|
1195
|
+
}, [isMounted, options?.allowFocusing, options?.autofocus, options?.defaultSelectedId, options?.direction, options?.loopAround, options?.listKey, options?.firstFocusDirection, options?.gridJumpToShortcutDirection]);
|
|
1196
|
+
|
|
1197
|
+
// Create a cached object to use as the context value:
|
|
1198
|
+
const context = React__default.useMemo(() => ({
|
|
1199
|
+
state,
|
|
1200
|
+
dispatch
|
|
1201
|
+
}), [state]);
|
|
1202
|
+
return /*#__PURE__*/jsx(MovingFocusContext.Provider, {
|
|
1203
|
+
value: context,
|
|
1204
|
+
children: children
|
|
1205
|
+
});
|
|
1206
|
+
};
|
|
1207
|
+
|
|
1208
|
+
/**
|
|
1209
|
+
* Hook options
|
|
1210
|
+
*/
|
|
1211
|
+
|
|
1212
|
+
/**
|
|
1213
|
+
* Hook to use in tab stop element of a virtual focus (ex: options of a listbox in a combobox).
|
|
1214
|
+
*
|
|
1215
|
+
* @returns true if the current tab stop has virtual focus
|
|
1216
|
+
*/
|
|
1217
|
+
const useVirtualFocus = (id, domElementRef, disabled = false, rowKey = null, autofocus = false) => {
|
|
1218
|
+
const isMounted = React__default.useRef(false);
|
|
1219
|
+
const {
|
|
1220
|
+
state,
|
|
1221
|
+
dispatch
|
|
1222
|
+
} = React__default.useContext(MovingFocusContext);
|
|
1223
|
+
|
|
1224
|
+
// Register the tab stop on mount and unregister it on unmount:
|
|
1225
|
+
React__default.useEffect(() => {
|
|
1226
|
+
const {
|
|
1227
|
+
current: domElement
|
|
1228
|
+
} = domElementRef;
|
|
1229
|
+
if (!domElement) {
|
|
1230
|
+
return undefined;
|
|
1231
|
+
}
|
|
1232
|
+
// Select tab stop on click
|
|
1233
|
+
const onClick = event => {
|
|
1234
|
+
dispatch({
|
|
1235
|
+
type: 'SELECT_TAB_STOP',
|
|
1236
|
+
payload: {
|
|
1237
|
+
id,
|
|
1238
|
+
type: getPointerTypeFromEvent(event)
|
|
1239
|
+
}
|
|
1240
|
+
});
|
|
1241
|
+
};
|
|
1242
|
+
domElement.addEventListener('click', onClick);
|
|
1243
|
+
|
|
1244
|
+
// Register tab stop in context
|
|
1245
|
+
dispatch({
|
|
1246
|
+
type: 'REGISTER_TAB_STOP',
|
|
1247
|
+
payload: {
|
|
1248
|
+
id,
|
|
1249
|
+
domElementRef,
|
|
1250
|
+
rowKey,
|
|
1251
|
+
disabled,
|
|
1252
|
+
autofocus
|
|
1253
|
+
}
|
|
1254
|
+
});
|
|
1255
|
+
return () => {
|
|
1256
|
+
domElement.removeEventListener('click', onClick);
|
|
1257
|
+
dispatch({
|
|
1258
|
+
type: 'UNREGISTER_TAB_STOP',
|
|
1259
|
+
payload: {
|
|
1260
|
+
id
|
|
1261
|
+
}
|
|
1262
|
+
});
|
|
1263
|
+
};
|
|
1264
|
+
},
|
|
1265
|
+
/**
|
|
1266
|
+
* Pass the list key as dependency to make tab stops
|
|
1267
|
+
* re-register when it changes.
|
|
1268
|
+
*/
|
|
1269
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1270
|
+
[state.listKey]);
|
|
1271
|
+
|
|
1272
|
+
/*
|
|
1273
|
+
* Update the tab stop data if `rowKey` or `disabled` change.
|
|
1274
|
+
* The isMounted flag is used to prevent this effect running on mount, which is benign but redundant (as the
|
|
1275
|
+
* REGISTER_TAB_STOP action would have just been dispatched).
|
|
1276
|
+
*/
|
|
1277
|
+
React__default.useEffect(() => {
|
|
1278
|
+
if (isMounted.current) {
|
|
1279
|
+
dispatch({
|
|
1280
|
+
type: 'UPDATE_TAB_STOP',
|
|
1281
|
+
payload: {
|
|
1282
|
+
id,
|
|
1283
|
+
rowKey,
|
|
1284
|
+
disabled
|
|
1285
|
+
}
|
|
1286
|
+
});
|
|
1287
|
+
} else {
|
|
1288
|
+
isMounted.current = true;
|
|
1289
|
+
}
|
|
1290
|
+
},
|
|
1291
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1292
|
+
[disabled, rowKey]);
|
|
1293
|
+
const isActive = id === state.selectedId;
|
|
1294
|
+
|
|
1295
|
+
// Scroll element into view when highlighted
|
|
1296
|
+
useEffect(() => {
|
|
1297
|
+
const {
|
|
1298
|
+
current
|
|
1299
|
+
} = domElementRef;
|
|
1300
|
+
if (isActive && current && current.scrollIntoView) {
|
|
1301
|
+
/**
|
|
1302
|
+
* In some cases, the selected item is contained in a popover
|
|
1303
|
+
* that won't be immediately set in the correct position.
|
|
1304
|
+
* Setting a small timeout before scroll the item into view
|
|
1305
|
+
* leaves it time to settle at the correct position.
|
|
1306
|
+
*/
|
|
1307
|
+
const timeout = setTimeout(() => {
|
|
1308
|
+
current.scrollIntoView({
|
|
1309
|
+
block: 'nearest'
|
|
1310
|
+
});
|
|
1311
|
+
}, 10);
|
|
1312
|
+
return () => {
|
|
1313
|
+
clearTimeout(timeout);
|
|
1314
|
+
};
|
|
1315
|
+
}
|
|
1316
|
+
return undefined;
|
|
1317
|
+
}, [domElementRef, isActive]);
|
|
1318
|
+
const focused = isActive && state.allowFocusing;
|
|
1319
|
+
|
|
1320
|
+
// Determine if the current tab stop is the currently active one:
|
|
1321
|
+
return focused;
|
|
1322
|
+
};
|
|
1323
|
+
|
|
1324
|
+
/**
|
|
1325
|
+
* Hook to use in a virtual focus parent (ex: `aria-activedescendant` on the input of a combobox).
|
|
1326
|
+
* * @returns the id of the currently active tab stop (virtual focus)
|
|
1327
|
+
*/
|
|
1328
|
+
const useVirtualFocusParent = ref => {
|
|
1329
|
+
const {
|
|
1330
|
+
state,
|
|
1331
|
+
dispatch
|
|
1332
|
+
} = React__default.useContext(MovingFocusContext);
|
|
1333
|
+
React__default.useEffect(() => {
|
|
1334
|
+
const {
|
|
1335
|
+
current: element
|
|
1336
|
+
} = ref;
|
|
1337
|
+
if (!element) {
|
|
1338
|
+
return undefined;
|
|
1339
|
+
}
|
|
1340
|
+
function handleKeyDown(evt) {
|
|
1341
|
+
const eventKey = evt.key;
|
|
1342
|
+
if (
|
|
1343
|
+
// Don't move if the current direction doesn't allow key
|
|
1344
|
+
!NAV_KEYS[state.direction].includes(eventKey) ||
|
|
1345
|
+
// Don't move if alt key is pressed
|
|
1346
|
+
evt.altKey ||
|
|
1347
|
+
// Don't move the focus if it hasn't been set yet and `firstFocusDirection` doesn't allow key
|
|
1348
|
+
!state.allowFocusing && state.firstFocusDirection && !NAV_KEYS[state.firstFocusDirection].includes(eventKey)) {
|
|
1349
|
+
return;
|
|
1350
|
+
}
|
|
1351
|
+
// If focus isn't allowed yet, simply enable it to stay on first item
|
|
1352
|
+
if (!state.allowFocusing && eventKey === 'ArrowDown') {
|
|
1353
|
+
dispatch({
|
|
1354
|
+
type: 'SET_ALLOW_FOCUSING',
|
|
1355
|
+
payload: {
|
|
1356
|
+
allow: true,
|
|
1357
|
+
isKeyboardNavigation: true
|
|
1358
|
+
}
|
|
1359
|
+
});
|
|
1360
|
+
} else {
|
|
1361
|
+
dispatch({
|
|
1362
|
+
type: 'KEY_NAV',
|
|
1363
|
+
payload: {
|
|
1364
|
+
key: eventKey,
|
|
1365
|
+
ctrlKey: evt.ctrlKey
|
|
1366
|
+
}
|
|
1367
|
+
});
|
|
1368
|
+
}
|
|
1369
|
+
evt.preventDefault();
|
|
1370
|
+
}
|
|
1371
|
+
element.addEventListener('keydown', handleKeyDown);
|
|
1372
|
+
return () => {
|
|
1373
|
+
element.removeEventListener('keydown', handleKeyDown);
|
|
1374
|
+
};
|
|
1375
|
+
}, [dispatch, ref, state.allowFocusing, state.direction, state.firstFocusDirection]);
|
|
1376
|
+
const focused = state.allowFocusing && state.selectedId || undefined;
|
|
1377
|
+
return focused;
|
|
1378
|
+
};
|
|
1379
|
+
|
|
61
1380
|
/**
|
|
62
1381
|
* A tuple of values to be applied by the containing component for the roving tabindex to work correctly.
|
|
63
1382
|
*/
|
|
@@ -170,5 +1489,5 @@ const useRovingTabIndex = (ref, disabled = false, rowKey = null, autofocus = fal
|
|
|
170
1489
|
return [tabIndex, focused, handleKeyDown, handleClick];
|
|
171
1490
|
};
|
|
172
1491
|
|
|
173
|
-
export { InfiniteScroll, MovingFocusContext, useRovingTabIndex };
|
|
1492
|
+
export { A11YLiveMessage, InfiniteScroll, MovingFocusContext, MovingFocusProvider, useRovingTabIndex, useVirtualFocus, useVirtualFocusParent };
|
|
174
1493
|
//# sourceMappingURL=index.js.map
|