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

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,
@@ -7368,6 +7386,10 @@ const getNewTargets = ({
7368
7386
  * @property {Object} [pathOpts]
7369
7387
  * @property {Object} [refPoint]
7370
7388
  * @param {DTAutosuggestProps} props
7389
+ * @param {string[]} [props.sources=[]]
7390
+ * @param {Object} [props.pathOpts={routesPrefix: 'linjat', stopsPrefix: 'pysakit'}]
7391
+ * @param {Object} [props.refPoint={}]
7392
+ * @param {Object} [props.fontWeights={medium: 500}]
7371
7393
  * @returns {JSX.Element}
7372
7394
  */
7373
7395
  function DTAutosuggest({
@@ -7389,24 +7411,19 @@ function DTAutosuggest({
7389
7411
  translatedPlaceholder,
7390
7412
  required = false,
7391
7413
  ariaLabel,
7392
- fontWeights = {
7393
- medium: 500
7394
- },
7414
+ fontWeights = DEFAULT_FONT_WEIGHTS,
7395
7415
  colors = defaultColors,
7396
7416
  modeSet,
7397
7417
  showScroll = false,
7398
7418
  isEmbedded = false,
7399
7419
  transportMode,
7400
7420
  targets,
7401
- sources = [],
7421
+ sources = DEFAULT_SOURCES,
7402
7422
  geocodingSize,
7403
7423
  filterResults,
7404
7424
  searchContext,
7405
- pathOpts = {
7406
- routesPrefix: 'linjat',
7407
- stopsPrefix: 'pysakit'
7408
- },
7409
- refPoint = {}
7425
+ pathOpts = DEFAULT_PATH_OPTS,
7426
+ refPoint = DEFAULT_REF_POINT
7410
7427
  }) {
7411
7428
  const [t] = useTranslation();
7412
7429
  const initialState = {
@@ -7418,7 +7435,6 @@ function DTAutosuggest({
7418
7435
  isCleared: false,
7419
7436
  renderMobile: false,
7420
7437
  value,
7421
- enterPending: false,
7422
7438
  isMenuOpen: false
7423
7439
  };
7424
7440
  const [state, dispatch] = useReducer(searchReducer, initialState);
@@ -7433,6 +7449,12 @@ function DTAutosuggest({
7433
7449
  }), [sources]);
7434
7450
  // create and store input ref in the parent if storeRef is provided
7435
7451
  const inputRef = useRef(id);
7452
+ // Tracks whether the user pressed enter/clicked an item while suggestions
7453
+ // were still loading; a ref (rather than reducer state) is used because
7454
+ // setting it is a pure bookkeeping side effect of the stateReducer below,
7455
+ // not something that should itself trigger a render or round-trip through
7456
+ // another dispatch.
7457
+ const pendingEnterRef = useRef(false);
7436
7458
  useEffect(() => {
7437
7459
  if (storeRef) {
7438
7460
  storeRef(inputRef.current);
@@ -7493,7 +7515,37 @@ function DTAutosuggest({
7493
7515
  inputRef.current.blur();
7494
7516
  }
7495
7517
  }, [isMobile]);
7496
- const onSelectedItemChange = changes => selectSuggestion(changes.selectedItem, changes.highlightedIndex);
7518
+ const onSelectedItemChange = changes => {
7519
+ const {
7520
+ selectedItem
7521
+ } = changes;
7522
+ if (!selectedItem) {
7523
+ return;
7524
+ }
7525
+ // These are sentinel navigation items, not real suggestions: react to
7526
+ // them here (the designated place for side effects) instead of from
7527
+ // inside the stateReducer, which should stay a pure function computing
7528
+ // downshift's next state.
7529
+ if (selectedItem.type === 'SelectFromOwnLocations') {
7530
+ dispatch({
7531
+ type: 'SET_SOURCES',
7532
+ sources: ['Favourite', 'Back'],
7533
+ showOwnPlaces: true,
7534
+ pendingSelection: selectedItem.type
7535
+ });
7536
+ return;
7537
+ }
7538
+ if (selectedItem.type === 'back') {
7539
+ dispatch({
7540
+ type: 'SET_SOURCES',
7541
+ sources,
7542
+ showOwnPlaces: false,
7543
+ pendingSelection: null
7544
+ });
7545
+ return;
7546
+ }
7547
+ selectSuggestion(selectedItem, changes.highlightedIndex);
7548
+ };
7497
7549
  const {
7498
7550
  isOpen,
7499
7551
  highlightedIndex,
@@ -7525,36 +7577,24 @@ function DTAutosuggest({
7525
7577
  case useCombobox.stateChangeTypes.ItemClick:
7526
7578
  case useCombobox.stateChangeTypes.InputKeyDownEnter:
7527
7579
  {
7528
- // setCleared(false);
7529
- // keep enterPressedRef to make selection when suggestions have loaded
7580
+ // If suggestions are still loading, remember that a selection was
7581
+ // requested and let the effect below make it once loading
7582
+ // finishes - see the "pending enter" effect near closeMenu().
7530
7583
  if (state.loading) {
7531
- dispatch({
7532
- type: 'PENDING_ENTER',
7533
- enterPending: true
7534
- });
7584
+ pendingEnterRef.current = true;
7535
7585
  return oldState;
7536
7586
  }
7537
7587
  if (!changes.selectedItem) {
7538
7588
  return changes;
7539
7589
  }
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
7590
+ if (changes.selectedItem.type === 'SelectFromOwnLocations' || changes.selectedItem.type === 'back') {
7591
+ // Keep the menu open for these sentinel "navigation" items
7592
+ // instead of closing/committing a normal selection. The
7593
+ // source-switching side effect itself runs in
7594
+ // onSelectedItemChange once downshift commits this change.
7595
+ return _objectSpread(_objectSpread({}, changes), {}, {
7596
+ isOpen: true
7556
7597
  });
7557
- return oldState;
7558
7598
  }
7559
7599
  return changes;
7560
7600
  }
@@ -7575,7 +7615,7 @@ function DTAutosuggest({
7575
7615
  {
7576
7616
  if (changes.selectedItem) {
7577
7617
  return _objectSpread(_objectSpread({}, changes), {}, {
7578
- selectedItem: oldState.selectItem,
7618
+ selectedItem: oldState.selectedItem,
7579
7619
  inputValue: oldState.inputValue
7580
7620
  });
7581
7621
  }
@@ -7649,11 +7689,12 @@ function DTAutosuggest({
7649
7689
 
7650
7690
  // this effect handles selecting the suggestion when enter was pressed but suggestions were still loading
7651
7691
  useEffect(() => {
7652
- if (state.enterPending && !state.loading) {
7692
+ if (pendingEnterRef.current && !state.loading) {
7693
+ pendingEnterRef.current = false;
7653
7694
  selectSuggestion(state.suggestions[0], 0);
7654
7695
  closeMenu();
7655
7696
  }
7656
- }, [state.loading, state.pendingEnter, state.suggestions]);
7697
+ }, [state.loading, state.suggestions]);
7657
7698
  const baseItemProps = useMemo(() => ({
7658
7699
  loading: state.loading,
7659
7700
  isMobile,
@@ -7728,7 +7769,8 @@ function DTAutosuggest({
7728
7769
  inputClassName: inputClassName,
7729
7770
  required: required,
7730
7771
  state: state,
7731
- dispatch: dispatch
7772
+ dispatch: dispatch,
7773
+ pendingEnterRef: pendingEnterRef
7732
7774
  }), /*#__PURE__*/React.createElement("div", {
7733
7775
  className: cx(styles['autosuggest-input-container'], styles[id], state.renderMobile && 'hidden'),
7734
7776
  style: {