@digitransit-component/digitransit-component-autosuggest 8.0.3 → 8.0.4

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/lib/index.js CHANGED
@@ -6810,6 +6810,9 @@ function _objectSpread$2(e) { for (var r = 1; r < arguments.length; r++) { var t
6810
6810
  * @property {boolean} [showScroll]
6811
6811
  * @property {AutosuggestState} state
6812
6812
  * @property {function} dispatch
6813
+ * @property {Object} pendingEnterRef ref tracking whether a selection was
6814
+ * requested (click/enter) while suggestions were still loading; shared
6815
+ * with the parent DTAutosuggest component
6813
6816
  *
6814
6817
  * @param {MobileViewProps} props
6815
6818
  * @returns {JSX.Element}
@@ -6832,6 +6835,7 @@ const MobileView = ({
6832
6835
  showScroll,
6833
6836
  state,
6834
6837
  dispatch,
6838
+ pendingEnterRef,
6835
6839
  colors
6836
6840
  }) => {
6837
6841
  const [t] = useTranslation();
@@ -6908,10 +6912,10 @@ const MobileView = ({
6908
6912
  }
6909
6913
  case useCombobox.stateChangeTypes.InputKeyDownEnter:
6910
6914
  if (state.loading) {
6911
- dispatch({
6912
- type: 'PENDING_ENTER',
6913
- enterPending: true
6914
- });
6915
+ // Refs are designed to be mutated directly; this signals the
6916
+ // parent's "pending enter" effect once suggestions have loaded.
6917
+ // eslint-disable-next-line no-param-reassign
6918
+ pendingEnterRef.current = true;
6915
6919
  return oldState;
6916
6920
  }
6917
6921
  // select the first item if none is highlighted
@@ -7080,7 +7084,10 @@ process.env.NODE_ENV !== "production" ? MobileView.propTypes = {
7080
7084
  renderMobile: PropTypes.bool.isRequired,
7081
7085
  loading: PropTypes.bool.isRequired
7082
7086
  }).isRequired,
7083
- dispatch: PropTypes.func.isRequired
7087
+ dispatch: PropTypes.func.isRequired,
7088
+ pendingEnterRef: PropTypes.shape({
7089
+ current: PropTypes.bool
7090
+ }).isRequired
7084
7091
  } : void 0;
7085
7092
 
7086
7093
  function ownKeys$1(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
@@ -7129,12 +7136,6 @@ const searchReducer = (state, action) => {
7129
7136
  {
7130
7137
  return action.initialState;
7131
7138
  }
7132
- case 'PENDING_ENTER':
7133
- {
7134
- return _objectSpread$1(_objectSpread$1({}, state), {}, {
7135
- enterPending: action.enterPending
7136
- });
7137
- }
7138
7139
  case 'TOGGLE_MENU':
7139
7140
  {
7140
7141
  if (action.isMobile) {
@@ -7161,6 +7162,23 @@ const searchReducer = (state, action) => {
7161
7162
 
7162
7163
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
7163
7164
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
7165
+
7166
+ // Hoisted so these defaults are referentially stable across renders. Using
7167
+ // inline object/array literals as parameter defaults (e.g. `sources = []`)
7168
+ // creates a brand-new reference on every render in which the caller omits
7169
+ // the prop, which in turn invalidates every hook (useEffect/useCallback/
7170
+ // useMemo) that depends on them - e.g. causing `fetchSuggestions` to be
7171
+ // recreated and refetch on every render, and the `RESET_SOURCES` effect to
7172
+ // fire in a loop.
7173
+ const DEFAULT_SOURCES = [];
7174
+ const DEFAULT_PATH_OPTS = {
7175
+ routesPrefix: 'linjat',
7176
+ stopsPrefix: 'pysakit'
7177
+ };
7178
+ const DEFAULT_REF_POINT = {};
7179
+ const DEFAULT_FONT_WEIGHTS = {
7180
+ medium: 500
7181
+ };
7164
7182
  const getAriaProps = ({
7165
7183
  id,
7166
7184
  ariaLabel,
@@ -7389,24 +7407,19 @@ function DTAutosuggest({
7389
7407
  translatedPlaceholder,
7390
7408
  required = false,
7391
7409
  ariaLabel,
7392
- fontWeights = {
7393
- medium: 500
7394
- },
7410
+ fontWeights = DEFAULT_FONT_WEIGHTS,
7395
7411
  colors = defaultColors,
7396
7412
  modeSet,
7397
7413
  showScroll = false,
7398
7414
  isEmbedded = false,
7399
7415
  transportMode,
7400
7416
  targets,
7401
- sources = [],
7417
+ sources = DEFAULT_SOURCES,
7402
7418
  geocodingSize,
7403
7419
  filterResults,
7404
7420
  searchContext,
7405
- pathOpts = {
7406
- routesPrefix: 'linjat',
7407
- stopsPrefix: 'pysakit'
7408
- },
7409
- refPoint = {}
7421
+ pathOpts = DEFAULT_PATH_OPTS,
7422
+ refPoint = DEFAULT_REF_POINT
7410
7423
  }) {
7411
7424
  const [t] = useTranslation();
7412
7425
  const initialState = {
@@ -7418,7 +7431,6 @@ function DTAutosuggest({
7418
7431
  isCleared: false,
7419
7432
  renderMobile: false,
7420
7433
  value,
7421
- enterPending: false,
7422
7434
  isMenuOpen: false
7423
7435
  };
7424
7436
  const [state, dispatch] = useReducer(searchReducer, initialState);
@@ -7433,6 +7445,12 @@ function DTAutosuggest({
7433
7445
  }), [sources]);
7434
7446
  // create and store input ref in the parent if storeRef is provided
7435
7447
  const inputRef = useRef(id);
7448
+ // Tracks whether the user pressed enter/clicked an item while suggestions
7449
+ // were still loading; a ref (rather than reducer state) is used because
7450
+ // setting it is a pure bookkeeping side effect of the stateReducer below,
7451
+ // not something that should itself trigger a render or round-trip through
7452
+ // another dispatch.
7453
+ const pendingEnterRef = useRef(false);
7436
7454
  useEffect(() => {
7437
7455
  if (storeRef) {
7438
7456
  storeRef(inputRef.current);
@@ -7493,7 +7511,37 @@ function DTAutosuggest({
7493
7511
  inputRef.current.blur();
7494
7512
  }
7495
7513
  }, [isMobile]);
7496
- const onSelectedItemChange = changes => selectSuggestion(changes.selectedItem, changes.highlightedIndex);
7514
+ const onSelectedItemChange = changes => {
7515
+ const {
7516
+ selectedItem
7517
+ } = changes;
7518
+ if (!selectedItem) {
7519
+ return;
7520
+ }
7521
+ // These are sentinel navigation items, not real suggestions: react to
7522
+ // them here (the designated place for side effects) instead of from
7523
+ // inside the stateReducer, which should stay a pure function computing
7524
+ // downshift's next state.
7525
+ if (selectedItem.type === 'SelectFromOwnLocations') {
7526
+ dispatch({
7527
+ type: 'SET_SOURCES',
7528
+ sources: ['Favourite', 'Back'],
7529
+ showOwnPlaces: true,
7530
+ pendingSelection: selectedItem.type
7531
+ });
7532
+ return;
7533
+ }
7534
+ if (selectedItem.type === 'back') {
7535
+ dispatch({
7536
+ type: 'SET_SOURCES',
7537
+ sources,
7538
+ showOwnPlaces: false,
7539
+ pendingSelection: null
7540
+ });
7541
+ return;
7542
+ }
7543
+ selectSuggestion(selectedItem, changes.highlightedIndex);
7544
+ };
7497
7545
  const {
7498
7546
  isOpen,
7499
7547
  highlightedIndex,
@@ -7525,36 +7573,24 @@ function DTAutosuggest({
7525
7573
  case useCombobox.stateChangeTypes.ItemClick:
7526
7574
  case useCombobox.stateChangeTypes.InputKeyDownEnter:
7527
7575
  {
7528
- // setCleared(false);
7529
- // keep enterPressedRef to make selection when suggestions have loaded
7576
+ // If suggestions are still loading, remember that a selection was
7577
+ // requested and let the effect below make it once loading
7578
+ // finishes - see the "pending enter" effect near closeMenu().
7530
7579
  if (state.loading) {
7531
- dispatch({
7532
- type: 'PENDING_ENTER',
7533
- enterPending: true
7534
- });
7580
+ pendingEnterRef.current = true;
7535
7581
  return oldState;
7536
7582
  }
7537
7583
  if (!changes.selectedItem) {
7538
7584
  return changes;
7539
7585
  }
7540
- if (changes.selectedItem.type === 'SelectFromOwnLocations') {
7541
- // if selecting from own locations, keep menu open and keep old state
7542
- dispatch({
7543
- type: 'SET_SOURCES',
7544
- sources: ['Favourite', 'Back'],
7545
- showOwnPlaces: true,
7546
- pendingSelection: changes.selectedItem.type
7547
- });
7548
- return oldState;
7549
- }
7550
- if (changes.selectedItem.type === 'back') {
7551
- dispatch({
7552
- type: 'SET_SOURCES',
7553
- sources,
7554
- showOwnPlaces: false,
7555
- pendingSelection: null
7586
+ if (changes.selectedItem.type === 'SelectFromOwnLocations' || changes.selectedItem.type === 'back') {
7587
+ // Keep the menu open for these sentinel "navigation" items
7588
+ // instead of closing/committing a normal selection. The
7589
+ // source-switching side effect itself runs in
7590
+ // onSelectedItemChange once downshift commits this change.
7591
+ return _objectSpread(_objectSpread({}, changes), {}, {
7592
+ isOpen: true
7556
7593
  });
7557
- return oldState;
7558
7594
  }
7559
7595
  return changes;
7560
7596
  }
@@ -7575,7 +7611,7 @@ function DTAutosuggest({
7575
7611
  {
7576
7612
  if (changes.selectedItem) {
7577
7613
  return _objectSpread(_objectSpread({}, changes), {}, {
7578
- selectedItem: oldState.selectItem,
7614
+ selectedItem: oldState.selectedItem,
7579
7615
  inputValue: oldState.inputValue
7580
7616
  });
7581
7617
  }
@@ -7649,11 +7685,12 @@ function DTAutosuggest({
7649
7685
 
7650
7686
  // this effect handles selecting the suggestion when enter was pressed but suggestions were still loading
7651
7687
  useEffect(() => {
7652
- if (state.enterPending && !state.loading) {
7688
+ if (pendingEnterRef.current && !state.loading) {
7689
+ pendingEnterRef.current = false;
7653
7690
  selectSuggestion(state.suggestions[0], 0);
7654
7691
  closeMenu();
7655
7692
  }
7656
- }, [state.loading, state.pendingEnter, state.suggestions]);
7693
+ }, [state.loading, state.suggestions]);
7657
7694
  const baseItemProps = useMemo(() => ({
7658
7695
  loading: state.loading,
7659
7696
  isMobile,
@@ -7728,7 +7765,8 @@ function DTAutosuggest({
7728
7765
  inputClassName: inputClassName,
7729
7766
  required: required,
7730
7767
  state: state,
7731
- dispatch: dispatch
7768
+ dispatch: dispatch,
7769
+ pendingEnterRef: pendingEnterRef
7732
7770
  }), /*#__PURE__*/React.createElement("div", {
7733
7771
  className: cx(styles['autosuggest-input-container'], styles[id], state.renderMobile && 'hidden'),
7734
7772
  style: {