@algolia/autocomplete-core 1.10.0 → 1.11.1

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.
@@ -3,9 +3,8 @@ var _excluded = ["props", "refresh", "store"],
3
3
  _excluded2 = ["inputElement", "formElement", "panelElement"],
4
4
  _excluded3 = ["inputElement"],
5
5
  _excluded4 = ["inputElement", "maxLength"],
6
- _excluded5 = ["sourceIndex"],
7
- _excluded6 = ["sourceIndex"],
8
- _excluded7 = ["item", "source", "sourceIndex"];
6
+ _excluded5 = ["source"],
7
+ _excluded6 = ["item", "source"];
9
8
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
10
9
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
11
10
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
@@ -16,7 +15,7 @@ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) r
16
15
  import { noop } from '@algolia/autocomplete-shared';
17
16
  import { onInput } from './onInput';
18
17
  import { onKeyDown as _onKeyDown } from './onKeyDown';
19
- import { getActiveItem, isOrContainsNode, isSamsung } from './utils';
18
+ import { getActiveItem, getAutocompleteElementId, isOrContainsNode, isSamsung } from './utils';
20
19
  export function getPropGetters(_ref) {
21
20
  var props = _ref.props,
22
21
  refresh = _ref.refresh,
@@ -85,8 +84,11 @@ export function getPropGetters(_ref) {
85
84
  role: 'combobox',
86
85
  'aria-expanded': store.getState().isOpen,
87
86
  'aria-haspopup': 'listbox',
88
- 'aria-owns': store.getState().isOpen ? "".concat(props.id, "-list") : undefined,
89
- 'aria-labelledby': "".concat(props.id, "-label")
87
+ 'aria-owns': store.getState().isOpen ? store.getState().collections.map(function (_ref2) {
88
+ var source = _ref2.source;
89
+ return getAutocompleteElementId(props.id, 'list', source);
90
+ }).join(' ') : undefined,
91
+ 'aria-labelledby': getAutocompleteElementId(props.id, 'label')
90
92
  }, rest);
91
93
  };
92
94
  var getFormProps = function getFormProps(providedProps) {
@@ -136,22 +138,25 @@ export function getPropGetters(_ref) {
136
138
  }
137
139
  store.dispatch('focus', null);
138
140
  }
139
- var _ref2 = providedProps || {},
140
- inputElement = _ref2.inputElement,
141
- _ref2$maxLength = _ref2.maxLength,
142
- maxLength = _ref2$maxLength === void 0 ? 512 : _ref2$maxLength,
143
- rest = _objectWithoutProperties(_ref2, _excluded4);
141
+ var _ref3 = providedProps || {},
142
+ inputElement = _ref3.inputElement,
143
+ _ref3$maxLength = _ref3.maxLength,
144
+ maxLength = _ref3$maxLength === void 0 ? 512 : _ref3$maxLength,
145
+ rest = _objectWithoutProperties(_ref3, _excluded4);
144
146
  var activeItem = getActiveItem(store.getState());
145
147
  var userAgent = ((_props$environment$na = props.environment.navigator) === null || _props$environment$na === void 0 ? void 0 : _props$environment$na.userAgent) || '';
146
148
  var shouldFallbackKeyHint = isSamsung(userAgent);
147
149
  var enterKeyHint = props.enterKeyHint || (activeItem !== null && activeItem !== void 0 && activeItem.itemUrl && !shouldFallbackKeyHint ? 'go' : 'search');
148
150
  return _objectSpread({
149
151
  'aria-autocomplete': 'both',
150
- 'aria-activedescendant': store.getState().isOpen && store.getState().activeItemId !== null ? "".concat(props.id, "-item-").concat(store.getState().activeItemId) : undefined,
151
- 'aria-controls': store.getState().isOpen ? "".concat(props.id, "-list") : undefined,
152
- 'aria-labelledby': "".concat(props.id, "-label"),
152
+ 'aria-activedescendant': store.getState().isOpen && store.getState().activeItemId !== null ? getAutocompleteElementId(props.id, "item-".concat(store.getState().activeItemId), activeItem === null || activeItem === void 0 ? void 0 : activeItem.source) : undefined,
153
+ 'aria-controls': store.getState().isOpen ? store.getState().collections.map(function (_ref4) {
154
+ var source = _ref4.source;
155
+ return getAutocompleteElementId(props.id, 'list', source);
156
+ }).join(' ') : undefined,
157
+ 'aria-labelledby': getAutocompleteElementId(props.id, 'label'),
153
158
  value: store.getState().completion || store.getState().query,
154
- id: "".concat(props.id, "-input"),
159
+ id: getAutocompleteElementId(props.id, 'input'),
155
160
  autoComplete: 'off',
156
161
  autoCorrect: 'off',
157
162
  autoCapitalize: 'off',
@@ -197,26 +202,20 @@ export function getPropGetters(_ref) {
197
202
  }
198
203
  }, rest);
199
204
  };
200
- var getAutocompleteId = function getAutocompleteId(instanceId, sourceId) {
201
- return typeof sourceId !== 'undefined' ? "".concat(instanceId, "-").concat(sourceId) : instanceId;
202
- };
203
- var getLabelProps = function getLabelProps(providedProps) {
204
- var _ref3 = providedProps || {},
205
- sourceIndex = _ref3.sourceIndex,
206
- rest = _objectWithoutProperties(_ref3, _excluded5);
205
+ var getLabelProps = function getLabelProps(rest) {
207
206
  return _objectSpread({
208
- htmlFor: "".concat(getAutocompleteId(props.id, sourceIndex), "-input"),
209
- id: "".concat(getAutocompleteId(props.id, sourceIndex), "-label")
207
+ htmlFor: getAutocompleteElementId(props.id, 'input'),
208
+ id: getAutocompleteElementId(props.id, 'label')
210
209
  }, rest);
211
210
  };
212
211
  var getListProps = function getListProps(providedProps) {
213
- var _ref4 = providedProps || {},
214
- sourceIndex = _ref4.sourceIndex,
215
- rest = _objectWithoutProperties(_ref4, _excluded6);
212
+ var _ref5 = providedProps || {},
213
+ source = _ref5.source,
214
+ rest = _objectWithoutProperties(_ref5, _excluded5);
216
215
  return _objectSpread({
217
216
  role: 'listbox',
218
- 'aria-labelledby': "".concat(getAutocompleteId(props.id, sourceIndex), "-label"),
219
- id: "".concat(getAutocompleteId(props.id, sourceIndex), "-list")
217
+ 'aria-labelledby': getAutocompleteElementId(props.id, 'label'),
218
+ id: getAutocompleteElementId(props.id, 'list', source)
220
219
  }, rest);
221
220
  };
222
221
  var getPanelProps = function getPanelProps(rest) {
@@ -235,10 +234,9 @@ export function getPropGetters(_ref) {
235
234
  var getItemProps = function getItemProps(providedProps) {
236
235
  var item = providedProps.item,
237
236
  source = providedProps.source,
238
- sourceIndex = providedProps.sourceIndex,
239
- rest = _objectWithoutProperties(providedProps, _excluded7);
237
+ rest = _objectWithoutProperties(providedProps, _excluded6);
240
238
  return _objectSpread({
241
- id: "".concat(getAutocompleteId(props.id, sourceIndex), "-item-").concat(item.__autocomplete_id),
239
+ id: getAutocompleteElementId(props.id, "item-".concat(item.__autocomplete_id), source),
242
240
  role: 'option',
243
241
  'aria-selected': store.getState().activeItemId === item.__autocomplete_id,
244
242
  onMouseMove: function onMouseMove(event) {
@@ -8,7 +8,7 @@ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input ==
8
8
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
9
9
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
10
10
  import { onInput } from './onInput';
11
- import { getActiveItem } from './utils';
11
+ import { getActiveItem, getAutocompleteElementId } from './utils';
12
12
  export function onKeyDown(_ref) {
13
13
  var event = _ref.event,
14
14
  props = _ref.props,
@@ -18,7 +18,8 @@ export function onKeyDown(_ref) {
18
18
  if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
19
19
  // eslint-disable-next-line no-inner-declarations
20
20
  var triggerScrollIntoView = function triggerScrollIntoView() {
21
- var nodeItem = props.environment.document.getElementById("".concat(props.id, "-item-").concat(store.getState().activeItemId));
21
+ var highlightedItem = getActiveItem(store.getState());
22
+ var nodeItem = props.environment.document.getElementById(getAutocompleteElementId(props.id, "item-".concat(store.getState().activeItemId), highlightedItem === null || highlightedItem === void 0 ? void 0 : highlightedItem.source));
22
23
  if (nodeItem) {
23
24
  if (nodeItem.scrollIntoViewIfNeeded) {
24
25
  nodeItem.scrollIntoViewIfNeeded(false);
@@ -0,0 +1,9 @@
1
+ import type { InternalAutocompleteSource } from '../types';
2
+ /**
3
+ * Returns a full element id for an autocomplete element.
4
+ *
5
+ * @param autocompleteInstanceId The id of the autocomplete instance
6
+ * @param elementId The specific element id
7
+ * @param source The source of the element, when it needs to be scoped
8
+ */
9
+ export declare function getAutocompleteElementId(autocompleteInstanceId: string, elementId: string, source?: InternalAutocompleteSource<any>): string;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Returns a full element id for an autocomplete element.
3
+ *
4
+ * @param autocompleteInstanceId The id of the autocomplete instance
5
+ * @param elementId The specific element id
6
+ * @param source The source of the element, when it needs to be scoped
7
+ */
8
+ export function getAutocompleteElementId(autocompleteInstanceId, elementId, source) {
9
+ return [autocompleteInstanceId, source === null || source === void 0 ? void 0 : source.sourceId, elementId].filter(Boolean).join('-').replace(/\s/g, '');
10
+ }
@@ -4,6 +4,7 @@ export * from './createConcurrentSafePromise';
4
4
  export * from './getNextActiveItemId';
5
5
  export * from './getNormalizedSources';
6
6
  export * from './getActiveItem';
7
+ export * from './getAutocompleteElementId';
7
8
  export * from './isOrContainsNode';
8
9
  export * from './isSamsung';
9
10
  export * from './mapToAlgoliaResponse';
@@ -4,6 +4,7 @@ export * from './createConcurrentSafePromise';
4
4
  export * from './getNextActiveItemId';
5
5
  export * from './getNormalizedSources';
6
6
  export * from './getActiveItem';
7
+ export * from './getAutocompleteElementId';
7
8
  export * from './isOrContainsNode';
8
9
  export * from './isSamsung';
9
10
  export * from './mapToAlgoliaResponse';
@@ -1,4 +1,4 @@
1
- /*! @algolia/autocomplete-core 1.10.0 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/autocomplete */
1
+ /*! @algolia/autocomplete-core 1.11.1 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/autocomplete */
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4
4
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
@@ -291,7 +291,7 @@
291
291
  return undefined;
292
292
  }
293
293
 
294
- var version = '1.10.0';
294
+ var version = '1.11.1';
295
295
 
296
296
  var userAgents = [{
297
297
  segment: 'autocomplete-core',
@@ -884,14 +884,23 @@
884
884
  var setContext = _ref4.setContext,
885
885
  onSelect = _ref4.onSelect,
886
886
  onActive = _ref4.onActive;
887
+ function setInsightsContext(userToken) {
888
+ setContext({
889
+ algoliaInsightsPlugin: {
890
+ __algoliaSearchParameters: _objectSpread({
891
+ clickAnalytics: true
892
+ }, userToken ? {
893
+ userToken: userToken
894
+ } : {}),
895
+ insights: insights
896
+ }
897
+ });
898
+ }
887
899
  insightsClient('addAlgoliaAgent', 'insights-plugin');
888
- setContext({
889
- algoliaInsightsPlugin: {
890
- __algoliaSearchParameters: {
891
- clickAnalytics: true
892
- },
893
- insights: insights
894
- }
900
+ setInsightsContext();
901
+ insightsClient('onUserTokenChange', setInsightsContext);
902
+ insightsClient('getUserToken', null, function (_error, userToken) {
903
+ setInsightsContext(userToken);
895
904
  });
896
905
  onSelect(function (_ref5) {
897
906
  var item = _ref5.item,
@@ -1247,6 +1256,17 @@
1247
1256
  };
1248
1257
  }
1249
1258
 
1259
+ /**
1260
+ * Returns a full element id for an autocomplete element.
1261
+ *
1262
+ * @param autocompleteInstanceId The id of the autocomplete instance
1263
+ * @param elementId The specific element id
1264
+ * @param source The source of the element, when it needs to be scoped
1265
+ */
1266
+ function getAutocompleteElementId(autocompleteInstanceId, elementId, source) {
1267
+ return [autocompleteInstanceId, source === null || source === void 0 ? void 0 : source.sourceId, elementId].filter(Boolean).join('-').replace(/\s/g, '');
1268
+ }
1269
+
1250
1270
  function isOrContainsNode(parent, child) {
1251
1271
  return parent === child || parent.contains(child);
1252
1272
  }
@@ -1725,7 +1745,8 @@
1725
1745
  if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
1726
1746
  // eslint-disable-next-line no-inner-declarations
1727
1747
  var triggerScrollIntoView = function triggerScrollIntoView() {
1728
- var nodeItem = props.environment.document.getElementById("".concat(props.id, "-item-").concat(store.getState().activeItemId));
1748
+ var highlightedItem = getActiveItem(store.getState());
1749
+ var nodeItem = props.environment.document.getElementById(getAutocompleteElementId(props.id, "item-".concat(store.getState().activeItemId), highlightedItem === null || highlightedItem === void 0 ? void 0 : highlightedItem.source));
1729
1750
  if (nodeItem) {
1730
1751
  if (nodeItem.scrollIntoViewIfNeeded) {
1731
1752
  nodeItem.scrollIntoViewIfNeeded(false);
@@ -1903,9 +1924,8 @@
1903
1924
  _excluded2 = ["inputElement", "formElement", "panelElement"],
1904
1925
  _excluded3 = ["inputElement"],
1905
1926
  _excluded4 = ["inputElement", "maxLength"],
1906
- _excluded5 = ["sourceIndex"],
1907
- _excluded6 = ["sourceIndex"],
1908
- _excluded7 = ["item", "source", "sourceIndex"];
1927
+ _excluded5 = ["source"],
1928
+ _excluded6 = ["item", "source"];
1909
1929
  function getPropGetters(_ref) {
1910
1930
  var props = _ref.props,
1911
1931
  refresh = _ref.refresh,
@@ -1974,8 +1994,11 @@
1974
1994
  role: 'combobox',
1975
1995
  'aria-expanded': store.getState().isOpen,
1976
1996
  'aria-haspopup': 'listbox',
1977
- 'aria-owns': store.getState().isOpen ? "".concat(props.id, "-list") : undefined,
1978
- 'aria-labelledby': "".concat(props.id, "-label")
1997
+ 'aria-owns': store.getState().isOpen ? store.getState().collections.map(function (_ref2) {
1998
+ var source = _ref2.source;
1999
+ return getAutocompleteElementId(props.id, 'list', source);
2000
+ }).join(' ') : undefined,
2001
+ 'aria-labelledby': getAutocompleteElementId(props.id, 'label')
1979
2002
  }, rest);
1980
2003
  };
1981
2004
  var getFormProps = function getFormProps(providedProps) {
@@ -2025,22 +2048,25 @@
2025
2048
  }
2026
2049
  store.dispatch('focus', null);
2027
2050
  }
2028
- var _ref2 = providedProps || {};
2029
- _ref2.inputElement;
2030
- var _ref2$maxLength = _ref2.maxLength,
2031
- maxLength = _ref2$maxLength === void 0 ? 512 : _ref2$maxLength,
2032
- rest = _objectWithoutProperties$1(_ref2, _excluded4);
2051
+ var _ref3 = providedProps || {};
2052
+ _ref3.inputElement;
2053
+ var _ref3$maxLength = _ref3.maxLength,
2054
+ maxLength = _ref3$maxLength === void 0 ? 512 : _ref3$maxLength,
2055
+ rest = _objectWithoutProperties$1(_ref3, _excluded4);
2033
2056
  var activeItem = getActiveItem(store.getState());
2034
2057
  var userAgent = ((_props$environment$na = props.environment.navigator) === null || _props$environment$na === void 0 ? void 0 : _props$environment$na.userAgent) || '';
2035
2058
  var shouldFallbackKeyHint = isSamsung(userAgent);
2036
2059
  var enterKeyHint = props.enterKeyHint || (activeItem !== null && activeItem !== void 0 && activeItem.itemUrl && !shouldFallbackKeyHint ? 'go' : 'search');
2037
2060
  return _objectSpread2({
2038
2061
  'aria-autocomplete': 'both',
2039
- 'aria-activedescendant': store.getState().isOpen && store.getState().activeItemId !== null ? "".concat(props.id, "-item-").concat(store.getState().activeItemId) : undefined,
2040
- 'aria-controls': store.getState().isOpen ? "".concat(props.id, "-list") : undefined,
2041
- 'aria-labelledby': "".concat(props.id, "-label"),
2062
+ 'aria-activedescendant': store.getState().isOpen && store.getState().activeItemId !== null ? getAutocompleteElementId(props.id, "item-".concat(store.getState().activeItemId), activeItem === null || activeItem === void 0 ? void 0 : activeItem.source) : undefined,
2063
+ 'aria-controls': store.getState().isOpen ? store.getState().collections.map(function (_ref4) {
2064
+ var source = _ref4.source;
2065
+ return getAutocompleteElementId(props.id, 'list', source);
2066
+ }).join(' ') : undefined,
2067
+ 'aria-labelledby': getAutocompleteElementId(props.id, 'label'),
2042
2068
  value: store.getState().completion || store.getState().query,
2043
- id: "".concat(props.id, "-input"),
2069
+ id: getAutocompleteElementId(props.id, 'input'),
2044
2070
  autoComplete: 'off',
2045
2071
  autoCorrect: 'off',
2046
2072
  autoCapitalize: 'off',
@@ -2086,26 +2112,20 @@
2086
2112
  }
2087
2113
  }, rest);
2088
2114
  };
2089
- var getAutocompleteId = function getAutocompleteId(instanceId, sourceId) {
2090
- return typeof sourceId !== 'undefined' ? "".concat(instanceId, "-").concat(sourceId) : instanceId;
2091
- };
2092
- var getLabelProps = function getLabelProps(providedProps) {
2093
- var _ref3 = providedProps || {},
2094
- sourceIndex = _ref3.sourceIndex,
2095
- rest = _objectWithoutProperties$1(_ref3, _excluded5);
2115
+ var getLabelProps = function getLabelProps(rest) {
2096
2116
  return _objectSpread2({
2097
- htmlFor: "".concat(getAutocompleteId(props.id, sourceIndex), "-input"),
2098
- id: "".concat(getAutocompleteId(props.id, sourceIndex), "-label")
2117
+ htmlFor: getAutocompleteElementId(props.id, 'input'),
2118
+ id: getAutocompleteElementId(props.id, 'label')
2099
2119
  }, rest);
2100
2120
  };
2101
2121
  var getListProps = function getListProps(providedProps) {
2102
- var _ref4 = providedProps || {},
2103
- sourceIndex = _ref4.sourceIndex,
2104
- rest = _objectWithoutProperties$1(_ref4, _excluded6);
2122
+ var _ref5 = providedProps || {},
2123
+ source = _ref5.source,
2124
+ rest = _objectWithoutProperties$1(_ref5, _excluded5);
2105
2125
  return _objectSpread2({
2106
2126
  role: 'listbox',
2107
- 'aria-labelledby': "".concat(getAutocompleteId(props.id, sourceIndex), "-label"),
2108
- id: "".concat(getAutocompleteId(props.id, sourceIndex), "-list")
2127
+ 'aria-labelledby': getAutocompleteElementId(props.id, 'label'),
2128
+ id: getAutocompleteElementId(props.id, 'list', source)
2109
2129
  }, rest);
2110
2130
  };
2111
2131
  var getPanelProps = function getPanelProps(rest) {
@@ -2124,10 +2144,9 @@
2124
2144
  var getItemProps = function getItemProps(providedProps) {
2125
2145
  var item = providedProps.item,
2126
2146
  source = providedProps.source,
2127
- sourceIndex = providedProps.sourceIndex,
2128
- rest = _objectWithoutProperties$1(providedProps, _excluded7);
2147
+ rest = _objectWithoutProperties$1(providedProps, _excluded6);
2129
2148
  return _objectSpread2({
2130
- id: "".concat(getAutocompleteId(props.id, sourceIndex), "-item-").concat(item.__autocomplete_id),
2149
+ id: getAutocompleteElementId(props.id, "item-".concat(item.__autocomplete_id), source),
2131
2150
  role: 'option',
2132
2151
  'aria-selected': store.getState().activeItemId === item.__autocomplete_id,
2133
2152
  onMouseMove: function onMouseMove(event) {