@react-aria/test-utils 1.0.0-alpha.3 → 1.0.0-alpha.4
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/dist/combobox.main.js +116 -82
- package/dist/combobox.main.js.map +1 -1
- package/dist/combobox.mjs +117 -83
- package/dist/combobox.module.js +117 -83
- package/dist/combobox.module.js.map +1 -1
- package/dist/events.main.js.map +1 -1
- package/dist/events.module.js.map +1 -1
- package/dist/gridlist.main.js +102 -59
- package/dist/gridlist.main.js.map +1 -1
- package/dist/gridlist.mjs +103 -60
- package/dist/gridlist.module.js +103 -60
- package/dist/gridlist.module.js.map +1 -1
- package/dist/listbox.main.js +135 -0
- package/dist/listbox.main.js.map +1 -0
- package/dist/listbox.mjs +130 -0
- package/dist/listbox.module.js +130 -0
- package/dist/listbox.module.js.map +1 -0
- package/dist/main.js.map +1 -1
- package/dist/menu.main.js +195 -161
- package/dist/menu.main.js.map +1 -1
- package/dist/menu.mjs +196 -162
- package/dist/menu.module.js +196 -162
- package/dist/menu.module.js.map +1 -1
- package/dist/module.js.map +1 -1
- package/dist/select.main.js +116 -70
- package/dist/select.main.js.map +1 -1
- package/dist/select.mjs +117 -71
- package/dist/select.module.js +117 -71
- package/dist/select.module.js.map +1 -1
- package/dist/table.main.js +170 -139
- package/dist/table.main.js.map +1 -1
- package/dist/table.mjs +171 -140
- package/dist/table.module.js +171 -140
- package/dist/table.module.js.map +1 -1
- package/dist/tabs.main.js +133 -0
- package/dist/tabs.main.js.map +1 -0
- package/dist/tabs.mjs +128 -0
- package/dist/tabs.module.js +128 -0
- package/dist/tabs.module.js.map +1 -0
- package/dist/tree.main.js +165 -0
- package/dist/tree.main.js.map +1 -0
- package/dist/tree.mjs +160 -0
- package/dist/tree.module.js +160 -0
- package/dist/tree.module.js.map +1 -0
- package/dist/types.d.ts +607 -145
- package/dist/types.d.ts.map +1 -1
- package/dist/user.main.js +15 -4
- package/dist/user.main.js.map +1 -1
- package/dist/user.mjs +15 -4
- package/dist/user.module.js +15 -4
- package/dist/user.module.js.map +1 -1
- package/package.json +2 -3
- package/src/combobox.ts +105 -36
- package/src/events.ts +1 -1
- package/src/gridlist.ts +146 -59
- package/src/index.ts +1 -1
- package/src/listbox.ts +226 -0
- package/src/menu.ts +152 -62
- package/src/select.ts +137 -44
- package/src/table.ts +129 -64
- package/src/tabs.ts +198 -0
- package/src/tree.ts +248 -0
- package/src/types.ts +133 -0
- package/src/user.ts +62 -37
package/src/table.ts
CHANGED
|
@@ -11,23 +11,29 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import {act, fireEvent, waitFor, within} from '@testing-library/react';
|
|
14
|
-
import {
|
|
14
|
+
import {GridRowActionOpts, TableTesterOpts, ToggleGridRowOpts, UserOpts} from './types';
|
|
15
15
|
import {pressElement, triggerLongPress} from './events';
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
|
|
17
|
+
interface TableToggleRowOpts extends ToggleGridRowOpts {}
|
|
18
|
+
interface TableToggleSortOpts {
|
|
19
|
+
/**
|
|
20
|
+
* The index, text, or node of the column to toggle selection for.
|
|
21
|
+
*/
|
|
22
|
+
column: number | string | HTMLElement,
|
|
23
|
+
/**
|
|
24
|
+
* What interaction type to use when sorting the column. Defaults to the interaction type set on the tester.
|
|
25
|
+
*/
|
|
26
|
+
interactionType?: UserOpts['interactionType']
|
|
19
27
|
}
|
|
28
|
+
interface TableRowActionOpts extends GridRowActionOpts {}
|
|
20
29
|
|
|
21
|
-
// TODO: Previously used logic like https://github.com/testing-library/react-testing-library/blame/c63b873072d62c858959c2a19e68f8e2cc0b11be/src/pure.js#L16
|
|
22
|
-
// but https://github.com/testing-library/dom-testing-library/issues/987#issuecomment-891901804 indicates that it may falsely indicate that fake timers are enabled
|
|
23
|
-
// when they aren't
|
|
24
30
|
export class TableTester {
|
|
25
31
|
private user;
|
|
26
32
|
private _interactionType: UserOpts['interactionType'];
|
|
27
33
|
private _advanceTimer: UserOpts['advanceTimer'];
|
|
28
34
|
private _table: HTMLElement;
|
|
29
35
|
|
|
30
|
-
constructor(opts:
|
|
36
|
+
constructor(opts: TableTesterOpts) {
|
|
31
37
|
let {root, user, interactionType, advanceTimer} = opts;
|
|
32
38
|
this.user = user;
|
|
33
39
|
this._interactionType = interactionType || 'mouse';
|
|
@@ -35,21 +41,43 @@ export class TableTester {
|
|
|
35
41
|
this._table = root;
|
|
36
42
|
}
|
|
37
43
|
|
|
38
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Set the interaction type used by the table tester.
|
|
46
|
+
*/
|
|
47
|
+
setInteractionType(type: UserOpts['interactionType']) {
|
|
39
48
|
this._interactionType = type;
|
|
40
|
-
}
|
|
49
|
+
}
|
|
41
50
|
|
|
42
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Toggles the selection for the specified table row. Defaults to using the interaction type set on the table tester.
|
|
53
|
+
*/
|
|
54
|
+
async toggleRowSelection(opts: TableToggleRowOpts) {
|
|
43
55
|
let {
|
|
44
|
-
|
|
45
|
-
text,
|
|
56
|
+
row,
|
|
46
57
|
needsLongPress,
|
|
58
|
+
checkboxSelection = true,
|
|
47
59
|
interactionType = this._interactionType
|
|
48
60
|
} = opts;
|
|
49
61
|
|
|
50
|
-
|
|
62
|
+
if (typeof row === 'string' || typeof row === 'number') {
|
|
63
|
+
row = this.findRow({rowIndexOrText: row});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!row) {
|
|
67
|
+
throw new Error('Target row not found in the table.');
|
|
68
|
+
}
|
|
69
|
+
|
|
51
70
|
let rowCheckbox = within(row).queryByRole('checkbox');
|
|
52
|
-
|
|
71
|
+
|
|
72
|
+
if (interactionType === 'keyboard' && !checkboxSelection) {
|
|
73
|
+
// TODO: for now focus the row directly until I add keyboard navigation
|
|
74
|
+
await act(async () => {
|
|
75
|
+
row.focus();
|
|
76
|
+
});
|
|
77
|
+
await this.user.keyboard('{Space}');
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (rowCheckbox && checkboxSelection) {
|
|
53
81
|
await pressElement(this.user, rowCheckbox, interactionType);
|
|
54
82
|
} else {
|
|
55
83
|
let cell = within(row).getAllByRole('gridcell')[0];
|
|
@@ -70,21 +98,25 @@ export class TableTester {
|
|
|
70
98
|
}
|
|
71
99
|
};
|
|
72
100
|
|
|
73
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Toggles the sort order for the specified table column. Defaults to using the interaction type set on the table tester.
|
|
103
|
+
*/
|
|
104
|
+
async toggleSort(opts: TableToggleSortOpts) {
|
|
74
105
|
let {
|
|
75
|
-
|
|
76
|
-
text,
|
|
106
|
+
column,
|
|
77
107
|
interactionType = this._interactionType
|
|
78
108
|
} = opts;
|
|
79
109
|
|
|
80
110
|
let columnheader;
|
|
81
|
-
if (
|
|
82
|
-
columnheader = this.columns[
|
|
83
|
-
} else if (
|
|
84
|
-
columnheader = within(this.rowGroups[0]).getByText(
|
|
111
|
+
if (typeof column === 'number') {
|
|
112
|
+
columnheader = this.columns[column];
|
|
113
|
+
} else if (typeof column === 'string') {
|
|
114
|
+
columnheader = within(this.rowGroups[0]).getByText(column);
|
|
85
115
|
while (columnheader && !/columnheader/.test(columnheader.getAttribute('role'))) {
|
|
86
116
|
columnheader = columnheader.parentElement;
|
|
87
117
|
}
|
|
118
|
+
} else {
|
|
119
|
+
columnheader = column;
|
|
88
120
|
}
|
|
89
121
|
|
|
90
122
|
let menuButton = within(columnheader).queryByRole('button');
|
|
@@ -154,38 +186,46 @@ export class TableTester {
|
|
|
154
186
|
} else {
|
|
155
187
|
await pressElement(this.user, columnheader, interactionType);
|
|
156
188
|
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Triggers the action for the specified table row. Defaults to using the interaction type set on the table tester.
|
|
193
|
+
*/
|
|
194
|
+
async triggerRowAction(opts: TableRowActionOpts) {
|
|
162
195
|
let {
|
|
163
|
-
|
|
164
|
-
text,
|
|
196
|
+
row,
|
|
165
197
|
needsDoubleClick,
|
|
166
198
|
interactionType = this._interactionType
|
|
167
199
|
} = opts;
|
|
168
200
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (needsDoubleClick) {
|
|
172
|
-
await this.user.dblClick(row);
|
|
173
|
-
} else if (interactionType === 'keyboard') {
|
|
174
|
-
act(() => row.focus());
|
|
175
|
-
await this.user.keyboard('[Enter]');
|
|
176
|
-
} else {
|
|
177
|
-
await pressElement(this.user, row, interactionType);
|
|
178
|
-
}
|
|
201
|
+
if (typeof row === 'string' || typeof row === 'number') {
|
|
202
|
+
row = this.findRow({rowIndexOrText: row});
|
|
179
203
|
}
|
|
180
|
-
|
|
204
|
+
|
|
205
|
+
if (!row) {
|
|
206
|
+
throw new Error('Target row not found in the table.');
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (needsDoubleClick) {
|
|
210
|
+
await this.user.dblClick(row);
|
|
211
|
+
} else if (interactionType === 'keyboard') {
|
|
212
|
+
// TODO: add keyboard navigation instead of focusing the row directly. Will need to consider if the focus in in the columns
|
|
213
|
+
act(() => row.focus());
|
|
214
|
+
await this.user.keyboard('[Enter]');
|
|
215
|
+
} else {
|
|
216
|
+
await pressElement(this.user, row, interactionType);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
181
219
|
|
|
182
220
|
// TODO: should there be utils for drag and drop and column resizing? For column resizing, I'm not entirely convinced that users will be doing that in their tests.
|
|
183
221
|
// For DnD, it might be tricky to do for keyboard DnD since we wouldn't know what valid drop zones there are... Similarly, for simulating mouse drag and drop the coordinates depend
|
|
184
222
|
// on the mocks the user sets up for their row height/etc.
|
|
185
223
|
// Additionally, should we also support keyboard navigation/typeahead? Those felt like they could be very easily replicated by the user via user.keyboard already and don't really
|
|
186
224
|
// add much value if we provide that to them
|
|
187
|
-
|
|
188
|
-
|
|
225
|
+
/**
|
|
226
|
+
* Toggle selection for all rows in the table. Defaults to using the interaction type set on the table tester.
|
|
227
|
+
*/
|
|
228
|
+
async toggleSelectAll(opts: {interactionType?: UserOpts['interactionType']} = {}) {
|
|
189
229
|
let {
|
|
190
230
|
interactionType = this._interactionType
|
|
191
231
|
} = opts;
|
|
@@ -196,30 +236,35 @@ export class TableTester {
|
|
|
196
236
|
} else {
|
|
197
237
|
await pressElement(this.user, checkbox, interactionType);
|
|
198
238
|
}
|
|
199
|
-
}
|
|
239
|
+
}
|
|
200
240
|
|
|
201
|
-
|
|
241
|
+
/**
|
|
242
|
+
* Returns a row matching the specified index or text content.
|
|
243
|
+
*/
|
|
244
|
+
findRow(opts: {rowIndexOrText: number | string}): HTMLElement {
|
|
202
245
|
let {
|
|
203
|
-
|
|
204
|
-
text
|
|
246
|
+
rowIndexOrText
|
|
205
247
|
} = opts;
|
|
206
248
|
|
|
207
249
|
let row;
|
|
208
250
|
let rows = this.rows;
|
|
209
251
|
let bodyRowGroup = this.rowGroups[1];
|
|
210
|
-
if (
|
|
211
|
-
row = rows[
|
|
212
|
-
} else if (
|
|
213
|
-
row = within(bodyRowGroup).getByText(
|
|
252
|
+
if (typeof rowIndexOrText === 'number') {
|
|
253
|
+
row = rows[rowIndexOrText];
|
|
254
|
+
} else if (typeof rowIndexOrText === 'string') {
|
|
255
|
+
row = within(bodyRowGroup).getByText(rowIndexOrText);
|
|
214
256
|
while (row && row.getAttribute('role') !== 'row') {
|
|
215
257
|
row = row.parentElement;
|
|
216
258
|
}
|
|
217
259
|
}
|
|
218
260
|
|
|
219
261
|
return row;
|
|
220
|
-
}
|
|
262
|
+
}
|
|
221
263
|
|
|
222
|
-
|
|
264
|
+
/**
|
|
265
|
+
* Returns a cell matching the specified text content.
|
|
266
|
+
*/
|
|
267
|
+
findCell(opts: {text: string}) {
|
|
223
268
|
let {
|
|
224
269
|
text
|
|
225
270
|
} = opts;
|
|
@@ -236,38 +281,58 @@ export class TableTester {
|
|
|
236
281
|
}
|
|
237
282
|
|
|
238
283
|
return cell;
|
|
239
|
-
}
|
|
284
|
+
}
|
|
240
285
|
|
|
241
|
-
|
|
286
|
+
/**
|
|
287
|
+
* Returns the table.
|
|
288
|
+
*/
|
|
289
|
+
get table(): HTMLElement {
|
|
242
290
|
return this._table;
|
|
243
291
|
}
|
|
244
292
|
|
|
245
|
-
|
|
293
|
+
/**
|
|
294
|
+
* Returns the row groups within the table.
|
|
295
|
+
*/
|
|
296
|
+
get rowGroups(): HTMLElement[] {
|
|
246
297
|
let table = this._table;
|
|
247
298
|
return table ? within(table).queryAllByRole('rowgroup') : [];
|
|
248
299
|
}
|
|
249
300
|
|
|
250
|
-
|
|
301
|
+
/**
|
|
302
|
+
* Returns the columns within the table.
|
|
303
|
+
*/
|
|
304
|
+
get columns(): HTMLElement[] {
|
|
251
305
|
let headerRowGroup = this.rowGroups[0];
|
|
252
306
|
return headerRowGroup ? within(headerRowGroup).queryAllByRole('columnheader') : [];
|
|
253
307
|
}
|
|
254
308
|
|
|
255
|
-
|
|
309
|
+
/**
|
|
310
|
+
* Returns the rows within the table if any.
|
|
311
|
+
*/
|
|
312
|
+
get rows(): HTMLElement[] {
|
|
256
313
|
let bodyRowGroup = this.rowGroups[1];
|
|
257
314
|
return bodyRowGroup ? within(bodyRowGroup).queryAllByRole('row') : [];
|
|
258
315
|
}
|
|
259
316
|
|
|
260
|
-
|
|
317
|
+
/**
|
|
318
|
+
* Returns the currently selected rows within the table if any.
|
|
319
|
+
*/
|
|
320
|
+
get selectedRows(): HTMLElement[] {
|
|
261
321
|
return this.rows.filter(row => row.getAttribute('aria-selected') === 'true');
|
|
262
322
|
}
|
|
263
323
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
324
|
+
/**
|
|
325
|
+
* Returns the row headers within the table if any.
|
|
326
|
+
*/
|
|
327
|
+
get rowHeaders(): HTMLElement[] {
|
|
328
|
+
return within(this.table).queryAllByRole('rowheader');
|
|
267
329
|
}
|
|
268
330
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
331
|
+
/**
|
|
332
|
+
* Returns the cells within the table if any. Can be filtered against a specific row if provided via `element`.
|
|
333
|
+
*/
|
|
334
|
+
cells(opts: {element?: HTMLElement} = {}): HTMLElement[] {
|
|
335
|
+
let {element = this.table} = opts;
|
|
336
|
+
return within(element).queryAllByRole('gridcell');
|
|
272
337
|
}
|
|
273
338
|
}
|
package/src/tabs.ts
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2024 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 {act, within} from '@testing-library/react';
|
|
14
|
+
import {Direction, Orientation, TabsTesterOpts, UserOpts} from './types';
|
|
15
|
+
import {pressElement} from './events';
|
|
16
|
+
|
|
17
|
+
interface TriggerTabOptions {
|
|
18
|
+
/**
|
|
19
|
+
* What interaction type to use when triggering a tab. Defaults to the interaction type set on the tester.
|
|
20
|
+
*/
|
|
21
|
+
interactionType?: UserOpts['interactionType'],
|
|
22
|
+
/**
|
|
23
|
+
* The index, text, or node of the tab to toggle selection for.
|
|
24
|
+
*/
|
|
25
|
+
tab: number | string | HTMLElement,
|
|
26
|
+
/**
|
|
27
|
+
* Whether the tab needs to be activated manually rather than on focus.
|
|
28
|
+
*/
|
|
29
|
+
manualActivation?: boolean
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class TabsTester {
|
|
33
|
+
private user;
|
|
34
|
+
private _interactionType: UserOpts['interactionType'];
|
|
35
|
+
private _tablist: HTMLElement;
|
|
36
|
+
private _direction: Direction;
|
|
37
|
+
|
|
38
|
+
constructor(opts: TabsTesterOpts) {
|
|
39
|
+
let {root, user, interactionType, direction} = opts;
|
|
40
|
+
this.user = user;
|
|
41
|
+
this._interactionType = interactionType || 'mouse';
|
|
42
|
+
this._direction = direction || 'ltr';
|
|
43
|
+
|
|
44
|
+
this._tablist = root;
|
|
45
|
+
let tablist = within(root).queryAllByRole('tablist');
|
|
46
|
+
if (tablist.length > 0) {
|
|
47
|
+
this._tablist = tablist[0];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Set the interaction type used by the tabs tester.
|
|
53
|
+
*/
|
|
54
|
+
setInteractionType(type: UserOpts['interactionType']) {
|
|
55
|
+
this._interactionType = type;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// TODO: This is pretty similar across most the utils, refactor to make it generic?
|
|
59
|
+
/**
|
|
60
|
+
* Returns a tab matching the specified index or text content.
|
|
61
|
+
*/
|
|
62
|
+
findTab(opts: {tabIndexOrText: number | string}): HTMLElement {
|
|
63
|
+
let {
|
|
64
|
+
tabIndexOrText
|
|
65
|
+
} = opts;
|
|
66
|
+
|
|
67
|
+
let tab;
|
|
68
|
+
let tabs = this.tabs;
|
|
69
|
+
if (typeof tabIndexOrText === 'number') {
|
|
70
|
+
tab = tabs[tabIndexOrText];
|
|
71
|
+
} else if (typeof tabIndexOrText === 'string') {
|
|
72
|
+
tab = (within(this._tablist).getByText(tabIndexOrText).closest('[role=tab]'))! as HTMLElement;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return tab;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// TODO: also quite similar across more utils albeit with orientation, refactor to make generic
|
|
79
|
+
private async keyboardNavigateToTab(opts: {tab: HTMLElement, orientation?: Orientation}) {
|
|
80
|
+
let {tab, orientation = 'vertical'} = opts;
|
|
81
|
+
let tabs = this.tabs;
|
|
82
|
+
let targetIndex = tabs.indexOf(tab);
|
|
83
|
+
if (targetIndex === -1) {
|
|
84
|
+
throw new Error('Tab provided is not in the tablist');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!this._tablist.contains(document.activeElement)) {
|
|
88
|
+
let selectedTab = this.selectedTab;
|
|
89
|
+
if (selectedTab != null) {
|
|
90
|
+
act(() => selectedTab.focus());
|
|
91
|
+
} else {
|
|
92
|
+
act(() => tabs.find(tab => !(tab.hasAttribute('disabled') || tab.getAttribute('aria-disabled') === 'true'))?.focus());
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let currIndex = this.tabs.indexOf(document.activeElement as HTMLElement);
|
|
97
|
+
if (currIndex === -1) {
|
|
98
|
+
throw new Error('ActiveElement is not in the tablist');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let arrowUp = 'ArrowUp';
|
|
102
|
+
let arrowDown = 'ArrowDown';
|
|
103
|
+
if (orientation === 'horizontal') {
|
|
104
|
+
if (this._direction === 'ltr') {
|
|
105
|
+
arrowUp = 'ArrowLeft';
|
|
106
|
+
arrowDown = 'ArrowRight';
|
|
107
|
+
} else {
|
|
108
|
+
arrowUp = 'ArrowRight';
|
|
109
|
+
arrowDown = 'ArrowLeft';
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let movementDirection = targetIndex > currIndex ? 'down' : 'up';
|
|
114
|
+
for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {
|
|
115
|
+
await this.user.keyboard(`[${movementDirection === 'down' ? arrowDown : arrowUp}]`);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Triggers the specified tab. Defaults to using the interaction type set on the tabs tester.
|
|
121
|
+
*/
|
|
122
|
+
async triggerTab(opts: TriggerTabOptions) {
|
|
123
|
+
let {
|
|
124
|
+
tab,
|
|
125
|
+
interactionType = this._interactionType,
|
|
126
|
+
manualActivation
|
|
127
|
+
} = opts;
|
|
128
|
+
|
|
129
|
+
if (typeof tab === 'string' || typeof tab === 'number') {
|
|
130
|
+
tab = this.findTab({tabIndexOrText: tab});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (!tab) {
|
|
134
|
+
throw new Error('Target tab not found in the tablist.');
|
|
135
|
+
} else if (tab.hasAttribute('disabled')) {
|
|
136
|
+
throw new Error('Target tab is disabled.');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (interactionType === 'keyboard') {
|
|
140
|
+
if (document.activeElement !== this._tablist || !this._tablist.contains(document.activeElement)) {
|
|
141
|
+
act(() => this._tablist.focus());
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
let tabsOrientation = this._tablist.getAttribute('aria-orientation') || 'horizontal';
|
|
145
|
+
await this.keyboardNavigateToTab({tab, orientation: tabsOrientation as Orientation});
|
|
146
|
+
if (manualActivation) {
|
|
147
|
+
await this.user.keyboard('[Enter]');
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
await pressElement(this.user, tab, interactionType);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Returns the tablist.
|
|
156
|
+
*/
|
|
157
|
+
get tablist(): HTMLElement {
|
|
158
|
+
return this._tablist;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Returns the tabpanels.
|
|
163
|
+
*/
|
|
164
|
+
get tabpanels(): HTMLElement[] {
|
|
165
|
+
let tabpanels = [] as HTMLElement[];
|
|
166
|
+
for (let tab of this.tabs) {
|
|
167
|
+
let controlId = tab.getAttribute('aria-controls');
|
|
168
|
+
let panel = controlId != null ? document.getElementById(controlId) : null;
|
|
169
|
+
if (panel != null) {
|
|
170
|
+
tabpanels.push(panel);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return tabpanels;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Returns the tabs in the tablist.
|
|
179
|
+
*/
|
|
180
|
+
get tabs(): HTMLElement[] {
|
|
181
|
+
return within(this.tablist).queryAllByRole('tab');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Returns the currently selected tab in the tablist if any.
|
|
186
|
+
*/
|
|
187
|
+
get selectedTab(): HTMLElement | null {
|
|
188
|
+
return this.tabs.find(tab => tab.getAttribute('aria-selected') === 'true') || null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Returns the currently active tabpanel if any.
|
|
193
|
+
*/
|
|
194
|
+
get activeTabpanel(): HTMLElement | null {
|
|
195
|
+
let activeTabpanelId = this.selectedTab?.getAttribute('aria-controls');
|
|
196
|
+
return activeTabpanelId ? document.getElementById(activeTabpanelId) : null;
|
|
197
|
+
}
|
|
198
|
+
}
|