@entur/dropdown 5.4.11 → 5.5.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.
@@ -1,5 +1,5 @@
1
1
  import { mergeRefs, useDebounce, useRandomId, warnAboutMissingStyles } from '@entur/utils';
2
- import React, { useState, useRef, useEffect } from 'react';
2
+ import React, { useState, useRef, useEffect, useCallback } from 'react';
3
3
  import Downshift, { useSelect, useCombobox, useMultipleSelection } from 'downshift';
4
4
  import classNames from 'classnames';
5
5
  import { BaseFormControl, VariantProvider, FeedbackText, Checkbox } from '@entur/form';
@@ -7,6 +7,7 @@ import { CheckIcon, DownArrowIcon, CloseSmallIcon, CloseIcon } from '@entur/icon
7
7
  import { VisuallyHidden } from '@entur/a11y';
8
8
  import { LoadingDots } from '@entur/loader';
9
9
  import { space } from '@entur/tokens';
10
+ import { useFloating, autoUpdate, offset, flip } from '@floating-ui/react-dom';
10
11
  import { IconButton } from '@entur/button';
11
12
  import { TagChip } from '@entur/chip';
12
13
  import { Tooltip } from '@entur/tooltip';
@@ -1234,7 +1235,7 @@ function SelectedItemsLabel(items) {
1234
1235
  }).toString() : items.length + " elementer valgt";
1235
1236
  }
1236
1237
 
1237
- var _excluded$5 = ["ariaLabelChosenSingular", "ariaLabelSelectedItem", "getItemProps", "getMenuProps", "inputValue", "isOpen", "highlightedIndex", "listItems", "listStyle", "loading", "loadingText", "noMatchesText", "selectAllCheckboxState", "selectAllItem", "selectedItems", "showSelectAllInList"];
1238
+ var _excluded$5 = ["ariaLabelChosenSingular", "ariaLabelSelectedItem", "getItemProps", "getMenuProps", "inputValue", "isOpen", "highlightedIndex", "listItems", "listStyle", "listRef", "loading", "loadingText", "noMatchesText", "selectAllCheckboxState", "selectAllItem", "selectedItems", "showSelectAllInList"];
1238
1239
  var DropdownList = function DropdownList(_ref) {
1239
1240
  var _listItems$;
1240
1241
  var _ref$ariaLabelChosenS = _ref.ariaLabelChosenSingular,
@@ -1247,6 +1248,7 @@ var DropdownList = function DropdownList(_ref) {
1247
1248
  highlightedIndex = _ref.highlightedIndex,
1248
1249
  listItems = _ref.listItems,
1249
1250
  listStyle = _ref.listStyle,
1251
+ listRef = _ref.listRef,
1250
1252
  _ref$loading = _ref.loading,
1251
1253
  loading = _ref$loading === void 0 ? false : _ref$loading,
1252
1254
  _ref$loadingText = _ref.loadingText,
@@ -1329,7 +1331,8 @@ var DropdownList = function DropdownList(_ref) {
1329
1331
  return (
1330
1332
  // use popover from @entur/tooltip when that package upgrades to floating-ui
1331
1333
  React.createElement("ul", _extends({}, getMenuProps({
1332
- 'aria-multiselectable': isMultiselect
1334
+ 'aria-multiselectable': isMultiselect,
1335
+ ref: mergeRefs(getMenuProps().ref, listRef)
1333
1336
  }), {
1334
1337
  className: "eds-dropdown__list",
1335
1338
  style: _extends({
@@ -1683,17 +1686,17 @@ var SearchableDropdown = function SearchableDropdown(_ref) {
1683
1686
  var _useState = useState(value !== null),
1684
1687
  showSelectedItem = _useState[0],
1685
1688
  setShowSelectedItem = _useState[1];
1686
- var _React$useState = React.useState(0),
1687
- lastHighlightedIndex = _React$useState[0],
1688
- setLastHighlightedIndex = _React$useState[1];
1689
+ var _useState2 = useState(0),
1690
+ lastHighlightedIndex = _useState2[0],
1691
+ setLastHighlightedIndex = _useState2[1];
1689
1692
  var inputRef = useRef(null);
1690
1693
  var _useResolvedItems = useResolvedItems(initialItems, debounceTimeout),
1691
1694
  normalizedItems = _useResolvedItems.items,
1692
1695
  loading = _useResolvedItems.loading,
1693
1696
  fetchItems = _useResolvedItems.fetchItems;
1694
- var _React$useState2 = React.useState(normalizedItems),
1695
- listItems = _React$useState2[0],
1696
- setListItems = _React$useState2[1];
1697
+ var _useState3 = useState(normalizedItems),
1698
+ listItems = _useState3[0],
1699
+ setListItems = _useState3[1];
1697
1700
  var filterListItems = function filterListItems(_ref2) {
1698
1701
  var inputValue = _ref2.inputValue;
1699
1702
  return setListItems(normalizedItems.filter(function (item) {
@@ -1708,13 +1711,23 @@ var SearchableDropdown = function SearchableDropdown(_ref) {
1708
1711
  inputValue: inputValue != null ? inputValue : EMPTY_INPUT
1709
1712
  });
1710
1713
  };
1711
- React.useEffect(function () {
1714
+ var inputHasFocus = typeof document !== 'undefined' ? (inputRef == null ? void 0 : inputRef.current) === ((_document = document) == null ? void 0 : _document.activeElement) : false;
1715
+ useEffect(function () {
1712
1716
  filterListItems({
1713
1717
  inputValue: inputValue
1714
1718
  });
1715
1719
  }, [normalizedItems]); // eslint-disable-line react-hooks/exhaustive-deps
1716
- var inputHasFocus = typeof document !== 'undefined' ? (inputRef == null ? void 0 : inputRef.current) === ((_document = document) == null ? void 0 : _document.activeElement) : false;
1717
- var stateReducer = React.useCallback(function (_, _ref4) {
1720
+ useEffect(function () {
1721
+ // sync internal state on initial render
1722
+ if (selectedItem !== null && !inputHasFocus) {
1723
+ setShowSelectedItem(true);
1724
+ updateListItems({
1725
+ inputValue: EMPTY_INPUT
1726
+ });
1727
+ setInputValue(EMPTY_INPUT);
1728
+ }
1729
+ }, []);
1730
+ var stateReducer = useCallback(function (_, _ref4) {
1718
1731
  var type = _ref4.type,
1719
1732
  changes = _ref4.changes;
1720
1733
  if (changes.highlightedIndex !== undefined && (changes == null ? void 0 : changes.highlightedIndex) >= 0) {
@@ -1807,6 +1820,16 @@ var SearchableDropdown = function SearchableDropdown(_ref) {
1807
1820
  selectedItem = _useCombobox.selectedItem,
1808
1821
  inputValue = _useCombobox.inputValue,
1809
1822
  setInputValue = _useCombobox.setInputValue;
1823
+ var _useFloating = useFloating({
1824
+ whileElementsMounted: function whileElementsMounted(ref, _float, update) {
1825
+ return autoUpdate(ref, _float, update);
1826
+ },
1827
+ placement: 'bottom-start',
1828
+ open: isOpen,
1829
+ middleware: [offset(space.extraSmall2), flip()]
1830
+ }),
1831
+ refs = _useFloating.refs,
1832
+ floatingStyles = _useFloating.floatingStyles;
1810
1833
  var handleOnClear = function handleOnClear() {
1811
1834
  var _inputRef$current;
1812
1835
  onChange(null);
@@ -1838,6 +1861,7 @@ var SearchableDropdown = function SearchableDropdown(_ref) {
1838
1861
  },
1839
1862
  prepend: prepend,
1840
1863
  readOnly: readOnly,
1864
+ ref: refs.setReference,
1841
1865
  variant: variant
1842
1866
  }, rest, {
1843
1867
  append: React.createElement(FieldAppend$1, {
@@ -1886,7 +1910,8 @@ var SearchableDropdown = function SearchableDropdown(_ref) {
1886
1910
  ariaLabelSelectedItem: ariaLabelSelectedItem,
1887
1911
  isOpen: isOpen,
1888
1912
  listItems: listItems,
1889
- listStyle: listStyle,
1913
+ listStyle: _extends({}, floatingStyles, listStyle),
1914
+ listRef: refs.setFloating,
1890
1915
  loading: loading,
1891
1916
  loadingText: loadingText,
1892
1917
  noMatchesText: noMatchesText,
@@ -2091,6 +2116,11 @@ var MultiSelect = function MultiSelect(_ref) {
2091
2116
  updateListItems({
2092
2117
  inputValue: changes.inputValue
2093
2118
  });
2119
+ // set highlighted item to first item after search
2120
+ setLastHighlightedIndex(hideSelectAll ? 0 : 1);
2121
+ return _extends({}, changes, {
2122
+ highlightedIndex: hideSelectAll ? 0 : 1
2123
+ });
2094
2124
  }
2095
2125
  return changes;
2096
2126
  }
@@ -2158,6 +2188,16 @@ var MultiSelect = function MultiSelect(_ref) {
2158
2188
  isOpen = _useCombobox.isOpen,
2159
2189
  openMenu = _useCombobox.openMenu,
2160
2190
  setInputValue = _useCombobox.setInputValue;
2191
+ var _useFloating = useFloating({
2192
+ whileElementsMounted: function whileElementsMounted(ref, _float, update) {
2193
+ return autoUpdate(ref, _float, update);
2194
+ },
2195
+ placement: 'bottom-start',
2196
+ open: isOpen,
2197
+ middleware: [offset(space.extraSmall2), flip()]
2198
+ }),
2199
+ refs = _useFloating.refs,
2200
+ floatingStyles = _useFloating.floatingStyles;
2161
2201
  var handleOnClear = function handleOnClear() {
2162
2202
  var _inputRef$current3;
2163
2203
  onChange([]);
@@ -2200,6 +2240,7 @@ var MultiSelect = function MultiSelect(_ref) {
2200
2240
  if (e.target === e.currentTarget) (_inputRef$current4 = inputRef.current) == null ? void 0 : _inputRef$current4.focus();
2201
2241
  },
2202
2242
  readOnly: readOnly,
2243
+ ref: refs.setReference,
2203
2244
  variant: variant
2204
2245
  }, rest), React.createElement("div", {
2205
2246
  className: classNames('eds-dropdown--multiselect__selected-items-and-input', {
@@ -2271,7 +2312,8 @@ var MultiSelect = function MultiSelect(_ref) {
2271
2312
  inputValue: inputValue,
2272
2313
  isOpen: isOpen,
2273
2314
  listItems: listItems,
2274
- listStyle: listStyle,
2315
+ listStyle: _extends({}, floatingStyles, listStyle),
2316
+ listRef: refs.setFloating,
2275
2317
  loading: loading,
2276
2318
  loadingText: loadingText,
2277
2319
  noMatchesText: noMatchesText,
@@ -2349,6 +2391,16 @@ var Dropdown = function Dropdown(_ref) {
2349
2391
  getMenuProps = _useSelect.getMenuProps,
2350
2392
  getToggleButtonProps = _useSelect.getToggleButtonProps,
2351
2393
  highlightedIndex = _useSelect.highlightedIndex;
2394
+ var _useFloating = useFloating({
2395
+ whileElementsMounted: function whileElementsMounted(ref, _float, update) {
2396
+ return autoUpdate(ref, _float, update);
2397
+ },
2398
+ placement: 'bottom-start',
2399
+ open: isOpen,
2400
+ middleware: [offset(space.extraSmall2), flip()]
2401
+ }),
2402
+ refs = _useFloating.refs,
2403
+ floatingStyles = _useFloating.floatingStyles;
2352
2404
  return React.createElement("div", {
2353
2405
  className: classNames('eds-dropdown__wrapper', className, {
2354
2406
  'eds-dropdown__wrapper--has-tooltip': labelTooltip !== undefined
@@ -2387,6 +2439,7 @@ var Dropdown = function Dropdown(_ref) {
2387
2439
  labelTooltip: labelTooltip,
2388
2440
  prepend: prepend,
2389
2441
  readOnly: readOnly,
2442
+ ref: refs.setReference,
2390
2443
  variant: variant
2391
2444
  }, rest), React.createElement("div", _extends({
2392
2445
  className: "eds-dropdown__selected-item"
@@ -2414,7 +2467,8 @@ var Dropdown = function Dropdown(_ref) {
2414
2467
  highlightedIndex: highlightedIndex,
2415
2468
  isOpen: isOpen,
2416
2469
  listItems: normalizedItems,
2417
- listStyle: listStyle,
2470
+ listStyle: _extends({}, floatingStyles, listStyle),
2471
+ listRef: refs.setFloating,
2418
2472
  loading: loading,
2419
2473
  loadingText: loadingText,
2420
2474
  selectedItems: selectedItem !== null ? [selectedItem] : []