react-instantsearch-core 7.21.0 → 7.22.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.
@@ -4,4 +4,4 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _default = exports.default = '7.21.0';
7
+ var _default = exports.default = '7.22.0';
@@ -1,2 +1,2 @@
1
- declare const _default: "7.21.0";
1
+ declare const _default: "7.22.0";
2
2
  export default _default;
@@ -1 +1 @@
1
- export default '7.21.0';
1
+ export default '7.22.0';
@@ -7,7 +7,7 @@
7
7
 
8
8
  var React__default = 'default' in React ? React['default'] : React;
9
9
 
10
- var version = '7.21.0';
10
+ var version = '7.22.0';
11
11
 
12
12
  function _arrayLikeToArray(r, a) {
13
13
  (null == a || a > r.length) && (a = r.length);
@@ -4772,7 +4772,7 @@
4772
4772
 
4773
4773
  var sortAndMergeRecommendations_1 = sortAndMergeRecommendations;
4774
4774
 
4775
- var version$1 = '3.26.1';
4775
+ var version$1 = '3.27.0';
4776
4776
 
4777
4777
  var escapeFacetValue$3 = escapeFacetValue_1.escapeFacetValue;
4778
4778
 
@@ -12833,7 +12833,7 @@
12833
12833
  };
12834
12834
  }
12835
12835
 
12836
- var version$2 = '4.85.2';
12836
+ var version$2 = '4.86.0';
12837
12837
 
12838
12838
  function _typeof$l(o) {
12839
12839
  "@babel/helpers - typeof";
@@ -18589,10 +18589,33 @@
18589
18589
 
18590
18590
  /**
18591
18591
  * The **SortBy** connector provides the logic to build a custom widget that will display a
18592
- * list of indices. With Algolia, this is most commonly used for changing ranking strategy. This allows
18593
- * a user to change how the hits are being sorted.
18592
+ * list of indices or sorting strategies. With Algolia, this is most commonly used for changing
18593
+ * ranking strategy. This allows a user to change how the hits are being sorted.
18594
+ *
18595
+ * This connector supports two sorting modes:
18596
+ * 1. **Index-based (traditional)**: Uses the `value` property to switch between different indices.
18597
+ * This is the standard behavior for non-composition setups.
18598
+ *
18599
+ * 2. **Strategy-based (composition mode)**: Uses the `strategy` property to apply sorting strategies
18600
+ * via the `sortBy` search parameter. This is only available when using Algolia Compositions.
18601
+ *
18602
+ * Items can mix both types in the same widget, allowing for flexible sorting options.
18594
18603
  */
18595
18604
 
18605
+ function isStrategyItem(item) {
18606
+ return 'strategy' in item && item.strategy !== undefined;
18607
+ }
18608
+ function getItemValue(item) {
18609
+ if (isStrategyItem(item)) {
18610
+ return item.strategy;
18611
+ }
18612
+ return item.value;
18613
+ }
18614
+ function isValidStrategy(itemsLookup, value) {
18615
+ if (!value) return false;
18616
+ var item = itemsLookup[value];
18617
+ return item !== undefined && isStrategyItem(item);
18618
+ }
18596
18619
  var connectSortBy = function connectSortBy(renderFn) {
18597
18620
  var unmountFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
18598
18621
  checkRendering(renderFn, withUsage$m());
@@ -18607,14 +18630,38 @@
18607
18630
  if (!Array.isArray(items)) {
18608
18631
  throw new Error(withUsage$m('The `items` option expects an array of objects.'));
18609
18632
  }
18633
+ var itemsLookup = {};
18634
+ items.forEach(function (item, index) {
18635
+ var hasValue = 'value' in item && item.value !== undefined;
18636
+ var hasStrategy = 'strategy' in item && item.strategy !== undefined;
18637
+
18638
+ // Validate mutual exclusivity
18639
+ if (hasValue && hasStrategy) {
18640
+ throw new Error(withUsage$m("Item at index ".concat(index, " cannot have both \"value\" and \"strategy\" properties.")));
18641
+ }
18642
+ if (!hasValue && !hasStrategy) {
18643
+ throw new Error(withUsage$m("Item at index ".concat(index, " must have either a \"value\" or \"strategy\" property.")));
18644
+ }
18645
+ var itemValue = getItemValue(item);
18646
+ itemsLookup[itemValue] = item;
18647
+ });
18648
+ connectorState.itemsLookup = itemsLookup;
18610
18649
  return {
18611
18650
  $$type: 'ais.sortBy',
18612
18651
  init: function init(initOptions) {
18613
18652
  var instantSearchInstance = initOptions.instantSearchInstance;
18653
+
18654
+ // Check if strategies are used outside composition mode
18655
+ var hasStrategyItems = items.some(function (item) {
18656
+ return 'strategy' in item && item.strategy;
18657
+ });
18658
+ if (hasStrategyItems && !instantSearchInstance.compositionID) {
18659
+ throw new Error(withUsage$m('Sorting strategies can only be used in composition mode. Please provide a "compositionID" to your InstantSearch instance.'));
18660
+ }
18614
18661
  var widgetRenderState = this.getWidgetRenderState(initOptions);
18615
18662
  var currentIndex = widgetRenderState.currentRefinement;
18616
18663
  var isCurrentIndexInItems = find$1(items, function (item) {
18617
- return item.value === currentIndex;
18664
+ return getItemValue(item) === currentIndex;
18618
18665
  });
18619
18666
  renderFn(_objectSpread$z(_objectSpread$z({}, widgetRenderState), {}, {
18620
18667
  instantSearchInstance: instantSearchInstance
@@ -18629,7 +18676,17 @@
18629
18676
  dispose: function dispose(_ref2) {
18630
18677
  var state = _ref2.state;
18631
18678
  unmountFn();
18632
- return connectorState.initialIndex ? state.setIndex(connectorState.initialIndex) : state;
18679
+
18680
+ // Clear sortBy parameter if it was set
18681
+ if (connectorState.isUsingComposition && state.sortBy) {
18682
+ state = state.setQueryParameter('sortBy', undefined);
18683
+ }
18684
+
18685
+ // Restore initial index if changed
18686
+ if (connectorState.initialValue && state.index !== connectorState.initialValue) {
18687
+ return state.setIndex(connectorState.initialValue);
18688
+ }
18689
+ return state;
18633
18690
  },
18634
18691
  getRenderState: function getRenderState(renderState, renderOptions) {
18635
18692
  return _objectSpread$z(_objectSpread$z({}, renderState), {}, {
@@ -18640,22 +18697,54 @@
18640
18697
  var results = _ref3.results,
18641
18698
  helper = _ref3.helper,
18642
18699
  state = _ref3.state,
18643
- parent = _ref3.parent;
18644
- if (!connectorState.initialIndex && parent) {
18645
- connectorState.initialIndex = parent.getIndexName();
18700
+ parent = _ref3.parent,
18701
+ instantSearchInstance = _ref3.instantSearchInstance;
18702
+ // Capture initial value (composition ID or main index)
18703
+ if (!connectorState.initialValue && parent) {
18704
+ connectorState.initialValue = parent.getIndexName();
18646
18705
  }
18647
- if (!connectorState.setIndex) {
18648
- connectorState.setIndex = function (indexName) {
18649
- helper.setIndex(indexName).search();
18706
+
18707
+ // Create refine function if not exists
18708
+ if (!connectorState.refine) {
18709
+ // Cache composition mode status for lifecycle methods that don't have access to instantSearchInstance
18710
+ connectorState.isUsingComposition = Boolean(instantSearchInstance === null || instantSearchInstance === void 0 ? void 0 : instantSearchInstance.compositionID);
18711
+ connectorState.refine = function (value) {
18712
+ // O(1) lookup using the items lookup table
18713
+ var item = connectorState.itemsLookup[value];
18714
+ if (item && isStrategyItem(item)) {
18715
+ // Strategy-based: set sortBy parameter for composition API
18716
+ // The composition backend will interpret this and apply the sorting strategy
18717
+ helper.setQueryParameter('sortBy', item.strategy).search();
18718
+ } else {
18719
+ // Index-based: clear any existing sortBy parameter and switch to the new index
18720
+ // Clearing sortBy is critical when transitioning from strategy to index-based sorting
18721
+ helper.setQueryParameter('sortBy', undefined).setIndex(value).search();
18722
+ }
18650
18723
  };
18651
18724
  }
18725
+
18726
+ // Transform items first (on original structure)
18727
+ var transformedItems = transformItems(items, {
18728
+ results: results
18729
+ });
18730
+
18731
+ // Normalize items: all get a 'value' property for the render state
18732
+ var normalizedItems = transformedItems.map(function (item) {
18733
+ return {
18734
+ label: item.label,
18735
+ value: getItemValue(item)
18736
+ };
18737
+ });
18738
+
18739
+ // Determine current refinement
18740
+ // In composition mode, prefer sortBy parameter if it corresponds to a valid strategy item
18741
+ // Otherwise use the index (for index-based items or when no valid strategy is active)
18742
+ var currentRefinement = connectorState.isUsingComposition && isValidStrategy(connectorState.itemsLookup, state.sortBy) ? state.sortBy : state.index;
18652
18743
  var hasNoResults = results ? results.nbHits === 0 : true;
18653
18744
  return {
18654
- currentRefinement: state.index,
18655
- options: transformItems(items, {
18656
- results: results
18657
- }),
18658
- refine: connectorState.setIndex,
18745
+ currentRefinement: currentRefinement,
18746
+ options: normalizedItems,
18747
+ refine: connectorState.refine,
18659
18748
  hasNoResults: hasNoResults,
18660
18749
  canRefine: !hasNoResults && items.length > 0,
18661
18750
  widgetParams: widgetParams
@@ -18663,14 +18752,25 @@
18663
18752
  },
18664
18753
  getWidgetUiState: function getWidgetUiState(uiState, _ref4) {
18665
18754
  var searchParameters = _ref4.searchParameters;
18666
- var currentIndex = searchParameters.index;
18755
+ // In composition mode with an active strategy, use sortBy parameter
18756
+ // Otherwise use index-based behavior (traditional mode)
18757
+ var currentValue = connectorState.isUsingComposition && isValidStrategy(connectorState.itemsLookup, searchParameters.sortBy) ? searchParameters.sortBy : searchParameters.index;
18667
18758
  return _objectSpread$z(_objectSpread$z({}, uiState), {}, {
18668
- sortBy: currentIndex !== connectorState.initialIndex ? currentIndex : undefined
18759
+ sortBy: currentValue !== connectorState.initialValue ? currentValue : undefined
18669
18760
  });
18670
18761
  },
18671
18762
  getWidgetSearchParameters: function getWidgetSearchParameters(searchParameters, _ref5) {
18672
18763
  var uiState = _ref5.uiState;
18673
- return searchParameters.setQueryParameter('index', uiState.sortBy || connectorState.initialIndex || searchParameters.index);
18764
+ var sortByValue = uiState.sortBy || connectorState.initialValue || searchParameters.index;
18765
+ if (isValidStrategy(connectorState.itemsLookup, sortByValue)) {
18766
+ var item = connectorState.itemsLookup[sortByValue];
18767
+ // Strategy-based: set the sortBy parameter for composition API
18768
+ // The index remains as the compositionID
18769
+ return searchParameters.setQueryParameter('sortBy', item.strategy);
18770
+ }
18771
+
18772
+ // Index-based: set the index parameter (traditional behavior)
18773
+ return searchParameters.setQueryParameter('index', sortByValue);
18674
18774
  }
18675
18775
  };
18676
18776
  };