@algolia/autocomplete-core
Version:
Core primitives for building autocomplete experiences.
130 lines • 8.06 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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; }
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; }
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; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { getItemsCount, generateAutocompleteId, flatten } from '@algolia/autocomplete-shared';
import { getNormalizedSources } from './utils';
export function getDefaultProps(props, pluginSubscribers) {
var _props$id;
/* eslint-disable no-restricted-globals */
var environment = typeof window !== 'undefined' ? window : {};
/* eslint-enable no-restricted-globals */
var plugins = props.plugins || [];
return _objectSpread(_objectSpread({
debug: false,
openOnFocus: false,
enterKeyHint: undefined,
ignoreCompositionEvents: false,
placeholder: '',
autoFocus: false,
defaultActiveItemId: null,
stallThreshold: 300,
insights: undefined,
environment: environment,
shouldPanelOpen: function shouldPanelOpen(_ref) {
var state = _ref.state;
return getItemsCount(state) > 0;
},
reshape: function reshape(_ref2) {
var sources = _ref2.sources;
return sources;
}
}, props), {}, {
// Since `generateAutocompleteId` triggers a side effect (it increments
// an internal counter), we don't want to execute it if unnecessary.
id: (_props$id = props.id) !== null && _props$id !== void 0 ? _props$id : generateAutocompleteId(),
plugins: plugins,
// The following props need to be deeply defaulted.
initialState: _objectSpread({
activeItemId: null,
query: '',
completion: null,
collections: [],
isOpen: false,
status: 'idle',
context: {}
}, props.initialState),
onStateChange: function onStateChange(params) {
var _props$onStateChange;
(_props$onStateChange = props.onStateChange) === null || _props$onStateChange === void 0 ? void 0 : _props$onStateChange.call(props, params);
plugins.forEach(function (x) {
var _x$onStateChange;
return (_x$onStateChange = x.onStateChange) === null || _x$onStateChange === void 0 ? void 0 : _x$onStateChange.call(x, params);
});
},
onSubmit: function onSubmit(params) {
var _props$onSubmit;
(_props$onSubmit = props.onSubmit) === null || _props$onSubmit === void 0 ? void 0 : _props$onSubmit.call(props, params);
plugins.forEach(function (x) {
var _x$onSubmit;
return (_x$onSubmit = x.onSubmit) === null || _x$onSubmit === void 0 ? void 0 : _x$onSubmit.call(x, params);
});
},
onReset: function onReset(params) {
var _props$onReset;
(_props$onReset = props.onReset) === null || _props$onReset === void 0 ? void 0 : _props$onReset.call(props, params);
plugins.forEach(function (x) {
var _x$onReset;
return (_x$onReset = x.onReset) === null || _x$onReset === void 0 ? void 0 : _x$onReset.call(x, params);
});
},
getSources: function getSources(params) {
return Promise.all([].concat(_toConsumableArray(plugins.map(function (plugin) {
return plugin.getSources;
})), [props.getSources]).filter(Boolean).map(function (getSources) {
return getNormalizedSources(getSources, params);
})).then(function (nested) {
return flatten(nested);
}).then(function (sources) {
return sources.map(function (source) {
return _objectSpread(_objectSpread({}, source), {}, {
onSelect: function onSelect(params) {
source.onSelect(params);
pluginSubscribers.forEach(function (x) {
var _x$onSelect;
return (_x$onSelect = x.onSelect) === null || _x$onSelect === void 0 ? void 0 : _x$onSelect.call(x, params);
});
},
onActive: function onActive(params) {
source.onActive(params);
pluginSubscribers.forEach(function (x) {
var _x$onActive;
return (_x$onActive = x.onActive) === null || _x$onActive === void 0 ? void 0 : _x$onActive.call(x, params);
});
},
onResolve: function onResolve(params) {
source.onResolve(params);
pluginSubscribers.forEach(function (x) {
var _x$onResolve;
return (_x$onResolve = x.onResolve) === null || _x$onResolve === void 0 ? void 0 : _x$onResolve.call(x, params);
});
}
});
});
});
},
navigator: _objectSpread({
navigate: function navigate(_ref3) {
var itemUrl = _ref3.itemUrl;
environment.location.assign(itemUrl);
},
navigateNewTab: function navigateNewTab(_ref4) {
var itemUrl = _ref4.itemUrl;
var windowReference = environment.open(itemUrl, '_blank', 'noopener');
windowReference === null || windowReference === void 0 ? void 0 : windowReference.focus();
},
navigateNewWindow: function navigateNewWindow(_ref5) {
var itemUrl = _ref5.itemUrl;
environment.open(itemUrl, '_blank', 'noopener');
}
}, props.navigator)
});
}