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