instantsearch.js 4.83.0 → 4.84.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 = '4.83.0';
7
+ var _default = exports.default = '4.84.0';
@@ -9,6 +9,7 @@ var _preact = require("preact");
9
9
  var _hooks = require("preact/hooks");
10
10
  var _Template = _interopRequireDefault(require("../../components/Template/Template"));
11
11
  var _index = require("../../connectors/index.umd");
12
+ var _components = require("../../helpers/components");
12
13
  var _suit = require("../../lib/suit");
13
14
  var _templating = require("../../lib/templating");
14
15
  var _utils = require("../../lib/utils");
@@ -90,7 +91,12 @@ var createRenderer = function createRenderer(params) {
90
91
  rendererParams.renderState = {
91
92
  indexTemplateProps: [],
92
93
  isolatedIndex: isolatedIndex,
93
- targetIndex: targetIndex
94
+ targetIndex: targetIndex,
95
+ templateProps: (0, _templating.prepareTemplateProps)({
96
+ defaultTemplates: {},
97
+ templatesConfig: connectorParams.instantSearchInstance.templatesConfig,
98
+ templates: rendererParams.templates
99
+ })
94
100
  };
95
101
  connectorParams.refine((_targetIndex$getHelpe = (_targetIndex$getHelpe2 = targetIndex.getHelper()) === null || _targetIndex$getHelpe2 === void 0 ? void 0 : _targetIndex$getHelpe2.state.query) !== null && _targetIndex$getHelpe !== void 0 ? _targetIndex$getHelpe : '');
96
102
  return;
@@ -108,7 +114,9 @@ function AutocompleteWrapper(_ref) {
108
114
  cssClasses = _ref.cssClasses,
109
115
  renderState = _ref.renderState,
110
116
  instantSearchInstance = _ref.instantSearchInstance,
111
- showRecent = _ref.showRecent;
117
+ showRecent = _ref.showRecent,
118
+ showSuggestions = _ref.showSuggestions,
119
+ templates = _ref.templates;
112
120
  var isolatedIndex = renderState.isolatedIndex,
113
121
  targetIndex = renderState.targetIndex;
114
122
  var searchboxQuery = isolatedIndex === null || isolatedIndex === void 0 ? void 0 : (_isolatedIndex$getHel = isolatedIndex.getHelper()) === null || _isolatedIndex$getHel === void 0 ? void 0 : _isolatedIndex$getHel.state.query;
@@ -163,17 +171,28 @@ function AutocompleteWrapper(_ref) {
163
171
  getItemProps = _usePropGetters.getItemProps,
164
172
  getPanelProps = _usePropGetters.getPanelProps,
165
173
  getRootProps = _usePropGetters.getRootProps;
166
- var AutocompleteRecentSearchComponent = AutocompleteRecentSearch;
174
+ var AutocompleteRecentSearchComponent = function AutocompleteRecentSearchComponent(_ref4) {
175
+ var item = _ref4.item,
176
+ onSelect = _ref4.onSelect,
177
+ onRemoveRecentSearch = _ref4.onRemoveRecentSearch;
178
+ return (0, _preact.h)(AutocompleteRecentSearch, {
179
+ item: item,
180
+ onSelect: onSelect,
181
+ onRemoveRecentSearch: onRemoveRecentSearch
182
+ }, (0, _preact.h)(ConditionalReverseHighlight, {
183
+ item: item
184
+ }));
185
+ };
167
186
  if (_typeof(showRecent) === 'object' && (_showRecent$templates = showRecent.templates) !== null && _showRecent$templates !== void 0 && _showRecent$templates.item) {
168
187
  var props = (0, _templating.prepareTemplateProps)({
169
188
  defaultTemplates: {},
170
189
  templatesConfig: instantSearchInstance.templatesConfig,
171
190
  templates: showRecent.templates
172
191
  });
173
- AutocompleteRecentSearchComponent = function AutocompleteRecentSearchComponent(_ref4) {
174
- var item = _ref4.item,
175
- onSelect = _ref4.onSelect,
176
- onRemoveRecentSearch = _ref4.onRemoveRecentSearch;
192
+ AutocompleteRecentSearchComponent = function AutocompleteRecentSearchComponent(_ref5) {
193
+ var item = _ref5.item,
194
+ onSelect = _ref5.onSelect,
195
+ onRemoveRecentSearch = _ref5.onRemoveRecentSearch;
177
196
  return (0, _preact.h)(_Template.default, _extends({}, props, {
178
197
  templateKey: "item",
179
198
  rootTagName: "fragment",
@@ -185,45 +204,36 @@ function AutocompleteWrapper(_ref) {
185
204
  }));
186
205
  };
187
206
  }
188
- return (0, _preact.h)(Autocomplete, _extends({}, getRootProps(), {
189
- classNames: cssClasses
190
- }), (0, _preact.h)(AutocompleteSearchBox, {
191
- query: searchboxQuery || '',
192
- inputProps: _objectSpread(_objectSpread({}, getInputProps()), {}, {
193
- // @ts-ignore - This clashes with some ambient React JSX declarations.
194
- onInput: function onInput(evt) {
195
- return refine(evt.currentTarget.value);
196
- }
197
- }),
198
- onClear: function onClear() {
199
- return onRefine('');
200
- },
201
- isSearchStalled: instantSearchInstance.status === 'stalled'
202
- }), (0, _preact.h)(AutocompletePanel, getPanelProps(), showRecent && (0, _preact.h)(AutocompleteIndex
203
- // @ts-ignore - there seems to be problems with React.ComponentType and this, but it's actually correct
204
- , {
205
- ItemComponent: function ItemComponent(_ref5) {
206
- var item = _ref5.item,
207
- onSelect = _ref5.onSelect;
208
- return (0, _preact.h)(AutocompleteRecentSearchComponent, {
209
- item: item,
210
- onSelect: onSelect,
211
- onRemoveRecentSearch: function onRemoveRecentSearch() {
212
- return storage.onRemove(item.query);
213
- }
214
- });
215
- },
216
- classNames: {
217
- root: 'ais-AutocompleteRecentSearches',
218
- list: 'ais-AutocompleteRecentSearchesList',
219
- item: 'ais-AutocompleteRecentSearchesItem'
220
- },
221
- items: storageHits,
222
- getItemProps: getItemProps
223
- }), indices.map(function (_ref6, i) {
207
+ var elements = {};
208
+ if (showRecent) {
209
+ elements.recent = (0, _preact.h)(AutocompleteIndex
210
+ // @ts-ignore - there seems to be problems with React.ComponentType and this, but it's actually correct
211
+ , {
212
+ ItemComponent: function ItemComponent(_ref6) {
213
+ var item = _ref6.item,
214
+ onSelect = _ref6.onSelect;
215
+ return (0, _preact.h)(AutocompleteRecentSearchComponent, {
216
+ item: item,
217
+ onSelect: onSelect,
218
+ onRemoveRecentSearch: function onRemoveRecentSearch() {
219
+ return storage.onRemove(item.query);
220
+ }
221
+ });
222
+ },
223
+ classNames: {
224
+ root: 'ais-AutocompleteRecentSearches',
225
+ list: 'ais-AutocompleteRecentSearchesList',
226
+ item: 'ais-AutocompleteRecentSearchesItem'
227
+ },
228
+ items: storageHits,
229
+ getItemProps: getItemProps
230
+ });
231
+ }
232
+ indices.forEach(function (_ref7, i) {
224
233
  var _indicesConfig$i$temp;
225
- var indexId = _ref6.indexId,
226
- hits = _ref6.hits;
234
+ var indexId = _ref7.indexId,
235
+ indexName = _ref7.indexName,
236
+ hits = _ref7.hits;
227
237
  if (!renderState.indexTemplateProps[i]) {
228
238
  renderState.indexTemplateProps[i] = (0, _templating.prepareTemplateProps)({
229
239
  defaultTemplates: {},
@@ -231,8 +241,8 @@ function AutocompleteWrapper(_ref) {
231
241
  templates: indicesConfig[i].templates
232
242
  });
233
243
  }
234
- var headerComponent = (_indicesConfig$i$temp = indicesConfig[i].templates) !== null && _indicesConfig$i$temp !== void 0 && _indicesConfig$i$temp.header ? function (_ref7) {
235
- var items = _ref7.items;
244
+ var headerComponent = (_indicesConfig$i$temp = indicesConfig[i].templates) !== null && _indicesConfig$i$temp !== void 0 && _indicesConfig$i$temp.header ? function (_ref8) {
245
+ var items = _ref8.items;
236
246
  return (0, _preact.h)(_Template.default, _extends({}, renderState.indexTemplateProps[i], {
237
247
  templateKey: "header",
238
248
  rootTagName: "fragment",
@@ -241,9 +251,9 @@ function AutocompleteWrapper(_ref) {
241
251
  }
242
252
  }));
243
253
  } : undefined;
244
- var itemComponent = function itemComponent(_ref8) {
245
- var item = _ref8.item,
246
- onSelect = _ref8.onSelect;
254
+ var itemComponent = function itemComponent(_ref9) {
255
+ var item = _ref9.item,
256
+ onSelect = _ref9.onSelect;
247
257
  return (0, _preact.h)(_Template.default, _extends({}, renderState.indexTemplateProps[i], {
248
258
  templateKey: "item",
249
259
  rootTagName: "fragment",
@@ -253,7 +263,8 @@ function AutocompleteWrapper(_ref) {
253
263
  }
254
264
  }));
255
265
  };
256
- return (0, _preact.h)(AutocompleteIndex, {
266
+ var elementId = indexName === (showSuggestions === null || showSuggestions === void 0 ? void 0 : showSuggestions.indexName) ? 'suggestions' : indexName;
267
+ elements[elementId] = (0, _preact.h)(AutocompleteIndex, {
257
268
  key: indexId,
258
269
  HeaderComponent: headerComponent,
259
270
  ItemComponent: itemComponent,
@@ -265,26 +276,54 @@ function AutocompleteWrapper(_ref) {
265
276
  getItemProps: getItemProps,
266
277
  classNames: indicesConfig[i].cssClasses
267
278
  });
279
+ });
280
+ return (0, _preact.h)(Autocomplete, _extends({}, getRootProps(), {
281
+ classNames: cssClasses
282
+ }), (0, _preact.h)(AutocompleteSearchBox, {
283
+ query: searchboxQuery || '',
284
+ inputProps: _objectSpread(_objectSpread({}, getInputProps()), {}, {
285
+ // @ts-ignore - This clashes with some ambient React JSX declarations.
286
+ onInput: function onInput(evt) {
287
+ return refine(evt.currentTarget.value);
288
+ }
289
+ }),
290
+ onClear: function onClear() {
291
+ return onRefine('');
292
+ },
293
+ isSearchStalled: instantSearchInstance.status === 'stalled'
294
+ }), (0, _preact.h)(AutocompletePanel, getPanelProps(), templates.panel ? (0, _preact.h)(_Template.default, _extends({}, renderState.templateProps, {
295
+ templateKey: "panel",
296
+ rootTagName: "fragment",
297
+ data: {
298
+ elements: elements,
299
+ indices: indices
300
+ }
301
+ })) : Object.keys(elements).map(function (elementId) {
302
+ return elements[elementId];
268
303
  })));
269
304
  }
270
305
  function EXPERIMENTAL_autocomplete(widgetParams) {
271
- var _ref9 = widgetParams || {},
272
- container = _ref9.container,
273
- escapeHTML = _ref9.escapeHTML,
274
- _ref9$indices = _ref9.indices,
275
- indices = _ref9$indices === void 0 ? [] : _ref9$indices,
276
- showSuggestions = _ref9.showSuggestions,
277
- showRecent = _ref9.showRecent,
278
- getSearchPageURL = _ref9.getSearchPageURL,
279
- onSelect = _ref9.onSelect,
280
- _ref9$templates = _ref9.templates,
281
- templates = _ref9$templates === void 0 ? {} : _ref9$templates,
282
- _ref9$cssClasses = _ref9.cssClasses,
283
- userCssClasses = _ref9$cssClasses === void 0 ? {} : _ref9$cssClasses;
306
+ var _ref0 = widgetParams || {},
307
+ container = _ref0.container,
308
+ escapeHTML = _ref0.escapeHTML,
309
+ _ref0$indices = _ref0.indices,
310
+ indices = _ref0$indices === void 0 ? [] : _ref0$indices,
311
+ showSuggestions = _ref0.showSuggestions,
312
+ showRecent = _ref0.showRecent,
313
+ userSearchParameters = _ref0.searchParameters,
314
+ getSearchPageURL = _ref0.getSearchPageURL,
315
+ onSelect = _ref0.onSelect,
316
+ _ref0$templates = _ref0.templates,
317
+ templates = _ref0$templates === void 0 ? {} : _ref0$templates,
318
+ _ref0$cssClasses = _ref0.cssClasses,
319
+ userCssClasses = _ref0$cssClasses === void 0 ? {} : _ref0$cssClasses;
284
320
  if (!container) {
285
321
  throw new Error(withUsage('The `container` option is required.'));
286
322
  }
287
323
  var containerNode = (0, _utils.getContainerNode)(container);
324
+ var searchParameters = _objectSpread({
325
+ hitsPerPage: 5
326
+ }, userSearchParameters);
288
327
  var cssClasses = {
289
328
  root: (0, _instantsearchUiComponents.cx)(suit(), userCssClasses.root)
290
329
  };
@@ -295,7 +334,16 @@ function EXPERIMENTAL_autocomplete(widgetParams) {
295
334
  indexName: showSuggestions.indexName,
296
335
  templates: _objectSpread({
297
336
  // @ts-expect-error
298
- item: AutocompleteSuggestion
337
+ item: function item(_ref1) {
338
+ var _item = _ref1.item,
339
+ onSelectItem = _ref1.onSelect;
340
+ return (0, _preact.h)(AutocompleteSuggestion, {
341
+ item: _item,
342
+ onSelect: onSelectItem
343
+ }, (0, _preact.h)(ConditionalReverseHighlight, {
344
+ item: _item
345
+ }));
346
+ }
299
347
  }, showSuggestions.templates),
300
348
  cssClasses: {
301
349
  root: (0, _instantsearchUiComponents.cx)('ais-AutocompleteSuggestions', (_showSuggestions$cssC = showSuggestions.cssClasses) === null || _showSuggestions$cssC === void 0 ? void 0 : _showSuggestions$cssC.root),
@@ -318,10 +366,12 @@ function EXPERIMENTAL_autocomplete(widgetParams) {
318
366
  onSelect: onSelect,
319
367
  cssClasses: cssClasses,
320
368
  showRecent: showRecent,
369
+ showSuggestions: showSuggestions,
321
370
  renderState: {
322
371
  indexTemplateProps: [],
323
372
  isolatedIndex: undefined,
324
- targetIndex: undefined
373
+ targetIndex: undefined,
374
+ templateProps: undefined
325
375
  },
326
376
  templates: templates
327
377
  });
@@ -333,17 +383,29 @@ function EXPERIMENTAL_autocomplete(widgetParams) {
333
383
  })({}), (0, _index2.default)({
334
384
  indexId: "ais-autocomplete-".concat(instanceId),
335
385
  EXPERIMENTAL_isolated: true
336
- }).addWidgets([].concat(_toConsumableArray(indicesConfig.map(function (_ref0) {
337
- var indexName = _ref0.indexName;
386
+ }).addWidgets([(0, _configure.default)(searchParameters)].concat(_toConsumableArray(indicesConfig.map(function (_ref10) {
387
+ var indexName = _ref10.indexName,
388
+ indexSearchParameters = _ref10.searchParameters;
338
389
  return (0, _index2.default)({
339
390
  indexName: indexName,
340
391
  indexId: indexName
341
- }).addWidgets([(0, _configure.default)({
342
- hitsPerPage: 5
343
- })]);
392
+ }).addWidgets([(0, _configure.default)(indexSearchParameters || {})]);
344
393
  })), [_objectSpread(_objectSpread({}, makeWidget({
345
394
  escapeHTML: escapeHTML
346
395
  })), {}, {
347
396
  $$widgetType: 'ais.autocomplete'
348
397
  })]))];
398
+ }
399
+ function ConditionalReverseHighlight(_ref11) {
400
+ var _item$_highlightResul;
401
+ var item = _ref11.item;
402
+ if (!((_item$_highlightResul = item._highlightResult) !== null && _item$_highlightResul !== void 0 && _item$_highlightResul.query) ||
403
+ // @ts-expect-error - we should not have matchLevel as arrays here
404
+ item._highlightResult.query.matchLevel === 'none') {
405
+ return item.query;
406
+ }
407
+ return (0, _preact.h)(_components.ReverseHighlight, {
408
+ attribute: "query",
409
+ hit: item
410
+ });
349
411
  }
@@ -204,6 +204,12 @@ var index = function index(widgetParams) {
204
204
  helper: helper
205
205
  })));
206
206
  },
207
+ scheduleLocalSearch: (0, _utils.defer)(function () {
208
+ if (isolated) {
209
+ var _helper2;
210
+ (_helper2 = helper) === null || _helper2 === void 0 ? void 0 : _helper2.search();
211
+ }
212
+ }),
207
213
  getWidgets: function getWidgets() {
208
214
  return localWidgets;
209
215
  },
@@ -269,8 +275,7 @@ var index = function index(widgetParams) {
269
275
  }
270
276
  });
271
277
  if (isolated) {
272
- var _helper2;
273
- (_helper2 = helper) === null || _helper2 === void 0 ? void 0 : _helper2.search();
278
+ this.scheduleLocalSearch();
274
279
  } else {
275
280
  localInstantSearchInstance.scheduleSearch();
276
281
  }
@@ -349,8 +354,7 @@ var index = function index(widgetParams) {
349
354
  helper.recommendState = cleanedRecommendState;
350
355
  if (localWidgets.length) {
351
356
  if (isolated) {
352
- var _helper3;
353
- (_helper3 = helper) === null || _helper3 === void 0 ? void 0 : _helper3.search();
357
+ this.scheduleLocalSearch();
354
358
  } else {
355
359
  localInstantSearchInstance.scheduleSearch();
356
360
  }
@@ -604,7 +608,7 @@ var index = function index(widgetParams) {
604
608
  },
605
609
  dispose: function dispose() {
606
610
  var _this5 = this,
607
- _helper4,
611
+ _helper3,
608
612
  _derivedHelper3;
609
613
  localWidgets.forEach(function (widget) {
610
614
  if (widget.dispose && helper) {
@@ -624,7 +628,7 @@ var index = function index(widgetParams) {
624
628
  });
625
629
  localInstantSearchInstance = null;
626
630
  localParent = null;
627
- (_helper4 = helper) === null || _helper4 === void 0 ? void 0 : _helper4.removeAllListeners();
631
+ (_helper3 = helper) === null || _helper3 === void 0 ? void 0 : _helper3.removeAllListeners();
628
632
  helper = null;
629
633
  (_derivedHelper3 = derivedHelper) === null || _derivedHelper3 === void 0 ? void 0 : _derivedHelper3.detach();
630
634
  derivedHelper = null;
@@ -309,7 +309,17 @@ declare type AutocompleteRenderState = {
309
309
  refine: (query: string) => void;
310
310
  };
311
311
 
312
- declare type AutocompleteTemplates<TItem extends BaseHit> = Partial<Record<string, TItem>>;
312
+ declare type AutocompleteSearchParameters = Omit<PlainSearchParameters, 'index'>;
313
+
314
+ declare type AutocompleteTemplates = {
315
+ /**
316
+ * Template to use for the panel.
317
+ */
318
+ panel?: Template<{
319
+ elements: PanelElements;
320
+ indices: AutocompleteRenderState['indices'];
321
+ }>;
322
+ };
313
323
 
314
324
  declare type AutocompleteWidgetDescription = {
315
325
  $$type: 'ais.autocomplete';
@@ -355,12 +365,16 @@ declare type AutocompleteWidgetParams<TItem extends BaseHit> = {
355
365
  }>;
356
366
  }>;
357
367
  };
368
+ /**
369
+ * Search parameters to apply to the autocomplete indices.
370
+ */
371
+ searchParameters?: AutocompleteSearchParameters;
358
372
  getSearchPageURL?: (nextUiState: IndexUiState) => string;
359
373
  onSelect?: AutocompleteIndexConfig<TItem>['onSelect'];
360
374
  /**
361
375
  * Templates to use for the widget.
362
376
  */
363
- templates?: AutocompleteTemplates<TItem>;
377
+ templates?: AutocompleteTemplates;
364
378
  /**
365
379
  * CSS classes to add.
366
380
  */
@@ -4887,6 +4901,10 @@ declare type IndexConfig<TItem extends BaseHit> = AutocompleteIndexConfig<TItem>
4887
4901
  onSelect: () => void;
4888
4902
  }>;
4889
4903
  }>;
4904
+ /**
4905
+ * Search parameters to apply to this index.
4906
+ */
4907
+ searchParameters?: AutocompleteSearchParameters;
4890
4908
  cssClasses?: Partial<AutocompleteIndexClassNames>;
4891
4909
  };
4892
4910
 
@@ -4947,6 +4965,11 @@ declare type IndexWidget<TUiState extends UiState = UiState> = Omit<Widget<Index
4947
4965
  * @private
4948
4966
  */
4949
4967
  _isolated: boolean;
4968
+ /**
4969
+ * Schedules a search for this index only.
4970
+ * @private
4971
+ */
4972
+ scheduleLocalSearch: () => void;
4950
4973
  };
4951
4974
 
4952
4975
  declare type IndexWidgetDescription = {
@@ -6350,6 +6373,8 @@ declare type PanelCSSClasses = Partial<{
6350
6373
  footer: string | string[];
6351
6374
  }>;
6352
6375
 
6376
+ declare type PanelElements = Partial<Record<'recent' | 'suggestions' | (string & {}), preact.JSX.Element>>;
6377
+
6353
6378
  declare type PanelRenderOptions<TWidgetFactory extends AnyWidgetFactory> = RenderOptions & GetWidgetRenderState<TWidgetFactory>;
6354
6379
 
6355
6380
  declare type PanelTemplates<TWidget extends AnyWidgetFactory> = Partial<{