@entur/dropdown 7.1.4 → 7.2.0-beta.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.
@@ -425,6 +425,9 @@ var DropdownList = function DropdownList(_ref) {
425
425
  "aria-label": ariaValuesSelectAll().label
426
426
  }, selectAllItem == null ? void 0 : selectAllItem.label));
427
427
  };
428
+ var isReactComponent = function isReactComponent(icon) {
429
+ return typeof icon === 'function' || typeof icon === 'object' && icon !== null && '$$typeof' in icon && typeof icon.$$typeof === 'symbol';
430
+ };
428
431
  var listItemContent = function listItemContent(item) {
429
432
  return React.createElement(React.Fragment, null, isMultiselect && React.createElement(Checkbox, {
430
433
  "aria-hidden": "true",
@@ -434,14 +437,15 @@ var DropdownList = function DropdownList(_ref) {
434
437
  readOnly: true
435
438
  }), React.createElement("span", {
436
439
  className: "eds-dropdown__list__item__text"
437
- }, item.label, React.createElement(VisuallyHidden, null, isItemSelected(item) ? ariaLabelSelectedItem : '')), item.icons && React.createElement("span", null, item.icons.map(function (Icon) {
438
- var _Icon$displayName;
440
+ }, item.label, React.createElement(VisuallyHidden, null, isItemSelected(item) ? ariaLabelSelectedItem : '')), Array.isArray(item.icons) ? item.icons.filter(isReactComponent).map(function (Icon, index) {
441
+ var _ref2, _Icon$displayName;
442
+ var key = ((_ref2 = (_Icon$displayName = Icon.displayName) != null ? _Icon$displayName : Icon.name) != null ? _ref2 : Icon.name) + "-" + index;
439
443
  return React.createElement(Icon, {
440
- key: (item == null ? void 0 : item.label) + (item == null ? void 0 : item.value) + ((_Icon$displayName = Icon == null ? void 0 : Icon.displayName) != null ? _Icon$displayName : Icon == null ? void 0 : Icon.name),
444
+ key: key,
441
445
  inline: true,
442
446
  className: "eds-dropdown__list__item__icon"
443
447
  });
444
- })));
448
+ }) : null);
445
449
  };
446
450
  return (
447
451
  // use popover from @entur/tooltip when that package upgrades to floating-ui
@@ -469,7 +473,11 @@ var DropdownList = function DropdownList(_ref) {
469
473
  }, noMatchesText);
470
474
  }
471
475
  return listItems.map(function (item, index) {
472
- var _item$icons$map, _item$icons;
476
+ var _item$itemKey, _item$label, _item$value, _item$icons;
477
+ var key = (_item$itemKey = item.itemKey) != null ? _item$itemKey : ((_item$label = item.label) != null ? _item$label : '') + "-" + ((_item$value = item.value) != null ? _item$value : '') + "-" + ((_item$icons = item.icons) != null ? _item$icons : []).map(function (icon) {
478
+ var _ref3, _icon$displayName;
479
+ return (_ref3 = (_icon$displayName = icon == null ? void 0 : icon.displayName) != null ? _icon$displayName : icon == null ? void 0 : icon.name) != null ? _ref3 : 'unknown';
480
+ }).join('-');
473
481
  var itemIsSelectAll = item.value === (selectAllItem == null ? void 0 : selectAllItem.value);
474
482
  if (itemIsSelectAll && listItems.length <= 2) return null;
475
483
  return React.createElement("li", _extends({
@@ -478,10 +486,7 @@ var DropdownList = function DropdownList(_ref) {
478
486
  'eds-dropdown__list__item--highlighted': highlightedIndex === index,
479
487
  'eds-dropdown__list__item--selected': !isMultiselect && isItemSelected(item)
480
488
  }),
481
- key: (item == null ? void 0 : item.label) + (item == null ? void 0 : item.value) + ((_item$icons$map = item == null || (_item$icons = item.icons) == null ? void 0 : _item$icons.map(function (icon) {
482
- var _icon$displayName;
483
- return (_icon$displayName = icon.displayName) != null ? _icon$displayName : icon.name;
484
- })) != null ? _item$icons$map : '').toString()
489
+ key: key
485
490
  }, getItemProps({
486
491
  // @ts-expect-error Since getItemProps expects the same item type
487
492
  // here as items, it throws error when selectAllItem is a string.
@@ -592,10 +597,19 @@ var ClearableButton = function ClearableButton(_ref4) {
592
597
  className: "eds-dropdown__appendix__clear-button",
593
598
  type: "button",
594
599
  tabIndex: focusable ? 0 : -1,
600
+ // These events bubble up to the Dropdown container and trigger openMenu.
601
+ // To avoid this, stopPropagation and preventDefault are added.
595
602
  onClick: function onClick(e) {
596
603
  e.stopPropagation();
597
604
  onClear();
598
605
  },
606
+ onKeyDown: function onKeyDown(e) {
607
+ if (e.key === 'Enter' || e.key === ' ') {
608
+ e.preventDefault();
609
+ e.stopPropagation();
610
+ onClear();
611
+ }
612
+ },
599
613
  "aria-label": labelClearSelectedItems
600
614
  }, React.createElement(CloseSmallIcon, {
601
615
  "aria-hidden": "true"
@@ -1618,9 +1632,22 @@ var Dropdown = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
1618
1632
  items: normalizedItems,
1619
1633
  defaultHighlightedIndex: selectedItem ? undefined : 0,
1620
1634
  selectedItem: selectedItem,
1621
- onStateChange: function onStateChange(_ref2) {
1622
- var type = _ref2.type,
1623
- newSelectedItem = _ref2.selectedItem;
1635
+ stateReducer: function stateReducer(_, _ref2) {
1636
+ var changes = _ref2.changes,
1637
+ type = _ref2.type;
1638
+ var toggleButtonIsFocused = typeof document !== 'undefined' && document.activeElement === refs.reference.current;
1639
+ switch (type) {
1640
+ case useSelect.stateChangeTypes.ToggleButtonKeyDownArrowDown:
1641
+ case useSelect.stateChangeTypes.ToggleButtonKeyDownArrowUp:
1642
+ if (!toggleButtonIsFocused) return _extends({}, changes, {
1643
+ isOpen: false
1644
+ });
1645
+ }
1646
+ return changes;
1647
+ },
1648
+ onStateChange: function onStateChange(_ref3) {
1649
+ var type = _ref3.type,
1650
+ newSelectedItem = _ref3.selectedItem;
1624
1651
  switch (type) {
1625
1652
  case useSelect.stateChangeTypes.ToggleButtonBlur:
1626
1653
  if (!selectOnBlur) return;
@@ -1636,7 +1663,8 @@ var Dropdown = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
1636
1663
  getLabelProps = _useSelect.getLabelProps,
1637
1664
  getMenuProps = _useSelect.getMenuProps,
1638
1665
  getToggleButtonProps = _useSelect.getToggleButtonProps,
1639
- highlightedIndex = _useSelect.highlightedIndex;
1666
+ highlightedIndex = _useSelect.highlightedIndex,
1667
+ reset = _useSelect.reset;
1640
1668
  // calculations for floating-UI popover position
1641
1669
  var _useFloating = useFloating({
1642
1670
  open: isOpen,
@@ -1644,10 +1672,10 @@ var Dropdown = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
1644
1672
  middleware: [offset(space.extraSmall2), shift({
1645
1673
  padding: space.extraSmall
1646
1674
  }), size({
1647
- apply: function apply(_ref3) {
1648
- var rects = _ref3.rects,
1649
- elements = _ref3.elements,
1650
- availableHeight = _ref3.availableHeight;
1675
+ apply: function apply(_ref4) {
1676
+ var rects = _ref4.rects,
1677
+ elements = _ref4.elements,
1678
+ availableHeight = _ref4.availableHeight;
1651
1679
  Object.assign(elements.floating.style, {
1652
1680
  width: rects.reference.width + "px",
1653
1681
  // Floating will flip when smaller than 10*16 px
@@ -1671,6 +1699,11 @@ var Dropdown = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
1671
1699
  return autoUpdate(refs.reference.current, refs.floating.current, update);
1672
1700
  }
1673
1701
  }, [isOpen, refs.reference, refs.floating, update]);
1702
+ var handleOnClear = function handleOnClear() {
1703
+ var _refs$reference$curre;
1704
+ reset();
1705
+ (_refs$reference$curre = refs.reference.current) == null || _refs$reference$curre.focus();
1706
+ };
1674
1707
  return React.createElement(BaseFormControl, _extends({
1675
1708
  className: classNames('eds-dropdown', className, {
1676
1709
  'eds-dropdown--has-tooltip': labelTooltip !== undefined
@@ -1734,9 +1767,7 @@ var Dropdown = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
1734
1767
  "aria-busy": !(loading != null ? loading : resolvedItemsLoading) ? undefined : 'true',
1735
1768
  "aria-expanded": isOpen,
1736
1769
  clearable: clearable,
1737
- onClear: function onClear() {
1738
- return onChange == null ? void 0 : onChange(null);
1739
- },
1770
+ onClear: handleOnClear,
1740
1771
  disabled: disabled || readOnly,
1741
1772
  focusable: false,
1742
1773
  labelClearSelected: labelClearSelectedItem,