@entur/dropdown 5.4.11 → 6.0.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.
@@ -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);
@@ -1817,13 +1840,24 @@ var SearchableDropdown = function SearchableDropdown(_ref) {
1817
1840
  });
1818
1841
  setShowSelectedItem(false);
1819
1842
  };
1820
- return React.createElement("div", {
1821
- className: classNames('eds-dropdown__wrapper', className, {
1822
- 'eds-dropdown__wrapper--has-tooltip': labelTooltip !== undefined
1843
+ return React.createElement(BaseFormControl, _extends({
1844
+ append: React.createElement(FieldAppend$1, {
1845
+ ariaLabelCloseList: ariaLabelCloseList,
1846
+ ariaLabelOpenList: ariaLabelOpenList,
1847
+ clearable: clearable,
1848
+ labelClearSelectedItems: labelClearSelectedItem,
1849
+ disabled: readOnly || disabled,
1850
+ focusable: false,
1851
+ getToggleButtonProps: getToggleButtonProps,
1852
+ isOpen: isOpen,
1853
+ loading: loading,
1854
+ loadingText: loadingText,
1855
+ onClear: handleOnClear,
1856
+ selectedItems: [selectedItem]
1857
+ }),
1858
+ className: classNames('eds-dropdown', 'eds-dropdown--searchable', className, {
1859
+ 'eds-dropdown--has-tooltip': labelTooltip !== undefined
1823
1860
  }),
1824
- style: style
1825
- }, React.createElement(BaseFormControl, _extends({
1826
- className: classNames('eds-dropdown', 'eds-dropdown--searchable'),
1827
1861
  disabled: disabled,
1828
1862
  disableLabelAnimation: disableLabelAnimation,
1829
1863
  feedback: feedback,
@@ -1838,23 +1872,10 @@ var SearchableDropdown = function SearchableDropdown(_ref) {
1838
1872
  },
1839
1873
  prepend: prepend,
1840
1874
  readOnly: readOnly,
1875
+ ref: refs.setReference,
1876
+ style: style,
1841
1877
  variant: variant
1842
- }, rest, {
1843
- append: React.createElement(FieldAppend$1, {
1844
- ariaLabelCloseList: ariaLabelCloseList,
1845
- ariaLabelOpenList: ariaLabelOpenList,
1846
- clearable: clearable,
1847
- labelClearSelectedItems: labelClearSelectedItem,
1848
- disabled: readOnly || disabled,
1849
- focusable: false,
1850
- getToggleButtonProps: getToggleButtonProps,
1851
- isOpen: isOpen,
1852
- loading: loading,
1853
- loadingText: loadingText,
1854
- onClear: handleOnClear,
1855
- selectedItems: [selectedItem]
1856
- })
1857
- }), React.createElement("span", {
1878
+ }, rest), React.createElement("span", {
1858
1879
  className: classNames('eds-dropdown--searchable__selected-item', {
1859
1880
  'eds-dropdown--searchable__selected-item--hidden': !showSelectedItem
1860
1881
  }),
@@ -1881,12 +1902,13 @@ var SearchableDropdown = function SearchableDropdown(_ref) {
1881
1902
  if (selectOnTab && isOpen && e.key === 'Tab') onChange == null ? void 0 : onChange(listItems[highlightedIndex]);
1882
1903
  },
1883
1904
  ref: inputRef
1884
- })))), React.createElement(DropdownList, {
1905
+ }))), React.createElement(DropdownList, {
1885
1906
  ariaLabelChosenSingular: ariaLabelChosenSingular,
1886
1907
  ariaLabelSelectedItem: ariaLabelSelectedItem,
1887
1908
  isOpen: isOpen,
1888
1909
  listItems: listItems,
1889
- listStyle: listStyle,
1910
+ listStyle: _extends({}, floatingStyles, listStyle),
1911
+ listRef: refs.setFloating,
1890
1912
  loading: loading,
1891
1913
  loadingText: loadingText,
1892
1914
  noMatchesText: noMatchesText,
@@ -2091,6 +2113,11 @@ var MultiSelect = function MultiSelect(_ref) {
2091
2113
  updateListItems({
2092
2114
  inputValue: changes.inputValue
2093
2115
  });
2116
+ // set highlighted item to first item after search
2117
+ setLastHighlightedIndex(hideSelectAll ? 0 : 1);
2118
+ return _extends({}, changes, {
2119
+ highlightedIndex: hideSelectAll ? 0 : 1
2120
+ });
2094
2121
  }
2095
2122
  return changes;
2096
2123
  }
@@ -2158,6 +2185,16 @@ var MultiSelect = function MultiSelect(_ref) {
2158
2185
  isOpen = _useCombobox.isOpen,
2159
2186
  openMenu = _useCombobox.openMenu,
2160
2187
  setInputValue = _useCombobox.setInputValue;
2188
+ var _useFloating = useFloating({
2189
+ whileElementsMounted: function whileElementsMounted(ref, _float, update) {
2190
+ return autoUpdate(ref, _float, update);
2191
+ },
2192
+ placement: 'bottom-start',
2193
+ open: isOpen,
2194
+ middleware: [offset(space.extraSmall2), flip()]
2195
+ }),
2196
+ refs = _useFloating.refs,
2197
+ floatingStyles = _useFloating.floatingStyles;
2161
2198
  var handleOnClear = function handleOnClear() {
2162
2199
  var _inputRef$current3;
2163
2200
  onChange([]);
@@ -2167,12 +2204,7 @@ var MultiSelect = function MultiSelect(_ref) {
2167
2204
  inputValue: inputValue
2168
2205
  });
2169
2206
  };
2170
- return React.createElement("div", {
2171
- className: classNames('eds-dropdown__wrapper', className, {
2172
- 'eds-dropdown__wrapper--has-tooltip': labelTooltip !== undefined
2173
- }),
2174
- style: style
2175
- }, React.createElement(BaseFormControl, _extends({
2207
+ return React.createElement(BaseFormControl, _extends({
2176
2208
  append: React.createElement(FieldAppend$1, {
2177
2209
  ariaLabelCloseList: ariaLabelCloseList,
2178
2210
  ariaLabelOpenList: ariaLabelOpenList,
@@ -2187,7 +2219,9 @@ var MultiSelect = function MultiSelect(_ref) {
2187
2219
  onClear: handleOnClear,
2188
2220
  getToggleButtonProps: getToggleButtonProps
2189
2221
  }),
2190
- className: classNames('eds-dropdown', 'eds-dropdown--multiselect'),
2222
+ className: classNames('eds-dropdown', 'eds-dropdown--multiselect', className, {
2223
+ 'eds-dropdown--has-tooltip': labelTooltip !== undefined
2224
+ }),
2191
2225
  disabled: disabled,
2192
2226
  feedback: feedback,
2193
2227
  isFilled: hasSelectedItems || inputValue !== EMPTY_INPUT,
@@ -2200,6 +2234,8 @@ var MultiSelect = function MultiSelect(_ref) {
2200
2234
  if (e.target === e.currentTarget) (_inputRef$current4 = inputRef.current) == null ? void 0 : _inputRef$current4.focus();
2201
2235
  },
2202
2236
  readOnly: readOnly,
2237
+ ref: refs.setReference,
2238
+ style: style,
2203
2239
  variant: variant
2204
2240
  }, rest), React.createElement("div", {
2205
2241
  className: classNames('eds-dropdown--multiselect__selected-items-and-input', {
@@ -2262,7 +2298,7 @@ var MultiSelect = function MultiSelect(_ref) {
2262
2298
  preventKeyAction: isOpen,
2263
2299
  ref: inputRef,
2264
2300
  value: inputValue != null ? inputValue : EMPTY_INPUT
2265
- }))))))), React.createElement(DropdownList, {
2301
+ })))))), React.createElement(DropdownList, {
2266
2302
  ariaLabelChosenSingular: ariaLabelChosenSingular,
2267
2303
  ariaLabelSelectedItem: ariaLabelSelectedItem,
2268
2304
  getItemProps: getItemProps,
@@ -2271,7 +2307,8 @@ var MultiSelect = function MultiSelect(_ref) {
2271
2307
  inputValue: inputValue,
2272
2308
  isOpen: isOpen,
2273
2309
  listItems: listItems,
2274
- listStyle: listStyle,
2310
+ listStyle: _extends({}, floatingStyles, listStyle),
2311
+ listRef: refs.setFloating,
2275
2312
  loading: loading,
2276
2313
  loadingText: loadingText,
2277
2314
  noMatchesText: noMatchesText,
@@ -2349,12 +2386,17 @@ var Dropdown = function Dropdown(_ref) {
2349
2386
  getMenuProps = _useSelect.getMenuProps,
2350
2387
  getToggleButtonProps = _useSelect.getToggleButtonProps,
2351
2388
  highlightedIndex = _useSelect.highlightedIndex;
2352
- return React.createElement("div", {
2353
- className: classNames('eds-dropdown__wrapper', className, {
2354
- 'eds-dropdown__wrapper--has-tooltip': labelTooltip !== undefined
2389
+ var _useFloating = useFloating({
2390
+ whileElementsMounted: function whileElementsMounted(ref, _float, update) {
2391
+ return autoUpdate(ref, _float, update);
2392
+ },
2393
+ placement: 'bottom-start',
2394
+ open: isOpen,
2395
+ middleware: [offset(space.extraSmall2), flip()]
2355
2396
  }),
2356
- style: style
2357
- }, React.createElement(BaseFormControl, _extends({
2397
+ refs = _useFloating.refs,
2398
+ floatingStyles = _useFloating.floatingStyles;
2399
+ return React.createElement(BaseFormControl, _extends({
2358
2400
  append: React.createElement(FieldAppend$1, {
2359
2401
  ariaHiddenToggleButton: true,
2360
2402
  ariaLabelCloseList: ariaLabelCloseList,
@@ -2374,8 +2416,8 @@ var Dropdown = function Dropdown(_ref) {
2374
2416
  disabled: readOnly || disabled,
2375
2417
  selectedItems: [selectedItem]
2376
2418
  }),
2377
- className: classNames('eds-dropdown', {
2378
- 'eds-dropdown--not-filled': !isFilled
2419
+ className: classNames('eds-dropdown', className, {
2420
+ 'eds-dropdown--has-tooltip': labelTooltip !== undefined
2379
2421
  }),
2380
2422
  disabled: disabled,
2381
2423
  disableLabelAnimation: disableLabelAnimation,
@@ -2387,6 +2429,8 @@ var Dropdown = function Dropdown(_ref) {
2387
2429
  labelTooltip: labelTooltip,
2388
2430
  prepend: prepend,
2389
2431
  readOnly: readOnly,
2432
+ ref: refs.setReference,
2433
+ style: style,
2390
2434
  variant: variant
2391
2435
  }, rest), React.createElement("div", _extends({
2392
2436
  className: "eds-dropdown__selected-item"
@@ -2406,7 +2450,7 @@ var Dropdown = function Dropdown(_ref) {
2406
2450
  className: classNames('eds-dropdown__selected-item__placeholder', {
2407
2451
  'eds-dropdown__selected-item__placeholder--readonly': readOnly
2408
2452
  })
2409
- }, placeholder)) != null ? _ref3 : '')), React.createElement(DropdownList, {
2453
+ }, placeholder)) != null ? _ref3 : ''), React.createElement(DropdownList, {
2410
2454
  ariaLabelChosenSingular: ariaLabelChosenSingular,
2411
2455
  ariaLabelSelectedItem: ariaLabelSelectedItem,
2412
2456
  getItemProps: getItemProps,
@@ -2414,7 +2458,8 @@ var Dropdown = function Dropdown(_ref) {
2414
2458
  highlightedIndex: highlightedIndex,
2415
2459
  isOpen: isOpen,
2416
2460
  listItems: normalizedItems,
2417
- listStyle: listStyle,
2461
+ listStyle: _extends({}, floatingStyles, listStyle),
2462
+ listRef: refs.setFloating,
2418
2463
  loading: loading,
2419
2464
  loadingText: loadingText,
2420
2465
  selectedItems: selectedItem !== null ? [selectedItem] : []