@algolia/autocomplete-core 1.7.4 → 1.8.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.
- package/dist/esm/createAutocomplete.js +13 -3
- package/dist/esm/getDefaultProps.js +8 -0
- package/dist/esm/onInput.js +2 -2
- package/dist/esm/reshape.js +15 -2
- package/dist/esm/resolve.d.ts +3 -3
- package/dist/esm/resolve.js +22 -9
- package/dist/esm/types/AutocompleteApi.d.ts +5 -0
- package/dist/esm/types/AutocompletePlugin.d.ts +9 -1
- package/dist/esm/types/AutocompleteReshape.d.ts +4 -2
- package/dist/esm/types/AutocompleteSource.d.ts +25 -7
- package/dist/esm/types/AutocompleteSubscribers.d.ts +2 -1
- package/dist/esm/utils/getNormalizedSources.js +9 -4
- package/dist/umd/index.development.js +65 -16
- package/dist/umd/index.development.js.map +1 -1
- package/dist/umd/index.production.js +2 -2
- package/dist/umd/index.production.js.map +1 -1
- package/package.json +5 -5
|
@@ -23,7 +23,8 @@ export function createAutocomplete(options) {
|
|
|
23
23
|
var propGetters = getPropGetters(_objectSpread({
|
|
24
24
|
props: props,
|
|
25
25
|
refresh: refresh,
|
|
26
|
-
store: store
|
|
26
|
+
store: store,
|
|
27
|
+
navigator: props.navigator
|
|
27
28
|
}, setters));
|
|
28
29
|
|
|
29
30
|
function onStoreStateChange(_ref) {
|
|
@@ -32,7 +33,8 @@ export function createAutocomplete(options) {
|
|
|
32
33
|
props.onStateChange(_objectSpread({
|
|
33
34
|
prevState: prevState,
|
|
34
35
|
state: state,
|
|
35
|
-
refresh: refresh
|
|
36
|
+
refresh: refresh,
|
|
37
|
+
navigator: props.navigator
|
|
36
38
|
}, setters));
|
|
37
39
|
}
|
|
38
40
|
|
|
@@ -43,6 +45,7 @@ export function createAutocomplete(options) {
|
|
|
43
45
|
isOpen: store.getState().isOpen
|
|
44
46
|
},
|
|
45
47
|
props: props,
|
|
48
|
+
navigator: props.navigator,
|
|
46
49
|
query: store.getState().query,
|
|
47
50
|
refresh: refresh,
|
|
48
51
|
store: store
|
|
@@ -53,6 +56,7 @@ export function createAutocomplete(options) {
|
|
|
53
56
|
var _plugin$subscribe;
|
|
54
57
|
|
|
55
58
|
return (_plugin$subscribe = plugin.subscribe) === null || _plugin$subscribe === void 0 ? void 0 : _plugin$subscribe.call(plugin, _objectSpread(_objectSpread({}, setters), {}, {
|
|
59
|
+
navigator: props.navigator,
|
|
56
60
|
refresh: refresh,
|
|
57
61
|
onSelect: function onSelect(fn) {
|
|
58
62
|
subscribers.push({
|
|
@@ -63,6 +67,11 @@ export function createAutocomplete(options) {
|
|
|
63
67
|
subscribers.push({
|
|
64
68
|
onActive: fn
|
|
65
69
|
});
|
|
70
|
+
},
|
|
71
|
+
onResolve: function onResolve(fn) {
|
|
72
|
+
subscribers.push({
|
|
73
|
+
onResolve: fn
|
|
74
|
+
});
|
|
66
75
|
}
|
|
67
76
|
}));
|
|
68
77
|
});
|
|
@@ -74,6 +83,7 @@ export function createAutocomplete(options) {
|
|
|
74
83
|
environment: props.environment
|
|
75
84
|
});
|
|
76
85
|
return _objectSpread(_objectSpread({
|
|
77
|
-
refresh: refresh
|
|
86
|
+
refresh: refresh,
|
|
87
|
+
navigator: props.navigator
|
|
78
88
|
}, propGetters), setters);
|
|
79
89
|
}
|
|
@@ -112,6 +112,14 @@ export function getDefaultProps(props, pluginSubscribers) {
|
|
|
112
112
|
|
|
113
113
|
return (_x$onActive = x.onActive) === null || _x$onActive === void 0 ? void 0 : _x$onActive.call(x, params);
|
|
114
114
|
});
|
|
115
|
+
},
|
|
116
|
+
onResolve: function onResolve(params) {
|
|
117
|
+
source.onResolve(params);
|
|
118
|
+
pluginSubscribers.forEach(function (x) {
|
|
119
|
+
var _x$onResolve;
|
|
120
|
+
|
|
121
|
+
return (_x$onResolve = x.onResolve) === null || _x$onResolve === void 0 ? void 0 : _x$onResolve.call(x, params);
|
|
122
|
+
});
|
|
115
123
|
}
|
|
116
124
|
});
|
|
117
125
|
});
|
package/dist/esm/onInput.js
CHANGED
|
@@ -82,10 +82,10 @@ export function onInput(_ref) {
|
|
|
82
82
|
refresh: refresh,
|
|
83
83
|
state: store.getState()
|
|
84
84
|
}, setters))).then(function (itemsOrDescription) {
|
|
85
|
-
return preResolve(itemsOrDescription, source.sourceId);
|
|
85
|
+
return preResolve(itemsOrDescription, source.sourceId, store.getState());
|
|
86
86
|
});
|
|
87
87
|
})).then(resolve).then(function (responses) {
|
|
88
|
-
return postResolve(responses, sources);
|
|
88
|
+
return postResolve(responses, sources, store);
|
|
89
89
|
}).then(function (collections) {
|
|
90
90
|
return reshape({
|
|
91
91
|
collections: collections,
|
package/dist/esm/reshape.js
CHANGED
|
@@ -11,7 +11,7 @@ export function reshape(_ref) {
|
|
|
11
11
|
state = _ref.state;
|
|
12
12
|
// Sources are grouped by `sourceId` to conveniently pick them via destructuring.
|
|
13
13
|
// Example: `const { recentSearchesPlugin } = sourcesBySourceId`
|
|
14
|
-
var
|
|
14
|
+
var originalSourcesBySourceId = collections.reduce(function (acc, collection) {
|
|
15
15
|
return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, collection.source.sourceId, _objectSpread(_objectSpread({}, collection.source), {}, {
|
|
16
16
|
getItems: function getItems() {
|
|
17
17
|
// We provide the resolved items from the collection to the `reshape` prop.
|
|
@@ -19,9 +19,22 @@ export function reshape(_ref) {
|
|
|
19
19
|
}
|
|
20
20
|
})));
|
|
21
21
|
}, {});
|
|
22
|
+
|
|
23
|
+
var _props$plugins$reduce = props.plugins.reduce(function (acc, plugin) {
|
|
24
|
+
if (plugin.reshape) {
|
|
25
|
+
return plugin.reshape(acc);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return acc;
|
|
29
|
+
}, {
|
|
30
|
+
sourcesBySourceId: originalSourcesBySourceId,
|
|
31
|
+
state: state
|
|
32
|
+
}),
|
|
33
|
+
sourcesBySourceId = _props$plugins$reduce.sourcesBySourceId;
|
|
34
|
+
|
|
22
35
|
var reshapeSources = props.reshape({
|
|
23
|
-
sources: Object.values(sourcesBySourceId),
|
|
24
36
|
sourcesBySourceId: sourcesBySourceId,
|
|
37
|
+
sources: Object.values(sourcesBySourceId),
|
|
25
38
|
state: state
|
|
26
39
|
}); // We reconstruct the collections with the items modified by the `reshape` prop.
|
|
27
40
|
|
package/dist/esm/resolve.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ExecuteResponse, RequesterDescription, TransformResponse } from '@algolia/autocomplete-preset-algolia';
|
|
2
2
|
import { MultipleQueriesQuery, SearchForFacetValuesResponse, SearchResponse } from '@algolia/client-search';
|
|
3
|
-
import { BaseItem, InternalAutocompleteSource } from './types';
|
|
3
|
+
import { AutocompleteState, AutocompleteStore, BaseItem, InternalAutocompleteSource } from './types';
|
|
4
4
|
declare type RequestDescriptionPreResolved<TItem extends BaseItem> = Pick<RequesterDescription<TItem>, 'execute' | 'requesterId' | 'searchClient' | 'transformResponse'> & {
|
|
5
5
|
requests: Array<{
|
|
6
6
|
query: MultipleQueriesQuery;
|
|
@@ -13,13 +13,13 @@ declare type RequestDescriptionPreResolvedCustom<TItem extends BaseItem> = {
|
|
|
13
13
|
sourceId: string;
|
|
14
14
|
transformResponse?: undefined;
|
|
15
15
|
};
|
|
16
|
-
export declare function preResolve<TItem extends BaseItem>(itemsOrDescription: TItem[] | TItem[][] | RequesterDescription<TItem>, sourceId: string): RequestDescriptionPreResolved<TItem> | RequestDescriptionPreResolvedCustom<TItem>;
|
|
16
|
+
export declare function preResolve<TItem extends BaseItem>(itemsOrDescription: TItem[] | TItem[][] | RequesterDescription<TItem>, sourceId: string, state: AutocompleteState<TItem>): RequestDescriptionPreResolved<TItem> | RequestDescriptionPreResolvedCustom<TItem>;
|
|
17
17
|
export declare function resolve<TItem extends BaseItem>(items: Array<RequestDescriptionPreResolved<TItem> | RequestDescriptionPreResolvedCustom<TItem>>): Promise<(RequestDescriptionPreResolvedCustom<TItem> | {
|
|
18
18
|
items: SearchForFacetValuesResponse | SearchResponse<TItem>;
|
|
19
19
|
sourceId: string;
|
|
20
20
|
transformResponse: TransformResponse<TItem>;
|
|
21
21
|
})[]>;
|
|
22
|
-
export declare function postResolve<TItem extends BaseItem>(responses: Array<RequestDescriptionPreResolvedCustom<TItem> | ExecuteResponse<TItem>[0]>, sources: Array<InternalAutocompleteSource<TItem
|
|
22
|
+
export declare function postResolve<TItem extends BaseItem>(responses: Array<RequestDescriptionPreResolvedCustom<TItem> | ExecuteResponse<TItem>[0]>, sources: Array<InternalAutocompleteSource<TItem>>, store: AutocompleteStore<TItem>): {
|
|
23
23
|
source: InternalAutocompleteSource<TItem>;
|
|
24
24
|
items: {
|
|
25
25
|
label: string;
|
package/dist/esm/resolve.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
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); }
|
|
2
2
|
|
|
3
|
+
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; }
|
|
4
|
+
|
|
5
|
+
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; }
|
|
6
|
+
|
|
7
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
+
|
|
3
9
|
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
4
10
|
|
|
5
11
|
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."); }
|
|
@@ -12,12 +18,6 @@ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToAr
|
|
|
12
18
|
|
|
13
19
|
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; }
|
|
14
20
|
|
|
15
|
-
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; }
|
|
16
|
-
|
|
17
|
-
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; }
|
|
18
|
-
|
|
19
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
20
|
-
|
|
21
21
|
import { decycle, flatten, invariant } from '@algolia/autocomplete-shared';
|
|
22
22
|
import { mapToAlgoliaResponse } from './utils';
|
|
23
23
|
|
|
@@ -29,12 +29,19 @@ function isRequesterDescription(description) {
|
|
|
29
29
|
return Boolean(description === null || description === void 0 ? void 0 : description.execute);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
export function preResolve(itemsOrDescription, sourceId) {
|
|
32
|
+
export function preResolve(itemsOrDescription, sourceId, state) {
|
|
33
33
|
if (isRequesterDescription(itemsOrDescription)) {
|
|
34
|
+
var contextParameters = itemsOrDescription.requesterId === 'algolia' ? Object.assign.apply(Object, [{}].concat(_toConsumableArray(Object.keys(state.context).map(function (key) {
|
|
35
|
+
var _state$context$key;
|
|
36
|
+
|
|
37
|
+
return (_state$context$key = state.context[key]) === null || _state$context$key === void 0 ? void 0 : _state$context$key.__algoliaSearchParameters;
|
|
38
|
+
})))) : {};
|
|
34
39
|
return _objectSpread(_objectSpread({}, itemsOrDescription), {}, {
|
|
35
40
|
requests: itemsOrDescription.queries.map(function (query) {
|
|
36
41
|
return {
|
|
37
|
-
query: query,
|
|
42
|
+
query: itemsOrDescription.requesterId === 'algolia' ? _objectSpread(_objectSpread({}, query), {}, {
|
|
43
|
+
params: _objectSpread(_objectSpread({}, contextParameters), query.params)
|
|
44
|
+
}) : query,
|
|
38
45
|
sourceId: sourceId,
|
|
39
46
|
transformResponse: itemsOrDescription.transformResponse
|
|
40
47
|
};
|
|
@@ -96,7 +103,7 @@ export function resolve(items) {
|
|
|
96
103
|
return flatten(responses);
|
|
97
104
|
});
|
|
98
105
|
}
|
|
99
|
-
export function postResolve(responses, sources) {
|
|
106
|
+
export function postResolve(responses, sources, store) {
|
|
100
107
|
return sources.map(function (source) {
|
|
101
108
|
var matches = responses.filter(function (response) {
|
|
102
109
|
return response.sourceId === source.sourceId;
|
|
@@ -107,6 +114,12 @@ export function postResolve(responses, sources) {
|
|
|
107
114
|
});
|
|
108
115
|
var transform = matches[0].transformResponse;
|
|
109
116
|
var items = transform ? transform(mapToAlgoliaResponse(results)) : results;
|
|
117
|
+
source.onResolve({
|
|
118
|
+
source: source,
|
|
119
|
+
results: results,
|
|
120
|
+
items: items,
|
|
121
|
+
state: store.getState()
|
|
122
|
+
});
|
|
110
123
|
invariant(Array.isArray(items), function () {
|
|
111
124
|
return "The `getItems` function from source \"".concat(source.sourceId, "\" must return an array of items but returned type ").concat(JSON.stringify(_typeof(items)), ":\n\n").concat(JSON.stringify(decycle(items), null, 2), ".\n\nSee: https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/sources/#param-getitems");
|
|
112
125
|
});
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AutocompleteNavigator } from './AutocompleteNavigator';
|
|
1
2
|
import { AutocompletePropGetters } from './AutocompletePropGetters';
|
|
2
3
|
import { AutocompleteSetters } from './AutocompleteSetters';
|
|
3
4
|
export declare type BaseItem = Record<string, unknown>;
|
|
@@ -6,5 +7,9 @@ export interface AutocompleteScopeApi<TItem extends BaseItem> extends Autocomple
|
|
|
6
7
|
* Triggers a search to refresh the state.
|
|
7
8
|
*/
|
|
8
9
|
refresh(): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Functions to navigate to a URL.
|
|
12
|
+
*/
|
|
13
|
+
navigator: AutocompleteNavigator<TItem>;
|
|
9
14
|
}
|
|
10
15
|
export declare type AutocompleteApi<TItem extends BaseItem, TEvent = Event, TMouseEvent = MouseEvent, TKeyboardEvent = KeyboardEvent> = AutocompleteScopeApi<TItem> & AutocompletePropGetters<TItem, TEvent, TMouseEvent, TKeyboardEvent>;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { AutocompleteScopeApi, BaseItem } from './AutocompleteApi';
|
|
2
2
|
import { AutocompleteOptions } from './AutocompleteOptions';
|
|
3
|
-
import {
|
|
3
|
+
import { PluginReshape } from './AutocompleteReshape';
|
|
4
|
+
import { OnSelectParams, OnActiveParams, OnResolveParams } from './AutocompleteSource';
|
|
4
5
|
declare type PluginSubscriber<TParams> = (params: TParams) => void;
|
|
5
6
|
export interface PluginSubscribeParams<TItem extends BaseItem> extends AutocompleteScopeApi<TItem> {
|
|
6
7
|
onSelect(fn: PluginSubscriber<OnSelectParams<TItem>>): void;
|
|
7
8
|
onActive(fn: PluginSubscriber<OnActiveParams<TItem>>): void;
|
|
9
|
+
onResolve(fn: PluginSubscriber<OnResolveParams<TItem>>): void;
|
|
8
10
|
}
|
|
9
11
|
export declare type AutocompletePlugin<TItem extends BaseItem, TData = unknown> = Partial<Pick<AutocompleteOptions<any>, 'onStateChange' | 'onSubmit' | 'onReset'> & Pick<AutocompleteOptions<TItem>, 'getSources'>> & {
|
|
10
12
|
/**
|
|
@@ -27,6 +29,12 @@ export declare type AutocompletePlugin<TItem extends BaseItem, TData = unknown>
|
|
|
27
29
|
* @link https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/plugins/#param-name
|
|
28
30
|
*/
|
|
29
31
|
name?: string;
|
|
32
|
+
/**
|
|
33
|
+
* A function to reshape the sources.
|
|
34
|
+
*
|
|
35
|
+
* It gets called before the user's reshape function.
|
|
36
|
+
*/
|
|
37
|
+
reshape?: PluginReshape<TItem>;
|
|
30
38
|
/**
|
|
31
39
|
* @internal
|
|
32
40
|
*/
|
|
@@ -5,7 +5,7 @@ export declare type AutocompleteReshapeSource<TItem extends BaseItem> = Autocomp
|
|
|
5
5
|
getItems(): TItem[];
|
|
6
6
|
};
|
|
7
7
|
export declare type AutocompleteReshapeSourcesBySourceId<TItem extends BaseItem> = Record<string, AutocompleteReshapeSource<TItem>>;
|
|
8
|
-
export declare type
|
|
8
|
+
export declare type ReshapeParams<TItem extends BaseItem, TState extends AutocompleteState<TItem> = AutocompleteState<TItem>> = {
|
|
9
9
|
/**
|
|
10
10
|
* The resolved sources provided by [`getSources`](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/sources/#param-getsources)
|
|
11
11
|
*/
|
|
@@ -20,4 +20,6 @@ export declare type Reshape<TItem extends BaseItem, TState extends AutocompleteS
|
|
|
20
20
|
* @link https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/state
|
|
21
21
|
*/
|
|
22
22
|
state: TState;
|
|
23
|
-
}
|
|
23
|
+
};
|
|
24
|
+
export declare type Reshape<TItem extends BaseItem, TState extends AutocompleteState<TItem> = AutocompleteState<TItem>> = (params: ReshapeParams<TItem, TState>) => Array<AutocompleteReshapeSource<TItem>>;
|
|
25
|
+
export declare type PluginReshape<TItem extends BaseItem, TState extends AutocompleteState<TItem> = AutocompleteState<TItem>> = (params: Omit<ReshapeParams<TItem, TState>, 'sources'>) => Omit<ReshapeParams<TItem, TState>, 'sources'>;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { RequesterDescription } from '@algolia/autocomplete-preset-algolia';
|
|
1
|
+
import type { SearchForFacetValuesResponse, SearchResponse, RequesterDescription } from '@algolia/autocomplete-preset-algolia';
|
|
2
2
|
import type { MaybePromise } from '@algolia/autocomplete-shared';
|
|
3
|
+
import { FacetHit, Hit } from '@algolia/client-search';
|
|
3
4
|
import { AutocompleteScopeApi, BaseItem } from './AutocompleteApi';
|
|
4
5
|
import { GetSourcesParams } from './AutocompleteOptions';
|
|
5
6
|
import { AutocompleteState } from './AutocompleteState';
|
|
@@ -12,6 +13,18 @@ export interface OnSelectParams<TItem extends BaseItem> extends AutocompleteScop
|
|
|
12
13
|
source: InternalAutocompleteSource<TItem>;
|
|
13
14
|
}
|
|
14
15
|
export declare type OnActiveParams<TItem extends BaseItem> = OnSelectParams<TItem>;
|
|
16
|
+
export declare type OnResolveParams<TItem extends BaseItem> = {
|
|
17
|
+
source: AutocompleteSource<TItem>;
|
|
18
|
+
results: SearchForFacetValuesResponse | SearchResponse<TItem> | TItem[] | TItem[][];
|
|
19
|
+
items: FacetHit[][] | FacetHit[] | Array<Hit<TItem>> | Array<SearchForFacetValuesResponse | SearchResponse<TItem> | TItem[] | TItem[][]>;
|
|
20
|
+
state: AutocompleteState<TItem>;
|
|
21
|
+
};
|
|
22
|
+
declare type DefaultIndicator = {
|
|
23
|
+
/**
|
|
24
|
+
* Optional key on a function to indicate it's the default value of this function.
|
|
25
|
+
*/
|
|
26
|
+
__default?: boolean;
|
|
27
|
+
};
|
|
15
28
|
export interface AutocompleteSource<TItem extends BaseItem> {
|
|
16
29
|
/**
|
|
17
30
|
* Unique identifier for the source.
|
|
@@ -22,19 +35,19 @@ export interface AutocompleteSource<TItem extends BaseItem> {
|
|
|
22
35
|
*
|
|
23
36
|
* The value is used to fill the search box.
|
|
24
37
|
*/
|
|
25
|
-
getItemInputValue
|
|
38
|
+
getItemInputValue?: DefaultIndicator & (({ item, state, }: {
|
|
26
39
|
item: TItem;
|
|
27
40
|
state: AutocompleteState<TItem>;
|
|
28
|
-
})
|
|
41
|
+
}) => string);
|
|
29
42
|
/**
|
|
30
43
|
* The function called to get the URL of the item.
|
|
31
44
|
*
|
|
32
45
|
* The value is used to add [keyboard accessibility](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/keyboard-navigation/) features to let users open items in the current tab, a new tab, or a new window.
|
|
33
46
|
*/
|
|
34
|
-
getItemUrl
|
|
47
|
+
getItemUrl?: DefaultIndicator & (({ item, state, }: {
|
|
35
48
|
item: TItem;
|
|
36
49
|
state: AutocompleteState<TItem>;
|
|
37
|
-
})
|
|
50
|
+
}) => string | undefined);
|
|
38
51
|
/**
|
|
39
52
|
* The function called when the input changes.
|
|
40
53
|
*
|
|
@@ -44,14 +57,19 @@ export interface AutocompleteSource<TItem extends BaseItem> {
|
|
|
44
57
|
/**
|
|
45
58
|
* The function called whenever an item is selected.
|
|
46
59
|
*/
|
|
47
|
-
onSelect
|
|
60
|
+
onSelect?: DefaultIndicator & ((params: OnSelectParams<TItem>) => void);
|
|
48
61
|
/**
|
|
49
62
|
* The function called whenever an item is active.
|
|
50
63
|
*
|
|
51
64
|
* You can trigger different behaviors if the item is active depending on the triggering event using the `event` parameter.
|
|
52
65
|
*/
|
|
53
|
-
onActive
|
|
66
|
+
onActive?: DefaultIndicator & ((params: OnActiveParams<TItem>) => void);
|
|
67
|
+
/**
|
|
68
|
+
* The function called whenever a source resolves.
|
|
69
|
+
*/
|
|
70
|
+
onResolve?: DefaultIndicator & ((params: OnResolveParams<TItem>) => void);
|
|
54
71
|
}
|
|
55
72
|
export declare type InternalAutocompleteSource<TItem extends BaseItem> = {
|
|
56
73
|
[KParam in keyof AutocompleteSource<TItem>]-?: AutocompleteSource<TItem>[KParam];
|
|
57
74
|
};
|
|
75
|
+
export {};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { BaseItem } from './AutocompleteApi';
|
|
2
|
-
import { OnActiveParams, OnSelectParams } from './AutocompleteSource';
|
|
2
|
+
import { OnActiveParams, OnResolveParams, OnSelectParams } from './AutocompleteSource';
|
|
3
3
|
export declare type AutocompleteSubscriber<TItem extends BaseItem> = {
|
|
4
4
|
onSelect(params: OnSelectParams<TItem>): void;
|
|
5
5
|
onActive(params: OnActiveParams<TItem>): void;
|
|
6
|
+
onResolve(params: OnResolveParams<TItem>): void;
|
|
6
7
|
};
|
|
7
8
|
export declare type AutocompleteSubscribers<TItem extends BaseItem> = Array<Partial<AutocompleteSubscriber<TItem>>>;
|
|
@@ -26,8 +26,7 @@ export function getNormalizedSources(getSources, params) {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
seenSourceIds.push(source.sourceId);
|
|
29
|
-
|
|
30
|
-
var normalizedSource = _objectSpread({
|
|
29
|
+
var defaultSource = {
|
|
31
30
|
getItemInputValue: function getItemInputValue(_ref) {
|
|
32
31
|
var state = _ref.state;
|
|
33
32
|
return state.query;
|
|
@@ -39,8 +38,14 @@ export function getNormalizedSources(getSources, params) {
|
|
|
39
38
|
var setIsOpen = _ref2.setIsOpen;
|
|
40
39
|
setIsOpen(false);
|
|
41
40
|
},
|
|
42
|
-
onActive: noop
|
|
43
|
-
|
|
41
|
+
onActive: noop,
|
|
42
|
+
onResolve: noop
|
|
43
|
+
};
|
|
44
|
+
Object.keys(defaultSource).forEach(function (key) {
|
|
45
|
+
defaultSource[key].__default = true;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
var normalizedSource = _objectSpread(_objectSpread({}, defaultSource), source);
|
|
44
49
|
|
|
45
50
|
return Promise.resolve(normalizedSource);
|
|
46
51
|
}));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! @algolia/autocomplete-core 1.
|
|
1
|
+
/*! @algolia/autocomplete-core 1.8.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) :
|
|
@@ -264,7 +264,7 @@
|
|
|
264
264
|
|
|
265
265
|
var noop = function noop() {};
|
|
266
266
|
|
|
267
|
-
var version = '1.
|
|
267
|
+
var version = '1.8.1';
|
|
268
268
|
|
|
269
269
|
var userAgents = [{
|
|
270
270
|
segment: 'autocomplete-core',
|
|
@@ -466,8 +466,7 @@
|
|
|
466
466
|
}
|
|
467
467
|
|
|
468
468
|
seenSourceIds.push(source.sourceId);
|
|
469
|
-
|
|
470
|
-
var normalizedSource = _objectSpread2({
|
|
469
|
+
var defaultSource = {
|
|
471
470
|
getItemInputValue: function getItemInputValue(_ref) {
|
|
472
471
|
var state = _ref.state;
|
|
473
472
|
return state.query;
|
|
@@ -479,8 +478,14 @@
|
|
|
479
478
|
var setIsOpen = _ref2.setIsOpen;
|
|
480
479
|
setIsOpen(false);
|
|
481
480
|
},
|
|
482
|
-
onActive: noop
|
|
483
|
-
|
|
481
|
+
onActive: noop,
|
|
482
|
+
onResolve: noop
|
|
483
|
+
};
|
|
484
|
+
Object.keys(defaultSource).forEach(function (key) {
|
|
485
|
+
defaultSource[key].__default = true;
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
var normalizedSource = _objectSpread2(_objectSpread2({}, defaultSource), source);
|
|
484
489
|
|
|
485
490
|
return Promise.resolve(normalizedSource);
|
|
486
491
|
}));
|
|
@@ -786,6 +791,14 @@
|
|
|
786
791
|
|
|
787
792
|
return (_x$onActive = x.onActive) === null || _x$onActive === void 0 ? void 0 : _x$onActive.call(x, params);
|
|
788
793
|
});
|
|
794
|
+
},
|
|
795
|
+
onResolve: function onResolve(params) {
|
|
796
|
+
source.onResolve(params);
|
|
797
|
+
pluginSubscribers.forEach(function (x) {
|
|
798
|
+
var _x$onResolve;
|
|
799
|
+
|
|
800
|
+
return (_x$onResolve = x.onResolve) === null || _x$onResolve === void 0 ? void 0 : _x$onResolve.call(x, params);
|
|
801
|
+
});
|
|
789
802
|
}
|
|
790
803
|
});
|
|
791
804
|
});
|
|
@@ -815,7 +828,7 @@
|
|
|
815
828
|
state = _ref.state;
|
|
816
829
|
// Sources are grouped by `sourceId` to conveniently pick them via destructuring.
|
|
817
830
|
// Example: `const { recentSearchesPlugin } = sourcesBySourceId`
|
|
818
|
-
var
|
|
831
|
+
var originalSourcesBySourceId = collections.reduce(function (acc, collection) {
|
|
819
832
|
return _objectSpread2(_objectSpread2({}, acc), {}, _defineProperty({}, collection.source.sourceId, _objectSpread2(_objectSpread2({}, collection.source), {}, {
|
|
820
833
|
getItems: function getItems() {
|
|
821
834
|
// We provide the resolved items from the collection to the `reshape` prop.
|
|
@@ -823,9 +836,22 @@
|
|
|
823
836
|
}
|
|
824
837
|
})));
|
|
825
838
|
}, {});
|
|
839
|
+
|
|
840
|
+
var _props$plugins$reduce = props.plugins.reduce(function (acc, plugin) {
|
|
841
|
+
if (plugin.reshape) {
|
|
842
|
+
return plugin.reshape(acc);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
return acc;
|
|
846
|
+
}, {
|
|
847
|
+
sourcesBySourceId: originalSourcesBySourceId,
|
|
848
|
+
state: state
|
|
849
|
+
}),
|
|
850
|
+
sourcesBySourceId = _props$plugins$reduce.sourcesBySourceId;
|
|
851
|
+
|
|
826
852
|
var reshapeSources = props.reshape({
|
|
827
|
-
sources: Object.values(sourcesBySourceId),
|
|
828
853
|
sourcesBySourceId: sourcesBySourceId,
|
|
854
|
+
sources: Object.values(sourcesBySourceId),
|
|
829
855
|
state: state
|
|
830
856
|
}); // We reconstruct the collections with the items modified by the `reshape` prop.
|
|
831
857
|
|
|
@@ -845,12 +871,19 @@
|
|
|
845
871
|
return Boolean(description === null || description === void 0 ? void 0 : description.execute);
|
|
846
872
|
}
|
|
847
873
|
|
|
848
|
-
function preResolve(itemsOrDescription, sourceId) {
|
|
874
|
+
function preResolve(itemsOrDescription, sourceId, state) {
|
|
849
875
|
if (isRequesterDescription(itemsOrDescription)) {
|
|
876
|
+
var contextParameters = itemsOrDescription.requesterId === 'algolia' ? Object.assign.apply(Object, [{}].concat(_toConsumableArray(Object.keys(state.context).map(function (key) {
|
|
877
|
+
var _state$context$key;
|
|
878
|
+
|
|
879
|
+
return (_state$context$key = state.context[key]) === null || _state$context$key === void 0 ? void 0 : _state$context$key.__algoliaSearchParameters;
|
|
880
|
+
})))) : {};
|
|
850
881
|
return _objectSpread2(_objectSpread2({}, itemsOrDescription), {}, {
|
|
851
882
|
requests: itemsOrDescription.queries.map(function (query) {
|
|
852
883
|
return {
|
|
853
|
-
query: query,
|
|
884
|
+
query: itemsOrDescription.requesterId === 'algolia' ? _objectSpread2(_objectSpread2({}, query), {}, {
|
|
885
|
+
params: _objectSpread2(_objectSpread2({}, contextParameters), query.params)
|
|
886
|
+
}) : query,
|
|
854
887
|
sourceId: sourceId,
|
|
855
888
|
transformResponse: itemsOrDescription.transformResponse
|
|
856
889
|
};
|
|
@@ -912,7 +945,7 @@
|
|
|
912
945
|
return flatten(responses);
|
|
913
946
|
});
|
|
914
947
|
}
|
|
915
|
-
function postResolve(responses, sources) {
|
|
948
|
+
function postResolve(responses, sources, store) {
|
|
916
949
|
return sources.map(function (source) {
|
|
917
950
|
var matches = responses.filter(function (response) {
|
|
918
951
|
return response.sourceId === source.sourceId;
|
|
@@ -923,6 +956,12 @@
|
|
|
923
956
|
});
|
|
924
957
|
var transform = matches[0].transformResponse;
|
|
925
958
|
var items = transform ? transform(mapToAlgoliaResponse(results)) : results;
|
|
959
|
+
source.onResolve({
|
|
960
|
+
source: source,
|
|
961
|
+
results: results,
|
|
962
|
+
items: items,
|
|
963
|
+
state: store.getState()
|
|
964
|
+
});
|
|
926
965
|
invariant(Array.isArray(items), function () {
|
|
927
966
|
return "The `getItems` function from source \"".concat(source.sourceId, "\" must return an array of items but returned type ").concat(JSON.stringify(_typeof$1(items)), ":\n\n").concat(JSON.stringify(decycle(items), null, 2), ".\n\nSee: https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/sources/#param-getitems");
|
|
928
967
|
});
|
|
@@ -1004,10 +1043,10 @@
|
|
|
1004
1043
|
refresh: refresh,
|
|
1005
1044
|
state: store.getState()
|
|
1006
1045
|
}, setters))).then(function (itemsOrDescription) {
|
|
1007
|
-
return preResolve(itemsOrDescription, source.sourceId);
|
|
1046
|
+
return preResolve(itemsOrDescription, source.sourceId, store.getState());
|
|
1008
1047
|
});
|
|
1009
1048
|
})).then(resolve).then(function (responses) {
|
|
1010
|
-
return postResolve(responses, sources);
|
|
1049
|
+
return postResolve(responses, sources, store);
|
|
1011
1050
|
}).then(function (collections) {
|
|
1012
1051
|
return reshape({
|
|
1013
1052
|
collections: collections,
|
|
@@ -1781,7 +1820,8 @@
|
|
|
1781
1820
|
var propGetters = getPropGetters(_objectSpread2({
|
|
1782
1821
|
props: props,
|
|
1783
1822
|
refresh: refresh,
|
|
1784
|
-
store: store
|
|
1823
|
+
store: store,
|
|
1824
|
+
navigator: props.navigator
|
|
1785
1825
|
}, setters));
|
|
1786
1826
|
|
|
1787
1827
|
function onStoreStateChange(_ref) {
|
|
@@ -1790,7 +1830,8 @@
|
|
|
1790
1830
|
props.onStateChange(_objectSpread2({
|
|
1791
1831
|
prevState: prevState,
|
|
1792
1832
|
state: state,
|
|
1793
|
-
refresh: refresh
|
|
1833
|
+
refresh: refresh,
|
|
1834
|
+
navigator: props.navigator
|
|
1794
1835
|
}, setters));
|
|
1795
1836
|
}
|
|
1796
1837
|
|
|
@@ -1801,6 +1842,7 @@
|
|
|
1801
1842
|
isOpen: store.getState().isOpen
|
|
1802
1843
|
},
|
|
1803
1844
|
props: props,
|
|
1845
|
+
navigator: props.navigator,
|
|
1804
1846
|
query: store.getState().query,
|
|
1805
1847
|
refresh: refresh,
|
|
1806
1848
|
store: store
|
|
@@ -1811,6 +1853,7 @@
|
|
|
1811
1853
|
var _plugin$subscribe;
|
|
1812
1854
|
|
|
1813
1855
|
return (_plugin$subscribe = plugin.subscribe) === null || _plugin$subscribe === void 0 ? void 0 : _plugin$subscribe.call(plugin, _objectSpread2(_objectSpread2({}, setters), {}, {
|
|
1856
|
+
navigator: props.navigator,
|
|
1814
1857
|
refresh: refresh,
|
|
1815
1858
|
onSelect: function onSelect(fn) {
|
|
1816
1859
|
subscribers.push({
|
|
@@ -1821,6 +1864,11 @@
|
|
|
1821
1864
|
subscribers.push({
|
|
1822
1865
|
onActive: fn
|
|
1823
1866
|
});
|
|
1867
|
+
},
|
|
1868
|
+
onResolve: function onResolve(fn) {
|
|
1869
|
+
subscribers.push({
|
|
1870
|
+
onResolve: fn
|
|
1871
|
+
});
|
|
1824
1872
|
}
|
|
1825
1873
|
}));
|
|
1826
1874
|
});
|
|
@@ -1832,7 +1880,8 @@
|
|
|
1832
1880
|
environment: props.environment
|
|
1833
1881
|
});
|
|
1834
1882
|
return _objectSpread2(_objectSpread2({
|
|
1835
|
-
refresh: refresh
|
|
1883
|
+
refresh: refresh,
|
|
1884
|
+
navigator: props.navigator
|
|
1836
1885
|
}, propGetters), setters);
|
|
1837
1886
|
}
|
|
1838
1887
|
|