@sprinklrjs/spaceweb 14.16.1 → 14.16.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/avatar/avatar.js CHANGED
@@ -31,9 +31,9 @@ var getIsImageLoaded = function (src) {
31
31
  return imageEl.complete;
32
32
  };
33
33
  var Icon = function (_a) {
34
- var overrides = _a.overrides, className = _a.className, props = tslib_1.__rest(_a, ["overrides", "className"]);
34
+ var overrides = _a.overrides, className = _a.className, title = _a.title, props = tslib_1.__rest(_a, ["overrides", "className", "title"]);
35
35
  var _b = tslib_1.__read((0, overrides_1.useOverrides)(overrides === null || overrides === void 0 ? void 0 : overrides.UserIcon, user_1.default), 2), UserIcon = _b[0], iconProps = _b[1];
36
- return ((0, jsx_runtime_1.jsx)(box_1.Box, tslib_1.__assign({ className: ['h-full w-full flex items-center justify-center', className] }, props, { children: (0, jsx_runtime_1.jsx)(UserIcon, tslib_1.__assign({ "data-testid": "user-icon", className: "w-1/2 h-1/2" }, iconProps)) })));
36
+ return ((0, jsx_runtime_1.jsx)(box_1.Box, tslib_1.__assign({ className: ['h-full w-full flex items-center justify-center', className] }, props, { children: (0, jsx_runtime_1.jsx)(UserIcon, tslib_1.__assign({ "data-testid": "user-icon", className: "w-1/2 h-1/2" }, iconProps, { title: title })) })));
37
37
  };
38
38
  var Avatar = function (props) {
39
39
  var _a = tslib_1.__read((0, react_1.useState)(null), 2), imageEl = _a[0], setImageEl = _a[1];
@@ -92,7 +92,7 @@ var Avatar = function (props) {
92
92
  style,
93
93
  rootProps === null || rootProps === void 0 ? void 0 : rootProps.style,
94
94
  ] }, { children: [!imageLoaded &&
95
- (isPhoneNumber && !allowPhoneInitials ? ((0, jsx_runtime_1.jsx)(Icon, tslib_1.__assign({ overrides: overrides }, sharedProps, initialsProps))) : ((0, jsx_runtime_1.jsx)(Initials, tslib_1.__assign({}, sharedProps, initialsProps, { children: _initials })))), (0, jsx_runtime_1.jsx)(AvatarImage, tslib_1.__assign({ ref: setImageEl, src: src, alt: name }, sharedProps, avatarImageProps))] })));
95
+ (isPhoneNumber && !allowPhoneInitials ? ((0, jsx_runtime_1.jsx)(Icon, tslib_1.__assign({ overrides: overrides, title: name }, sharedProps, initialsProps))) : ((0, jsx_runtime_1.jsx)(Initials, tslib_1.__assign({}, sharedProps, initialsProps, { children: _initials })))), (0, jsx_runtime_1.jsx)(AvatarImage, tslib_1.__assign({ ref: setImageEl, src: src, alt: name }, sharedProps, avatarImageProps))] })));
96
96
  };
97
97
  exports.Avatar = Avatar;
98
98
  Avatar.__standard = 'Component';
@@ -16,6 +16,15 @@ var date_helpers_1 = tslib_1.__importDefault(require("./utils/date-helpers"));
16
16
  var overrides_1 = require("../../overrides");
17
17
  var focusVisible_1 = require("../utils/focusVisible");
18
18
  var constants_1 = require("./constants");
19
+ // Month/year menu keeps focus while open. dayElm.focus() fights Menu blur and Popover focusLock.
20
+ function shouldSkipCalendarDayFocus(dayElm) {
21
+ var activeElement = document.activeElement;
22
+ if (!(activeElement instanceof Element))
23
+ return false;
24
+ if (activeElement === dayElm)
25
+ return true;
26
+ return !!activeElement.closest('[role="menu"], [role="listbox"]');
27
+ }
19
28
  var Day = /** @class */ (function (_super) {
20
29
  tslib_1.__extends(Day, _super);
21
30
  function Day(props) {
@@ -43,15 +52,15 @@ var Day = /** @class */ (function (_super) {
43
52
  var nextStartDate = null;
44
53
  var nextEndDate = null;
45
54
  if (_this.props.selectedInput === constants_1.INPUT_ROLE.startDate) {
46
- // @ts-ignore
55
+ // @ts-ignore -- range start accepts the selected date value
47
56
  nextStartDate = selectedDate;
48
- // @ts-ignore
57
+ // @ts-ignore -- range end copies the current end date
49
58
  nextEndDate = Array.isArray(currentDate) && currentDate[1] ? currentDate[1] : null;
50
59
  }
51
60
  else if (_this.props.selectedInput === constants_1.INPUT_ROLE.endDate) {
52
- // @ts-ignore
61
+ // @ts-ignore -- range start copies the current start date
53
62
  nextStartDate = Array.isArray(currentDate) && currentDate[0] ? currentDate[0] : null;
54
- // @ts-ignore
63
+ // @ts-ignore -- range end accepts the selected date value
55
64
  nextEndDate = selectedDate;
56
65
  }
57
66
  nextDate = [nextStartDate];
@@ -171,14 +180,18 @@ var Day = /** @class */ (function (_super) {
171
180
  Day.prototype.componentDidMount = function () {
172
181
  if (this.dayElm && this.props.focusedCalendar) {
173
182
  if (this.props.highlighted || (!this.props.highlightedDate && this.isSelected())) {
174
- this.dayElm.focus();
183
+ if (!shouldSkipCalendarDayFocus(this.dayElm)) {
184
+ this.dayElm.focus();
185
+ }
175
186
  }
176
187
  }
177
188
  };
178
189
  Day.prototype.componentDidUpdate = function (_prevProps) {
179
190
  if (this.dayElm && this.props.focusedCalendar) {
180
191
  if (this.props.highlighted || (!this.props.highlightedDate && this.isSelected())) {
181
- this.dayElm.focus();
192
+ if (!shouldSkipCalendarDayFocus(this.dayElm)) {
193
+ this.dayElm.focus();
194
+ }
182
195
  }
183
196
  }
184
197
  };
@@ -320,7 +333,7 @@ var Day = /** @class */ (function (_super) {
320
333
  (!this.props.highlightedDate && this.isSelected()) ||
321
334
  hasFallbackTabIndex;
322
335
  return !peekNextMonth && sharedProps.$outsideMonth ? ((0, jsx_runtime_1.jsx)(DayComp, tslib_1.__assign({ role: "gridcell" }, sharedProps, dayProps, { onFocus: this.onFocus, onBlur: this.onBlur }))) : ((0, jsx_runtime_1.jsxs)(DayComp, tslib_1.__assign({ "aria-label": this.getAriaLabel(sharedProps),
323
- // @ts-ignore
336
+ // @ts-ignore -- styled day ref callback is untyped
324
337
  ref: function (dayElm) {
325
338
  _this.dayElm = dayElm;
326
339
  }, role: "gridcell", "aria-roledescription": "button", tabIndex: isTabbable ? 0 : -1 }, sharedProps, dayProps, {
@@ -344,7 +357,7 @@ var Day = /** @class */ (function (_super) {
344
357
  onMouseLeave: noop_1.default,
345
358
  overrides: {},
346
359
  peekNextMonth: true,
347
- // @ts-ignore
360
+ // @ts-ignore -- default value is null before a date is selected
348
361
  value: null,
349
362
  };
350
363
  return Day;
@@ -216,6 +216,9 @@ var DateInput = function (_a) {
216
216
  if (isOpen) {
217
217
  closeDatePicker();
218
218
  }
219
+ else {
220
+ setParsedDate();
221
+ }
219
222
  // on blur, clear the input if the value is malformed
220
223
  if (!value && inputValue) {
221
224
  setInputValue('');
@@ -28,9 +28,9 @@ var getIsImageLoaded = function (src) {
28
28
  return imageEl.complete;
29
29
  };
30
30
  var Icon = function (_a) {
31
- var overrides = _a.overrides, className = _a.className, props = __rest(_a, ["overrides", "className"]);
31
+ var overrides = _a.overrides, className = _a.className, title = _a.title, props = __rest(_a, ["overrides", "className", "title"]);
32
32
  var _b = __read(useOverrides(overrides === null || overrides === void 0 ? void 0 : overrides.UserIcon, DefaultUserIcon), 2), UserIcon = _b[0], iconProps = _b[1];
33
- return (_jsx(Box, __assign({ className: ['h-full w-full flex items-center justify-center', className] }, props, { children: _jsx(UserIcon, __assign({ "data-testid": "user-icon", className: "w-1/2 h-1/2" }, iconProps)) })));
33
+ return (_jsx(Box, __assign({ className: ['h-full w-full flex items-center justify-center', className] }, props, { children: _jsx(UserIcon, __assign({ "data-testid": "user-icon", className: "w-1/2 h-1/2" }, iconProps, { title: title })) })));
34
34
  };
35
35
  var Avatar = function (props) {
36
36
  var _a = __read(useState(null), 2), imageEl = _a[0], setImageEl = _a[1];
@@ -89,7 +89,7 @@ var Avatar = function (props) {
89
89
  style,
90
90
  rootProps === null || rootProps === void 0 ? void 0 : rootProps.style,
91
91
  ] }, { children: [!imageLoaded &&
92
- (isPhoneNumber && !allowPhoneInitials ? (_jsx(Icon, __assign({ overrides: overrides }, sharedProps, initialsProps))) : (_jsx(Initials, __assign({}, sharedProps, initialsProps, { children: _initials })))), _jsx(AvatarImage, __assign({ ref: setImageEl, src: src, alt: name }, sharedProps, avatarImageProps))] })));
92
+ (isPhoneNumber && !allowPhoneInitials ? (_jsx(Icon, __assign({ overrides: overrides, title: name }, sharedProps, initialsProps))) : (_jsx(Initials, __assign({}, sharedProps, initialsProps, { children: _initials })))), _jsx(AvatarImage, __assign({ ref: setImageEl, src: src, alt: name }, sharedProps, avatarImageProps))] })));
93
93
  };
94
94
  Avatar.__standard = 'Component';
95
95
  export { Avatar };
@@ -14,6 +14,15 @@ import DateHelpers from './utils/date-helpers';
14
14
  import { getOverrides } from '../../overrides';
15
15
  import { isFocusVisible } from '../utils/focusVisible';
16
16
  import { INPUT_ROLE } from './constants';
17
+ // Month/year menu keeps focus while open. dayElm.focus() fights Menu blur and Popover focusLock.
18
+ function shouldSkipCalendarDayFocus(dayElm) {
19
+ var activeElement = document.activeElement;
20
+ if (!(activeElement instanceof Element))
21
+ return false;
22
+ if (activeElement === dayElm)
23
+ return true;
24
+ return !!activeElement.closest('[role="menu"], [role="listbox"]');
25
+ }
17
26
  var Day = /** @class */ (function (_super) {
18
27
  __extends(Day, _super);
19
28
  function Day(props) {
@@ -41,15 +50,15 @@ var Day = /** @class */ (function (_super) {
41
50
  var nextStartDate = null;
42
51
  var nextEndDate = null;
43
52
  if (_this.props.selectedInput === INPUT_ROLE.startDate) {
44
- // @ts-ignore
53
+ // @ts-ignore -- range start accepts the selected date value
45
54
  nextStartDate = selectedDate;
46
- // @ts-ignore
55
+ // @ts-ignore -- range end copies the current end date
47
56
  nextEndDate = Array.isArray(currentDate) && currentDate[1] ? currentDate[1] : null;
48
57
  }
49
58
  else if (_this.props.selectedInput === INPUT_ROLE.endDate) {
50
- // @ts-ignore
59
+ // @ts-ignore -- range start copies the current start date
51
60
  nextStartDate = Array.isArray(currentDate) && currentDate[0] ? currentDate[0] : null;
52
- // @ts-ignore
61
+ // @ts-ignore -- range end accepts the selected date value
53
62
  nextEndDate = selectedDate;
54
63
  }
55
64
  nextDate = [nextStartDate];
@@ -169,14 +178,18 @@ var Day = /** @class */ (function (_super) {
169
178
  Day.prototype.componentDidMount = function () {
170
179
  if (this.dayElm && this.props.focusedCalendar) {
171
180
  if (this.props.highlighted || (!this.props.highlightedDate && this.isSelected())) {
172
- this.dayElm.focus();
181
+ if (!shouldSkipCalendarDayFocus(this.dayElm)) {
182
+ this.dayElm.focus();
183
+ }
173
184
  }
174
185
  }
175
186
  };
176
187
  Day.prototype.componentDidUpdate = function (_prevProps) {
177
188
  if (this.dayElm && this.props.focusedCalendar) {
178
189
  if (this.props.highlighted || (!this.props.highlightedDate && this.isSelected())) {
179
- this.dayElm.focus();
190
+ if (!shouldSkipCalendarDayFocus(this.dayElm)) {
191
+ this.dayElm.focus();
192
+ }
180
193
  }
181
194
  }
182
195
  };
@@ -318,7 +331,7 @@ var Day = /** @class */ (function (_super) {
318
331
  (!this.props.highlightedDate && this.isSelected()) ||
319
332
  hasFallbackTabIndex;
320
333
  return !peekNextMonth && sharedProps.$outsideMonth ? (_jsx(DayComp, __assign({ role: "gridcell" }, sharedProps, dayProps, { onFocus: this.onFocus, onBlur: this.onBlur }))) : (_jsxs(DayComp, __assign({ "aria-label": this.getAriaLabel(sharedProps),
321
- // @ts-ignore
334
+ // @ts-ignore -- styled day ref callback is untyped
322
335
  ref: function (dayElm) {
323
336
  _this.dayElm = dayElm;
324
337
  }, role: "gridcell", "aria-roledescription": "button", tabIndex: isTabbable ? 0 : -1 }, sharedProps, dayProps, {
@@ -342,7 +355,7 @@ var Day = /** @class */ (function (_super) {
342
355
  onMouseLeave: _noop,
343
356
  overrides: {},
344
357
  peekNextMonth: true,
345
- // @ts-ignore
358
+ // @ts-ignore -- default value is null before a date is selected
346
359
  value: null,
347
360
  };
348
361
  return Day;
@@ -213,6 +213,9 @@ export var DateInput = function (_a) {
213
213
  if (isOpen) {
214
214
  closeDatePicker();
215
215
  }
216
+ else {
217
+ setParsedDate();
218
+ }
216
219
  // on blur, clear the input if the value is malformed
217
220
  if (!value && inputValue) {
218
221
  setInputValue('');
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Detects a focus trap between two controls.
3
+ *
4
+ * Moves focus. Tab from `last` must land on `first`, and Shift+Tab from `first`
5
+ * must land on `last`. Render the dialog or popover first, then pass the
6
+ * focusable ends inside it.
7
+ *
8
+ * ```ts
9
+ * import { isFocusLocked } from '@sprinklrjs/spaceweb/test-utils/keyboard';
10
+ *
11
+ * expect(isFocusLocked({ first, last })).toBe(true);
12
+ * ```
13
+ *
14
+ * Test-only. Requires `@testing-library/user-event` v13 and `@testing-library/react`.
15
+ */
16
+ export declare function isFocusLocked(ends: {
17
+ first: HTMLElement;
18
+ last: HTMLElement;
19
+ }): boolean;
@@ -0,0 +1,31 @@
1
+ import { act } from '@testing-library/react';
2
+ import { shiftTab, tab } from './press';
3
+ /**
4
+ * Detects a focus trap between two controls.
5
+ *
6
+ * Moves focus. Tab from `last` must land on `first`, and Shift+Tab from `first`
7
+ * must land on `last`. Render the dialog or popover first, then pass the
8
+ * focusable ends inside it.
9
+ *
10
+ * ```ts
11
+ * import { isFocusLocked } from '@sprinklrjs/spaceweb/test-utils/keyboard';
12
+ *
13
+ * expect(isFocusLocked({ first, last })).toBe(true);
14
+ * ```
15
+ *
16
+ * Test-only. Requires `@testing-library/user-event` v13 and `@testing-library/react`.
17
+ */
18
+ export function isFocusLocked(ends) {
19
+ var first = ends.first, last = ends.last;
20
+ act(function () {
21
+ last.focus();
22
+ });
23
+ tab();
24
+ var wrapsForward = document.activeElement === first;
25
+ act(function () {
26
+ first.focus();
27
+ });
28
+ shiftTab();
29
+ var wrapsBackward = document.activeElement === last;
30
+ return wrapsForward && wrapsBackward;
31
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Test-only keyboard helpers for Jest.
3
+ *
4
+ * ```ts
5
+ * import { isFocusLocked, press, tab } from '@sprinklrjs/spaceweb/test-utils/keyboard';
6
+ * ```
7
+ *
8
+ * Requires `@testing-library/user-event` v13 and `@testing-library/react`.
9
+ * The `describe*` suites also need `@testing-library/jest-dom` (`toHaveFocus`, `toBeChecked`).
10
+ * Dialog and radio suites assume the fixture names in their JSDoc (a button named
11
+ * `Second`, a button named `After`). Do not import this from application code.
12
+ */
13
+ export { isFocusLocked } from './focusLock';
14
+ export { press, shiftTab, tab, type, type KeyboardKey } from './press';
15
+ export { describeDialogKeyboard, describeRadioGroupKeyboard, describeRovingKeyboard, describeToggleKeyboard, } from './patterns';
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Test-only keyboard helpers for Jest.
3
+ *
4
+ * ```ts
5
+ * import { isFocusLocked, press, tab } from '@sprinklrjs/spaceweb/test-utils/keyboard';
6
+ * ```
7
+ *
8
+ * Requires `@testing-library/user-event` v13 and `@testing-library/react`.
9
+ * The `describe*` suites also need `@testing-library/jest-dom` (`toHaveFocus`, `toBeChecked`).
10
+ * Dialog and radio suites assume the fixture names in their JSDoc (a button named
11
+ * `Second`, a button named `After`). Do not import this from application code.
12
+ */
13
+ export { isFocusLocked } from './focusLock';
14
+ export { press, shiftTab, tab, type } from './press';
15
+ export { describeDialogKeyboard, describeRadioGroupKeyboard, describeRovingKeyboard, describeToggleKeyboard, } from './patterns';
@@ -0,0 +1,63 @@
1
+ import { type KeyboardKey } from './press';
2
+ /**
3
+ * Shared keyboard contracts for Spaceweb.
4
+ *
5
+ * Add a `describe*` function when a new interactive component shares a contract
6
+ * other components will reuse (menu, listbox, combobox, slider). Call it from
7
+ * that component's spec, inside `describe('keyboard interaction')`. Keep one-off branches in the component file.
8
+ * These tests press keys only — no click or hover.
9
+ *
10
+ * | Pattern | Keys | Components |
11
+ * | toggle | Tab, Space, Enter does not toggle | Checkbox, Switch |
12
+ * | radio | Tab lands on the active radio, Space selects, arrows move within the group | RadioGroup |
13
+ * | roving | next/previous, wrap, Home, End | Tabs. ButtonGroup uses the same hook |
14
+ * | dialog | Escape, Tab trap, focus return | Modal |
15
+ */
16
+ type RenderFn = () => void;
17
+ export declare function describeToggleKeyboard(options: {
18
+ role: 'checkbox' | 'switch';
19
+ /** Renders one unchecked, enabled control. Space must check it. */
20
+ renderEnabled: RenderFn;
21
+ /** Renders one unchecked, disabled control. Space must not check it. */
22
+ renderDisabled: RenderFn;
23
+ }): void;
24
+ export declare function describeRadioGroupKeyboard(options: {
25
+ /** Renders Apple and Pear. `value` selects one. Include a button named After after the group. */
26
+ renderGroup: (props?: {
27
+ value?: string;
28
+ disabled?: boolean;
29
+ }) => void;
30
+ }): void;
31
+ /**
32
+ * Roving focus across `setup()` stops. The first stop is the tab entry.
33
+ * `setup` must return at least three stops in keyboard order.
34
+ */
35
+ export declare function describeRovingKeyboard(options: {
36
+ name: string;
37
+ setup: () => HTMLElement[];
38
+ nextKey: KeyboardKey;
39
+ previousKey: KeyboardKey;
40
+ ignoredKeys?: KeyboardKey[];
41
+ }): void;
42
+ export declare function describeDialogKeyboard(options: {
43
+ /**
44
+ * Renders one dialog. `onClose` must unmount it. `first` and `last` are the
45
+ * focusable ends inside the dialog. Include a button named Second between them
46
+ * when the dialog is closeable.
47
+ */
48
+ mount: (props?: {
49
+ closeable?: boolean;
50
+ focusLock?: boolean;
51
+ autoFocus?: boolean;
52
+ }) => {
53
+ dialog: HTMLElement;
54
+ first: HTMLElement;
55
+ last: HTMLElement;
56
+ };
57
+ /**
58
+ * Renders a closed dialog and a trigger. Enter on the trigger opens the dialog.
59
+ * Escape must close it and return focus to the trigger.
60
+ */
61
+ openFromTrigger: () => HTMLElement;
62
+ }): void;
63
+ export {};
@@ -0,0 +1,178 @@
1
+ import { __awaiter, __generator, __read } from "tslib";
2
+ import { act, screen, waitFor } from '@testing-library/react';
3
+ import { isFocusLocked } from './focusLock';
4
+ import { press, tab } from './press';
5
+ export function describeToggleKeyboard(options) {
6
+ var role = options.role, renderEnabled = options.renderEnabled, renderDisabled = options.renderDisabled;
7
+ test('Tab moves focus onto the control', function () {
8
+ renderEnabled();
9
+ var control = screen.getByRole(role);
10
+ tab();
11
+ expect(control).toHaveFocus();
12
+ });
13
+ test('Space checks the control', function () {
14
+ renderEnabled();
15
+ var control = screen.getByRole(role);
16
+ tab();
17
+ press('Space');
18
+ expect(control).toBeChecked();
19
+ });
20
+ test('Enter does not check the control', function () {
21
+ renderEnabled();
22
+ var control = screen.getByRole(role);
23
+ tab();
24
+ press('Enter');
25
+ expect(control).not.toBeChecked();
26
+ });
27
+ test('Space does not check a disabled control', function () {
28
+ renderDisabled();
29
+ var control = screen.getByRole(role);
30
+ act(function () {
31
+ control.focus();
32
+ });
33
+ press('Space');
34
+ expect(control).not.toBeChecked();
35
+ });
36
+ }
37
+ export function describeRadioGroupKeyboard(options) {
38
+ test('Tab focuses the first radio when none is selected', function () {
39
+ options.renderGroup();
40
+ var _a = __read(screen.getAllByRole('radio'), 2), first = _a[0], second = _a[1];
41
+ tab();
42
+ expect(first).toHaveFocus();
43
+ expect(second).toHaveAttribute('tabindex', '-1');
44
+ });
45
+ test('Tab focuses the selected radio', function () {
46
+ options.renderGroup({ value: 'pear' });
47
+ var _a = __read(screen.getAllByRole('radio'), 2), first = _a[0], second = _a[1];
48
+ tab();
49
+ expect(second).toHaveFocus();
50
+ expect(first).toHaveAttribute('tabindex', '-1');
51
+ });
52
+ test('Tab leaves the group from the active radio', function () {
53
+ options.renderGroup({ value: 'pear' });
54
+ var second = screen.getAllByRole('radio')[1];
55
+ tab();
56
+ expect(second).toHaveFocus();
57
+ tab();
58
+ expect(screen.getByRole('button', { name: 'After' })).toHaveFocus();
59
+ });
60
+ test('Space selects the active radio', function () {
61
+ options.renderGroup();
62
+ var first = screen.getAllByRole('radio')[0];
63
+ tab();
64
+ press('Space');
65
+ expect(first).toBeChecked();
66
+ });
67
+ test('Space does not select a radio in a disabled group', function () {
68
+ options.renderGroup({ disabled: true });
69
+ var first = screen.getAllByRole('radio')[0];
70
+ act(function () {
71
+ first.focus();
72
+ });
73
+ press('Space');
74
+ expect(first).not.toBeChecked();
75
+ });
76
+ }
77
+ /**
78
+ * Roving focus across `setup()` stops. The first stop is the tab entry.
79
+ * `setup` must return at least three stops in keyboard order.
80
+ */
81
+ export function describeRovingKeyboard(options) {
82
+ var name = options.name, setup = options.setup, nextKey = options.nextKey, previousKey = options.previousKey, _a = options.ignoredKeys, ignoredKeys = _a === void 0 ? [] : _a;
83
+ describe(name, function () {
84
+ test("".concat(nextKey, " moves to the next stop and wraps to the first"), function () {
85
+ var stops = setup();
86
+ tab();
87
+ expect(stops[0]).toHaveFocus();
88
+ press(nextKey);
89
+ expect(stops[1]).toHaveFocus();
90
+ press(nextKey);
91
+ expect(stops[2]).toHaveFocus();
92
+ press(nextKey);
93
+ expect(stops[0]).toHaveFocus();
94
+ });
95
+ test("".concat(previousKey, " wraps from the first stop to the last"), function () {
96
+ var stops = setup();
97
+ tab();
98
+ press(previousKey);
99
+ expect(stops[stops.length - 1]).toHaveFocus();
100
+ });
101
+ test('Home moves to the first stop and End moves to the last', function () {
102
+ var stops = setup();
103
+ tab();
104
+ press('End');
105
+ expect(stops[stops.length - 1]).toHaveFocus();
106
+ press('Home');
107
+ expect(stops[0]).toHaveFocus();
108
+ });
109
+ if (ignoredKeys.length > 0) {
110
+ test("".concat(ignoredKeys.join(', '), " do not move focus"), function () {
111
+ var stops = setup();
112
+ tab();
113
+ ignoredKeys.forEach(function (key) {
114
+ press(key);
115
+ expect(stops[0]).toHaveFocus();
116
+ });
117
+ });
118
+ }
119
+ });
120
+ }
121
+ export function describeDialogKeyboard(options) {
122
+ var _this = this;
123
+ test('Escape closes a closeable dialog', function () {
124
+ options.mount();
125
+ press('Escape');
126
+ expect(screen.queryByRole('dialog')).toBeNull();
127
+ });
128
+ test('Escape does not close a dialog that is not closeable', function () {
129
+ options.mount({ closeable: false });
130
+ press('Escape');
131
+ screen.getByRole('dialog');
132
+ });
133
+ test('autoFocus moves focus to the first control inside the dialog', function () {
134
+ var first = options.mount().first;
135
+ expect(first).toHaveFocus();
136
+ });
137
+ test('autoFocus false leaves the first control unfocused', function () {
138
+ var first = options.mount({ autoFocus: false }).first;
139
+ expect(first).not.toHaveFocus();
140
+ });
141
+ test('Tab moves through the dialog controls', function () {
142
+ var _a = options.mount(), first = _a.first, last = _a.last;
143
+ expect(first).toHaveFocus();
144
+ tab();
145
+ expect(screen.getByRole('button', { name: 'Second' })).toHaveFocus();
146
+ tab();
147
+ expect(last).toHaveFocus();
148
+ });
149
+ test('focus stays locked inside the dialog', function () {
150
+ var _a = options.mount(), first = _a.first, last = _a.last;
151
+ expect(isFocusLocked({ first: first, last: last })).toBe(true);
152
+ });
153
+ test('Tab does not wrap inside the dialog when focus is not locked', function () {
154
+ var _a = options.mount({ focusLock: false }), first = _a.first, last = _a.last;
155
+ expect(isFocusLocked({ first: first, last: last })).toBe(false);
156
+ });
157
+ test('Escape returns focus to the trigger', function () { return __awaiter(_this, void 0, void 0, function () {
158
+ var trigger;
159
+ return __generator(this, function (_a) {
160
+ switch (_a.label) {
161
+ case 0:
162
+ trigger = options.openFromTrigger();
163
+ tab();
164
+ expect(trigger).toHaveFocus();
165
+ press('Enter');
166
+ expect(screen.getByRole('dialog')).toBeTruthy();
167
+ press('Escape');
168
+ return [4 /*yield*/, waitFor(function () {
169
+ expect(screen.queryByRole('dialog')).toBeNull();
170
+ expect(trigger).toHaveFocus();
171
+ })];
172
+ case 1:
173
+ _a.sent();
174
+ return [2 /*return*/];
175
+ }
176
+ });
177
+ }); });
178
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Keys the keyboard harness can press. Add a key here when a new pattern needs it.
3
+ * Tab uses `userEvent.tab()` — `{Tab}` is not a substitute.
4
+ */
5
+ export type KeyboardKey = 'Enter' | 'Space' | 'Escape' | 'ArrowUp' | 'ArrowDown' | 'ArrowLeft' | 'ArrowRight' | 'Home' | 'End';
6
+ export declare function press(key: KeyboardKey, modifiers?: {
7
+ shift?: boolean;
8
+ }): void;
9
+ /** Types into the focused element. Does not click or move focus. */
10
+ export declare function type(text: string): void;
11
+ export declare function tab(): void;
12
+ export declare function shiftTab(): void;
@@ -0,0 +1,22 @@
1
+ import userEvent from '@testing-library/user-event';
2
+ function keyToken(key) {
3
+ return key === 'Space' ? '{space}' : "{".concat(key, "}");
4
+ }
5
+ export function press(key, modifiers) {
6
+ var token = keyToken(key);
7
+ if (modifiers === null || modifiers === void 0 ? void 0 : modifiers.shift) {
8
+ userEvent.keyboard("{Shift>}".concat(token, "{/Shift}"));
9
+ return;
10
+ }
11
+ userEvent.keyboard(token);
12
+ }
13
+ /** Types into the focused element. Does not click or move focus. */
14
+ export function type(text) {
15
+ userEvent.keyboard(text);
16
+ }
17
+ export function tab() {
18
+ userEvent.tab();
19
+ }
20
+ export function shiftTab() {
21
+ userEvent.tab({ shift: true });
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sprinklrjs/spaceweb",
3
- "version": "14.16.1",
3
+ "version": "14.16.2",
4
4
  "description": "Components for SpaceWeb",
5
5
  "main": "index.js",
6
6
  "module": "./esm/index.js",
@@ -100,8 +100,18 @@
100
100
  "typescript": "~5.0.0"
101
101
  },
102
102
  "peerDependencies": {
103
- "@sprinklrjs/spaceweb-themes": "14.16.1",
103
+ "@sprinklrjs/spaceweb-themes": "14.16.2",
104
+ "@testing-library/react": "^14.0.0",
105
+ "@testing-library/user-event": "^13.2.1",
104
106
  "react": ">=17.0.2 <19.0.0",
105
107
  "react-dom": ">=17.0.2 <19.0.0"
108
+ },
109
+ "peerDependenciesMeta": {
110
+ "@testing-library/react": {
111
+ "optional": true
112
+ },
113
+ "@testing-library/user-event": {
114
+ "optional": true
115
+ }
106
116
  }
107
117
  }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Detects a focus trap between two controls.
3
+ *
4
+ * Moves focus. Tab from `last` must land on `first`, and Shift+Tab from `first`
5
+ * must land on `last`. Render the dialog or popover first, then pass the
6
+ * focusable ends inside it.
7
+ *
8
+ * ```ts
9
+ * import { isFocusLocked } from '@sprinklrjs/spaceweb/test-utils/keyboard';
10
+ *
11
+ * expect(isFocusLocked({ first, last })).toBe(true);
12
+ * ```
13
+ *
14
+ * Test-only. Requires `@testing-library/user-event` v13 and `@testing-library/react`.
15
+ */
16
+ export declare function isFocusLocked(ends: {
17
+ first: HTMLElement;
18
+ last: HTMLElement;
19
+ }): boolean;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isFocusLocked = void 0;
4
+ var react_1 = require("@testing-library/react");
5
+ var press_1 = require("./press");
6
+ /**
7
+ * Detects a focus trap between two controls.
8
+ *
9
+ * Moves focus. Tab from `last` must land on `first`, and Shift+Tab from `first`
10
+ * must land on `last`. Render the dialog or popover first, then pass the
11
+ * focusable ends inside it.
12
+ *
13
+ * ```ts
14
+ * import { isFocusLocked } from '@sprinklrjs/spaceweb/test-utils/keyboard';
15
+ *
16
+ * expect(isFocusLocked({ first, last })).toBe(true);
17
+ * ```
18
+ *
19
+ * Test-only. Requires `@testing-library/user-event` v13 and `@testing-library/react`.
20
+ */
21
+ function isFocusLocked(ends) {
22
+ var first = ends.first, last = ends.last;
23
+ (0, react_1.act)(function () {
24
+ last.focus();
25
+ });
26
+ (0, press_1.tab)();
27
+ var wrapsForward = document.activeElement === first;
28
+ (0, react_1.act)(function () {
29
+ first.focus();
30
+ });
31
+ (0, press_1.shiftTab)();
32
+ var wrapsBackward = document.activeElement === last;
33
+ return wrapsForward && wrapsBackward;
34
+ }
35
+ exports.isFocusLocked = isFocusLocked;