@testing-library/user-event 14.2.6 → 14.3.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/dist/index.cjs CHANGED
@@ -646,6 +646,18 @@ function eventWrapper(cb) {
646
646
  return result;
647
647
  }
648
648
 
649
+ // src/utils/misc/findClosest.ts
650
+ function findClosest(element, callback) {
651
+ let el = element;
652
+ do {
653
+ if (callback(el)) {
654
+ return el;
655
+ }
656
+ el = el.parentElement;
657
+ } while (el && el !== element.ownerDocument.body);
658
+ return void 0;
659
+ }
660
+
649
661
  // src/utils/misc/isDisabled.ts
650
662
  function isDisabled(element) {
651
663
  var _a;
@@ -704,16 +716,6 @@ function isFocusable(element) {
704
716
  return element.matches(FOCUSABLE_SELECTOR);
705
717
  }
706
718
 
707
- // src/utils/focus/blur.ts
708
- function blur(element) {
709
- if (!isFocusable(element))
710
- return;
711
- const wasActive = getActiveElement(element.ownerDocument) === element;
712
- if (!wasActive)
713
- return;
714
- eventWrapper(() => element.blur());
715
- }
716
-
717
719
  // src/utils/focus/cursor.ts
718
720
  function getNextCursorPosition(node, offset, direction, inputType) {
719
721
  if (isTextNode(node) && offset + direction >= 0 && offset + direction <= node.nodeValue.length) {
@@ -929,6 +931,49 @@ function moveSelection(node, direction) {
929
931
  }
930
932
  }
931
933
 
934
+ // src/utils/focus/focus.ts
935
+ function focus(element) {
936
+ const target = findClosest(element, isFocusable);
937
+ const activeElement = getActiveElement(element.ownerDocument);
938
+ if ((target != null ? target : element.ownerDocument.body) === activeElement) {
939
+ return;
940
+ } else if (target) {
941
+ eventWrapper(() => target.focus());
942
+ } else {
943
+ eventWrapper(() => activeElement == null ? void 0 : activeElement.blur());
944
+ }
945
+ updateSelectionOnFocus(target != null ? target : element.ownerDocument.body);
946
+ }
947
+
948
+ // src/utils/edit/walkRadio.ts
949
+ function walkRadio(config, el, direction) {
950
+ const window = getWindow(el);
951
+ const group = Array.from(el.ownerDocument.querySelectorAll(el.name ? `input[type="radio"][name="${window.CSS.escape(el.name)}"]` : `input[type="radio"][name=""], input[type="radio"]:not([name])`));
952
+ for (let i = group.findIndex((e) => e === el) + direction; ; i += direction) {
953
+ if (!group[i]) {
954
+ i = direction > 0 ? 0 : group.length - 1;
955
+ }
956
+ if (group[i] === el) {
957
+ return;
958
+ }
959
+ if (isDisabled(group[i])) {
960
+ continue;
961
+ }
962
+ focus(group[i]);
963
+ dispatchUIEvent(config, group[i], "click");
964
+ }
965
+ }
966
+
967
+ // src/utils/focus/blur.ts
968
+ function blur(element) {
969
+ if (!isFocusable(element))
970
+ return;
971
+ const wasActive = getActiveElement(element.ownerDocument) === element;
972
+ if (!wasActive)
973
+ return;
974
+ eventWrapper(() => element.blur());
975
+ }
976
+
932
977
  // src/utils/focus/copySelection.ts
933
978
  function copySelection(target) {
934
979
  const data = hasOwnSelection(target) ? { "text/plain": readSelectedValueFromInput(target) } : { "text/plain": String(target.ownerDocument.getSelection()) };
@@ -946,32 +991,6 @@ function readSelectedValueFromInput(target) {
946
991
  return val.substring(sel.startOffset, sel.endOffset);
947
992
  }
948
993
 
949
- // src/utils/misc/findClosest.ts
950
- function findClosest(element, callback) {
951
- let el = element;
952
- do {
953
- if (callback(el)) {
954
- return el;
955
- }
956
- el = el.parentElement;
957
- } while (el && el !== element.ownerDocument.body);
958
- return void 0;
959
- }
960
-
961
- // src/utils/focus/focus.ts
962
- function focus(element) {
963
- const target = findClosest(element, isFocusable);
964
- const activeElement = getActiveElement(element.ownerDocument);
965
- if ((target != null ? target : element.ownerDocument.body) === activeElement) {
966
- return;
967
- } else if (target) {
968
- eventWrapper(() => target.focus());
969
- } else {
970
- eventWrapper(() => activeElement == null ? void 0 : activeElement.blur());
971
- }
972
- updateSelectionOnFocus(target != null ? target : element.ownerDocument.body);
973
- }
974
-
975
994
  // src/utils/misc/isVisible.ts
976
995
  function isVisible(element) {
977
996
  const window = getWindow(element);
@@ -1549,8 +1568,28 @@ behavior.keydown = (event, target, config) => {
1549
1568
  return (_b = (_a = keydownBehavior[event.key]) == null ? void 0 : _a.call(keydownBehavior, event, target, config)) != null ? _b : combinationBehavior(event, target, config);
1550
1569
  };
1551
1570
  var keydownBehavior = {
1552
- ArrowLeft: (event, target) => () => moveSelection(target, -1),
1553
- ArrowRight: (event, target) => () => moveSelection(target, 1),
1571
+ ArrowDown: (event, target, config) => {
1572
+ if (isElementType(target, "input", { type: "radio" })) {
1573
+ return () => walkRadio(config, target, -1);
1574
+ }
1575
+ },
1576
+ ArrowLeft: (event, target, config) => {
1577
+ if (isElementType(target, "input", { type: "radio" })) {
1578
+ return () => walkRadio(config, target, -1);
1579
+ }
1580
+ return () => moveSelection(target, -1);
1581
+ },
1582
+ ArrowRight: (event, target, config) => {
1583
+ if (isElementType(target, "input", { type: "radio" })) {
1584
+ return () => walkRadio(config, target, 1);
1585
+ }
1586
+ return () => moveSelection(target, 1);
1587
+ },
1588
+ ArrowUp: (event, target, config) => {
1589
+ if (isElementType(target, "input", { type: "radio" })) {
1590
+ return () => walkRadio(config, target, 1);
1591
+ }
1592
+ },
1554
1593
  Backspace: (event, target, config) => {
1555
1594
  if (isEditable(target)) {
1556
1595
  return () => {
package/dist/index.mjs CHANGED
@@ -662,6 +662,18 @@ function eventWrapper(cb) {
662
662
  return result;
663
663
  }
664
664
 
665
+ // src/utils/misc/findClosest.ts
666
+ function findClosest(element, callback) {
667
+ let el = element;
668
+ do {
669
+ if (callback(el)) {
670
+ return el;
671
+ }
672
+ el = el.parentElement;
673
+ } while (el && el !== element.ownerDocument.body);
674
+ return void 0;
675
+ }
676
+
665
677
  // src/utils/misc/isDisabled.ts
666
678
  function isDisabled(element) {
667
679
  var _a;
@@ -720,16 +732,6 @@ function isFocusable(element) {
720
732
  return element.matches(FOCUSABLE_SELECTOR);
721
733
  }
722
734
 
723
- // src/utils/focus/blur.ts
724
- function blur(element) {
725
- if (!isFocusable(element))
726
- return;
727
- const wasActive = getActiveElement(element.ownerDocument) === element;
728
- if (!wasActive)
729
- return;
730
- eventWrapper(() => element.blur());
731
- }
732
-
733
735
  // src/utils/focus/cursor.ts
734
736
  function getNextCursorPosition(node, offset, direction, inputType) {
735
737
  if (isTextNode(node) && offset + direction >= 0 && offset + direction <= node.nodeValue.length) {
@@ -945,6 +947,49 @@ function moveSelection(node, direction) {
945
947
  }
946
948
  }
947
949
 
950
+ // src/utils/focus/focus.ts
951
+ function focus(element) {
952
+ const target = findClosest(element, isFocusable);
953
+ const activeElement = getActiveElement(element.ownerDocument);
954
+ if ((target != null ? target : element.ownerDocument.body) === activeElement) {
955
+ return;
956
+ } else if (target) {
957
+ eventWrapper(() => target.focus());
958
+ } else {
959
+ eventWrapper(() => activeElement == null ? void 0 : activeElement.blur());
960
+ }
961
+ updateSelectionOnFocus(target != null ? target : element.ownerDocument.body);
962
+ }
963
+
964
+ // src/utils/edit/walkRadio.ts
965
+ function walkRadio(config, el, direction) {
966
+ const window = getWindow(el);
967
+ const group = Array.from(el.ownerDocument.querySelectorAll(el.name ? `input[type="radio"][name="${window.CSS.escape(el.name)}"]` : `input[type="radio"][name=""], input[type="radio"]:not([name])`));
968
+ for (let i = group.findIndex((e) => e === el) + direction; ; i += direction) {
969
+ if (!group[i]) {
970
+ i = direction > 0 ? 0 : group.length - 1;
971
+ }
972
+ if (group[i] === el) {
973
+ return;
974
+ }
975
+ if (isDisabled(group[i])) {
976
+ continue;
977
+ }
978
+ focus(group[i]);
979
+ dispatchUIEvent(config, group[i], "click");
980
+ }
981
+ }
982
+
983
+ // src/utils/focus/blur.ts
984
+ function blur(element) {
985
+ if (!isFocusable(element))
986
+ return;
987
+ const wasActive = getActiveElement(element.ownerDocument) === element;
988
+ if (!wasActive)
989
+ return;
990
+ eventWrapper(() => element.blur());
991
+ }
992
+
948
993
  // src/utils/focus/copySelection.ts
949
994
  function copySelection(target) {
950
995
  const data = hasOwnSelection(target) ? { "text/plain": readSelectedValueFromInput(target) } : { "text/plain": String(target.ownerDocument.getSelection()) };
@@ -962,32 +1007,6 @@ function readSelectedValueFromInput(target) {
962
1007
  return val.substring(sel.startOffset, sel.endOffset);
963
1008
  }
964
1009
 
965
- // src/utils/misc/findClosest.ts
966
- function findClosest(element, callback) {
967
- let el = element;
968
- do {
969
- if (callback(el)) {
970
- return el;
971
- }
972
- el = el.parentElement;
973
- } while (el && el !== element.ownerDocument.body);
974
- return void 0;
975
- }
976
-
977
- // src/utils/focus/focus.ts
978
- function focus(element) {
979
- const target = findClosest(element, isFocusable);
980
- const activeElement = getActiveElement(element.ownerDocument);
981
- if ((target != null ? target : element.ownerDocument.body) === activeElement) {
982
- return;
983
- } else if (target) {
984
- eventWrapper(() => target.focus());
985
- } else {
986
- eventWrapper(() => activeElement == null ? void 0 : activeElement.blur());
987
- }
988
- updateSelectionOnFocus(target != null ? target : element.ownerDocument.body);
989
- }
990
-
991
1010
  // src/utils/misc/isVisible.ts
992
1011
  function isVisible(element) {
993
1012
  const window = getWindow(element);
@@ -1562,8 +1581,28 @@ behavior.keydown = (event, target, config) => {
1562
1581
  return (_b = (_a = keydownBehavior[event.key]) == null ? void 0 : _a.call(keydownBehavior, event, target, config)) != null ? _b : combinationBehavior(event, target, config);
1563
1582
  };
1564
1583
  var keydownBehavior = {
1565
- ArrowLeft: (event, target) => () => moveSelection(target, -1),
1566
- ArrowRight: (event, target) => () => moveSelection(target, 1),
1584
+ ArrowDown: (event, target, config) => {
1585
+ if (isElementType(target, "input", { type: "radio" })) {
1586
+ return () => walkRadio(config, target, -1);
1587
+ }
1588
+ },
1589
+ ArrowLeft: (event, target, config) => {
1590
+ if (isElementType(target, "input", { type: "radio" })) {
1591
+ return () => walkRadio(config, target, -1);
1592
+ }
1593
+ return () => moveSelection(target, -1);
1594
+ },
1595
+ ArrowRight: (event, target, config) => {
1596
+ if (isElementType(target, "input", { type: "radio" })) {
1597
+ return () => walkRadio(config, target, 1);
1598
+ }
1599
+ return () => moveSelection(target, 1);
1600
+ },
1601
+ ArrowUp: (event, target, config) => {
1602
+ if (isElementType(target, "input", { type: "radio" })) {
1603
+ return () => walkRadio(config, target, 1);
1604
+ }
1605
+ },
1567
1606
  Backspace: (event, target, config) => {
1568
1607
  if (isEditable(target)) {
1569
1608
  return () => {
@@ -0,0 +1,4 @@
1
+ import { Config } from '../../setup';
2
+ export declare function walkRadio(config: Config, el: HTMLInputElement & {
3
+ type: 'radio';
4
+ }, direction: -1 | 1): void;
@@ -8,6 +8,7 @@ export * from './edit/input';
8
8
  export * from './edit/isContentEditable';
9
9
  export * from './edit/isEditable';
10
10
  export * from './edit/setFiles';
11
+ export * from './edit/walkRadio';
11
12
  export * from './focus/blur';
12
13
  export * from './focus/copySelection';
13
14
  export * from './focus/cursor';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "14.2.6",
3
+ "version": "14.3.0",
4
4
  "description": "Fire events the same way the user does",
5
5
  "keywords": [
6
6
  "react-testing-library",