@constructor-io/constructorio-ui-autocomplete 1.25.5 → 1.27.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.
- package/README.md +24 -0
- package/dist/constructorio-ui-autocomplete-bundled.js +10 -10
- package/lib/cjs/constants.js +4 -2
- package/lib/cjs/hooks/useCioAutocomplete.js +23 -3
- package/lib/cjs/hooks/useCioClient.js +3 -2
- package/lib/cjs/hooks/useNormalizedProps.js +3 -3
- package/lib/cjs/utils/helpers.js +2 -2
- package/lib/cjs/utils/shopifyDefaults.js +48 -0
- package/lib/cjs/version.js +1 -1
- package/lib/mjs/constants.js +3 -1
- package/lib/mjs/hooks/useCioAutocomplete.js +23 -3
- package/lib/mjs/hooks/useCioClient.js +3 -2
- package/lib/mjs/hooks/useNormalizedProps.js +3 -3
- package/lib/mjs/utils/helpers.js +2 -2
- package/lib/mjs/utils/shopifyDefaults.js +44 -0
- package/lib/mjs/version.js +1 -1
- package/lib/types/constants.d.ts +1 -1
- package/lib/types/hooks/useDownShift.d.ts +1 -1
- package/lib/types/hooks/useNormalizedProps.d.ts +1 -1
- package/lib/types/types.d.ts +54 -8
- package/lib/types/utils/helpers.d.ts +1 -1
- package/lib/types/utils/shopifyDefaults.d.ts +13 -0
- package/lib/types/version.d.ts +1 -1
- package/package.json +1 -1
package/lib/cjs/constants.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.displayShowAllResultsButtonDescription = exports.customRenderItemDescription = exports.translationsDescription = exports.fetchZeroStateOnFocusDescription = exports.debounceDescription = exports.termsWithImagesAndCountsDescription = exports.filteredSuggestionsDescription = exports.termsWithGroupSuggestionsDescription = exports.advancedParametersDefaultDescription = exports.advancedParametersDescription = exports.customStylesDescription = exports.multipleSectionsDescription = exports.openOnFocusDescription = exports.zeroStateSectionsDescription = exports.onSubmitDefault = exports.onSubmitDescription = exports.onIsOpenChangeDescription = exports.onChangeDescription = exports.onFocusDescription = exports.customSectionDescription = exports.displayNoResultsMessageDescription = exports.displaySearchTermHighlightsDescription = exports.recommendationsDescription = exports.sectionOrderDescription = exports.numResultsDescription = exports.contentDescription = exports.productsDescription = exports.searchSuggestionsDescription = exports.placeholderDescription = exports.
|
|
3
|
+
exports.displayShowAllResultsButtonDescription = exports.customRenderItemDescription = exports.translationsDescription = exports.fetchZeroStateOnFocusDescription = exports.debounceDescription = exports.termsWithImagesAndCountsDescription = exports.filteredSuggestionsDescription = exports.termsWithGroupSuggestionsDescription = exports.advancedParametersDefaultDescription = exports.advancedParametersDescription = exports.customStylesDescription = exports.multipleSectionsDescription = exports.openOnFocusDescription = exports.zeroStateSectionsDescription = exports.onSubmitDefault = exports.onSubmitDescription = exports.onIsOpenChangeDescription = exports.onChangeDescription = exports.onFocusDescription = exports.customSectionDescription = exports.displayNoResultsMessageDescription = exports.displaySearchTermHighlightsDescription = exports.recommendationsDescription = exports.sectionOrderDescription = exports.numResultsDescription = exports.contentDescription = exports.productsDescription = exports.searchSuggestionsDescription = exports.placeholderDescription = exports.cioClientOptionsDescription = exports.cioJsClientDescription = exports.apiKeyDescription = exports.fullFeaturedAndStyledExampleDescription = exports.zeroStateDescription = exports.userEventsDescription = exports.sectionsDescription = exports.hookDescription = exports.componentDescription = exports.apiKey = void 0;
|
|
4
4
|
// Autocomplete key index
|
|
5
5
|
exports.apiKey = 'key_M57QS8SMPdLdLx4x';
|
|
6
6
|
/// //////////////////////////////
|
|
@@ -119,7 +119,9 @@ exports.apiKeyDescription = `Pass an \`apiKey\` to request results from construc
|
|
|
119
119
|
exports.cioJsClientDescription = `If you are already using an instance of the \`ConstructorIOClient\`, you can pass a \`cioJsClient\` instead of an \`apiKey\` to request results from constructor's servers
|
|
120
120
|
|
|
121
121
|
> Note: when we say \`cioJsClient\`, we are referring to an instance of the [constructorio-client-javascript](https://www.npmjs.com/package/@constructor-io/constructorio-client-javascript)`;
|
|
122
|
-
exports.
|
|
122
|
+
exports.cioClientOptionsDescription = `If you don't want to create an instance of the \`ConstructorIOClient\` but still want to customize some of the options, you can pass a \`cioClientOptions\` object. You can learn more about the possible values [here under the parameters section](https://constructor-io.github.io/constructorio-client-javascript/ConstructorIO.html).
|
|
123
|
+
|
|
124
|
+
> Note: The legacy \`cioJsClientOptions\` prop is still supported for backwards compatibility, but \`cioClientOptions\` is now the preferred name.`;
|
|
123
125
|
exports.placeholderDescription = `Pass a \`placeholder\` to override the default input field placeholder text shown to users before they start typing their query`;
|
|
124
126
|
exports.searchSuggestionsDescription = `Override default \`sections\` to only suggest search terms`;
|
|
125
127
|
exports.productsDescription = `Override default \`sections\` to only suggested products`;
|
|
@@ -17,6 +17,7 @@ const useRecommendationsObserver_1 = tslib_1.__importDefault(require("./useRecom
|
|
|
17
17
|
const typeGuards_1 = require("../typeGuards");
|
|
18
18
|
const useNormalizedProps_1 = tslib_1.__importDefault(require("./useNormalizedProps"));
|
|
19
19
|
const useCustomBlur_1 = tslib_1.__importDefault(require("./useCustomBlur"));
|
|
20
|
+
const shopifyDefaults_1 = require("../utils/shopifyDefaults");
|
|
20
21
|
exports.defaultSections = [
|
|
21
22
|
{
|
|
22
23
|
indexSectionName: 'Search Suggestions',
|
|
@@ -28,11 +29,30 @@ exports.defaultSections = [
|
|
|
28
29
|
},
|
|
29
30
|
];
|
|
30
31
|
const useCioAutocomplete = (options) => {
|
|
31
|
-
const { sections, zeroStateSections,
|
|
32
|
-
const { onSubmit, onChange, openOnFocus, apiKey, cioJsClient, placeholder
|
|
32
|
+
const { sections, zeroStateSections, cioClientOptions, advancedParameters } = (0, useNormalizedProps_1.default)(options);
|
|
33
|
+
const { onSubmit: onSubmitProp, onChange, openOnFocus, apiKey, cioJsClient, placeholder: placeholderProp, autocompleteClassName = 'cio-autocomplete', defaultInput, useShopifyDefaults, shopifySettings, getSearchResultsUrl } = options, rest = tslib_1.__rest(options, ["onSubmit", "onChange", "openOnFocus", "apiKey", "cioJsClient", "placeholder", "autocompleteClassName", "defaultInput", "useShopifyDefaults", "shopifySettings", "getSearchResultsUrl"]);
|
|
34
|
+
let placeholder = 'What can we help you find today?';
|
|
35
|
+
if (placeholderProp) {
|
|
36
|
+
placeholder = placeholderProp;
|
|
37
|
+
}
|
|
38
|
+
else if (useShopifyDefaults) {
|
|
39
|
+
placeholder = shopifyDefaults_1.shopifyDefaults.placeholder;
|
|
40
|
+
}
|
|
41
|
+
let onSubmit;
|
|
42
|
+
if (onSubmitProp) {
|
|
43
|
+
onSubmit = onSubmitProp;
|
|
44
|
+
}
|
|
45
|
+
else if (useShopifyDefaults) {
|
|
46
|
+
onSubmit = (e) => shopifyDefaults_1.shopifyDefaults.onSubmit(e, shopifySettings);
|
|
47
|
+
}
|
|
33
48
|
const [query, setQuery] = (0, react_1.useState)(defaultInput || '');
|
|
34
49
|
const previousQuery = (0, usePrevious_1.default)(query);
|
|
35
|
-
const cioClient = (0, useCioClient_1.default)({
|
|
50
|
+
const cioClient = (0, useCioClient_1.default)({
|
|
51
|
+
apiKey,
|
|
52
|
+
cioJsClient,
|
|
53
|
+
cioClientOptions,
|
|
54
|
+
cioJsClientOptions: options.cioJsClientOptions,
|
|
55
|
+
});
|
|
36
56
|
// Get autocomplete sections (autocomplete + recommendations + custom)
|
|
37
57
|
const { fetchRecommendationResults, activeSections, activeSectionsWithData, zeroStateActiveSections, request, totalNumResultsPerSection, } = (0, useSections_1.default)(query, cioClient, sections, zeroStateSections, advancedParameters);
|
|
38
58
|
const features = (0, react_1.useMemo)(() => (0, features_1.getFeatures)(request), [request]);
|
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const react_1 = require("react");
|
|
4
4
|
const helpers_1 = require("../utils/helpers");
|
|
5
|
-
const useCioClient = ({ apiKey, cioJsClient, cioJsClientOptions }) => {
|
|
5
|
+
const useCioClient = ({ apiKey, cioJsClient, cioClientOptions, cioJsClientOptions, }) => {
|
|
6
6
|
if (!apiKey && !cioJsClient) {
|
|
7
7
|
// eslint-disable-next-line no-console
|
|
8
8
|
console.error('Either apiKey or cioJsClient is required');
|
|
9
9
|
}
|
|
10
|
-
|
|
10
|
+
const mergedClientOptions = cioClientOptions !== null && cioClientOptions !== void 0 ? cioClientOptions : cioJsClientOptions;
|
|
11
|
+
return (0, react_1.useMemo)(() => cioJsClient || (0, helpers_1.getCioClient)(apiKey, mergedClientOptions), [apiKey, cioJsClient, mergedClientOptions]);
|
|
11
12
|
};
|
|
12
13
|
exports.default = useCioClient;
|
|
@@ -38,15 +38,15 @@ const normalizeSections = (sections) => {
|
|
|
38
38
|
};
|
|
39
39
|
// Normalize and Memoize Objects to prevent infinite rerenders if users pass object literals to useCioAutocomplete
|
|
40
40
|
const useNormalizedProps = (options) => {
|
|
41
|
-
const { sections = exports.defaultSections, zeroStateSections, cioJsClientOptions, advancedParameters, } = options;
|
|
41
|
+
const { sections = exports.defaultSections, zeroStateSections, cioClientOptions, cioJsClientOptions, advancedParameters, } = options;
|
|
42
42
|
const sectionsMemoized = (0, react_1.useMemo)(() => normalizeSections(sections), [JSON.stringify(sections)]);
|
|
43
43
|
const zeroStateSectionsMemoized = (0, react_1.useMemo)(() => normalizeSections(zeroStateSections), [JSON.stringify(zeroStateSections)]);
|
|
44
|
-
const
|
|
44
|
+
const cioClientOptionsMemoized = (0, react_1.useMemo)(() => cioClientOptions !== null && cioClientOptions !== void 0 ? cioClientOptions : cioJsClientOptions, [JSON.stringify(cioClientOptions), JSON.stringify(cioJsClientOptions)]);
|
|
45
45
|
const advancedParametersMemoized = (0, react_1.useMemo)(() => advancedParameters, [JSON.stringify(advancedParameters)]);
|
|
46
46
|
return {
|
|
47
47
|
sections: sectionsMemoized,
|
|
48
48
|
zeroStateSections: zeroStateSectionsMemoized,
|
|
49
|
-
|
|
49
|
+
cioClientOptions: cioClientOptionsMemoized,
|
|
50
50
|
advancedParameters: advancedParametersMemoized,
|
|
51
51
|
};
|
|
52
52
|
};
|
package/lib/cjs/utils/helpers.js
CHANGED
|
@@ -61,9 +61,9 @@ const disableStoryActions = (story) => {
|
|
|
61
61
|
story.parameters.actions = { argTypesRegex: null };
|
|
62
62
|
};
|
|
63
63
|
exports.disableStoryActions = disableStoryActions;
|
|
64
|
-
const getCioClient = (apiKey,
|
|
64
|
+
const getCioClient = (apiKey, cioClientOptions) => {
|
|
65
65
|
if (apiKey && typeof window !== 'undefined') {
|
|
66
|
-
const cioClient = new constructorio_client_javascript_1.default(Object.assign(Object.assign({ apiKey, sendTrackingEvents: true, version: `cio-ui-autocomplete-${version_1.default}` },
|
|
66
|
+
const cioClient = new constructorio_client_javascript_1.default(Object.assign(Object.assign({ apiKey, sendTrackingEvents: true, version: `cio-ui-autocomplete-${version_1.default}` }, cioClientOptions), { eventDispatcher: { waitForBeacon: false } }));
|
|
67
67
|
// eslint-disable-next-line no-console
|
|
68
68
|
cioClient.tracker.on('error', (error) => console.error(error));
|
|
69
69
|
return cioClient;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.shopifyDefaults = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
// eslint-disable-next-line import/prefer-default-export
|
|
6
|
+
exports.shopifyDefaults = {
|
|
7
|
+
selector: '#cio-autocomplete-ui-container',
|
|
8
|
+
placeholder: 'Search',
|
|
9
|
+
onSubmit(event, shopifySettings) {
|
|
10
|
+
var _a;
|
|
11
|
+
/* Handle redirecting to a product page */
|
|
12
|
+
if ((0, types_1.isAutocompleteSelectSubmit)(event) && event.item.section === 'Products') {
|
|
13
|
+
const productUrl = (_a = event.item.data) === null || _a === void 0 ? void 0 : _a.url;
|
|
14
|
+
if (productUrl) {
|
|
15
|
+
let url;
|
|
16
|
+
try {
|
|
17
|
+
url = new URL(productUrl);
|
|
18
|
+
}
|
|
19
|
+
catch (_b) {
|
|
20
|
+
url = new URL(productUrl, location.origin);
|
|
21
|
+
}
|
|
22
|
+
const currentParams = new URLSearchParams(location.search);
|
|
23
|
+
currentParams.forEach((value, key) => {
|
|
24
|
+
if (!url.searchParams.has(key)) {
|
|
25
|
+
url.searchParams.set(key, value);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
window.location.href = url.toString();
|
|
29
|
+
}
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
/* Handle redirecting to a search page */
|
|
33
|
+
let query = '';
|
|
34
|
+
if (!(0, types_1.isAutocompleteSelectSubmit)(event) && event.query) {
|
|
35
|
+
query = event.query;
|
|
36
|
+
}
|
|
37
|
+
if ((0, types_1.isAutocompleteSelectSubmit)(event) && event.item.section === 'Search Suggestions') {
|
|
38
|
+
query = event.item.value;
|
|
39
|
+
}
|
|
40
|
+
if (!query) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const queryParams = new URLSearchParams(location.search);
|
|
44
|
+
const shopifySearchUrl = (shopifySettings === null || shopifySettings === void 0 ? void 0 : shopifySettings.searchUrl) || '';
|
|
45
|
+
queryParams.set('q', query);
|
|
46
|
+
window.location.href = location.origin + shopifySearchUrl + '?' + queryParams.toString();
|
|
47
|
+
},
|
|
48
|
+
};
|
package/lib/cjs/version.js
CHANGED
package/lib/mjs/constants.js
CHANGED
|
@@ -116,7 +116,9 @@ export const apiKeyDescription = `Pass an \`apiKey\` to request results from con
|
|
|
116
116
|
export const cioJsClientDescription = `If you are already using an instance of the \`ConstructorIOClient\`, you can pass a \`cioJsClient\` instead of an \`apiKey\` to request results from constructor's servers
|
|
117
117
|
|
|
118
118
|
> Note: when we say \`cioJsClient\`, we are referring to an instance of the [constructorio-client-javascript](https://www.npmjs.com/package/@constructor-io/constructorio-client-javascript)`;
|
|
119
|
-
export const
|
|
119
|
+
export const cioClientOptionsDescription = `If you don't want to create an instance of the \`ConstructorIOClient\` but still want to customize some of the options, you can pass a \`cioClientOptions\` object. You can learn more about the possible values [here under the parameters section](https://constructor-io.github.io/constructorio-client-javascript/ConstructorIO.html).
|
|
120
|
+
|
|
121
|
+
> Note: The legacy \`cioJsClientOptions\` prop is still supported for backwards compatibility, but \`cioClientOptions\` is now the preferred name.`;
|
|
120
122
|
export const placeholderDescription = `Pass a \`placeholder\` to override the default input field placeholder text shown to users before they start typing their query`;
|
|
121
123
|
export const searchSuggestionsDescription = `Override default \`sections\` to only suggest search terms`;
|
|
122
124
|
export const productsDescription = `Override default \`sections\` to only suggested products`;
|
|
@@ -13,6 +13,7 @@ import useRecommendationsObserver from './useRecommendationsObserver';
|
|
|
13
13
|
import { isCustomSection, isRecommendationsSection } from '../typeGuards';
|
|
14
14
|
import useNormalizedProps from './useNormalizedProps';
|
|
15
15
|
import useCustomBlur from './useCustomBlur';
|
|
16
|
+
import { shopifyDefaults } from '../utils/shopifyDefaults';
|
|
16
17
|
export const defaultSections = [
|
|
17
18
|
{
|
|
18
19
|
indexSectionName: 'Search Suggestions',
|
|
@@ -24,11 +25,30 @@ export const defaultSections = [
|
|
|
24
25
|
},
|
|
25
26
|
];
|
|
26
27
|
const useCioAutocomplete = (options) => {
|
|
27
|
-
const { sections, zeroStateSections,
|
|
28
|
-
const { onSubmit, onChange, openOnFocus, apiKey, cioJsClient, placeholder
|
|
28
|
+
const { sections, zeroStateSections, cioClientOptions, advancedParameters } = useNormalizedProps(options);
|
|
29
|
+
const { onSubmit: onSubmitProp, onChange, openOnFocus, apiKey, cioJsClient, placeholder: placeholderProp, autocompleteClassName = 'cio-autocomplete', defaultInput, useShopifyDefaults, shopifySettings, getSearchResultsUrl, ...rest } = options;
|
|
30
|
+
let placeholder = 'What can we help you find today?';
|
|
31
|
+
if (placeholderProp) {
|
|
32
|
+
placeholder = placeholderProp;
|
|
33
|
+
}
|
|
34
|
+
else if (useShopifyDefaults) {
|
|
35
|
+
placeholder = shopifyDefaults.placeholder;
|
|
36
|
+
}
|
|
37
|
+
let onSubmit;
|
|
38
|
+
if (onSubmitProp) {
|
|
39
|
+
onSubmit = onSubmitProp;
|
|
40
|
+
}
|
|
41
|
+
else if (useShopifyDefaults) {
|
|
42
|
+
onSubmit = (e) => shopifyDefaults.onSubmit(e, shopifySettings);
|
|
43
|
+
}
|
|
29
44
|
const [query, setQuery] = useState(defaultInput || '');
|
|
30
45
|
const previousQuery = usePrevious(query);
|
|
31
|
-
const cioClient = useCioClient({
|
|
46
|
+
const cioClient = useCioClient({
|
|
47
|
+
apiKey,
|
|
48
|
+
cioJsClient,
|
|
49
|
+
cioClientOptions,
|
|
50
|
+
cioJsClientOptions: options.cioJsClientOptions,
|
|
51
|
+
});
|
|
32
52
|
// Get autocomplete sections (autocomplete + recommendations + custom)
|
|
33
53
|
const { fetchRecommendationResults, activeSections, activeSectionsWithData, zeroStateActiveSections, request, totalNumResultsPerSection, } = useSections(query, cioClient, sections, zeroStateSections, advancedParameters);
|
|
34
54
|
const features = useMemo(() => getFeatures(request), [request]);
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { useMemo } from 'react';
|
|
2
2
|
import { getCioClient } from '../utils/helpers';
|
|
3
|
-
const useCioClient = ({ apiKey, cioJsClient, cioJsClientOptions }) => {
|
|
3
|
+
const useCioClient = ({ apiKey, cioJsClient, cioClientOptions, cioJsClientOptions, }) => {
|
|
4
4
|
if (!apiKey && !cioJsClient) {
|
|
5
5
|
// eslint-disable-next-line no-console
|
|
6
6
|
console.error('Either apiKey or cioJsClient is required');
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
const mergedClientOptions = cioClientOptions ?? cioJsClientOptions;
|
|
9
|
+
return useMemo(() => cioJsClient || getCioClient(apiKey, mergedClientOptions), [apiKey, cioJsClient, mergedClientOptions]);
|
|
9
10
|
};
|
|
10
11
|
export default useCioClient;
|
|
@@ -35,15 +35,15 @@ const normalizeSections = (sections) => {
|
|
|
35
35
|
};
|
|
36
36
|
// Normalize and Memoize Objects to prevent infinite rerenders if users pass object literals to useCioAutocomplete
|
|
37
37
|
const useNormalizedProps = (options) => {
|
|
38
|
-
const { sections = defaultSections, zeroStateSections, cioJsClientOptions, advancedParameters, } = options;
|
|
38
|
+
const { sections = defaultSections, zeroStateSections, cioClientOptions, cioJsClientOptions, advancedParameters, } = options;
|
|
39
39
|
const sectionsMemoized = useMemo(() => normalizeSections(sections), [JSON.stringify(sections)]);
|
|
40
40
|
const zeroStateSectionsMemoized = useMemo(() => normalizeSections(zeroStateSections), [JSON.stringify(zeroStateSections)]);
|
|
41
|
-
const
|
|
41
|
+
const cioClientOptionsMemoized = useMemo(() => cioClientOptions ?? cioJsClientOptions, [JSON.stringify(cioClientOptions), JSON.stringify(cioJsClientOptions)]);
|
|
42
42
|
const advancedParametersMemoized = useMemo(() => advancedParameters, [JSON.stringify(advancedParameters)]);
|
|
43
43
|
return {
|
|
44
44
|
sections: sectionsMemoized,
|
|
45
45
|
zeroStateSections: zeroStateSectionsMemoized,
|
|
46
|
-
|
|
46
|
+
cioClientOptions: cioClientOptionsMemoized,
|
|
47
47
|
advancedParameters: advancedParametersMemoized,
|
|
48
48
|
};
|
|
49
49
|
};
|
package/lib/mjs/utils/helpers.js
CHANGED
|
@@ -50,13 +50,13 @@ export const disableStoryActions = (story) => {
|
|
|
50
50
|
// eslint-disable-next-line
|
|
51
51
|
story.parameters.actions = { argTypesRegex: null };
|
|
52
52
|
};
|
|
53
|
-
export const getCioClient = (apiKey,
|
|
53
|
+
export const getCioClient = (apiKey, cioClientOptions) => {
|
|
54
54
|
if (apiKey && typeof window !== 'undefined') {
|
|
55
55
|
const cioClient = new ConstructorIOClient({
|
|
56
56
|
apiKey,
|
|
57
57
|
sendTrackingEvents: true,
|
|
58
58
|
version: `cio-ui-autocomplete-${version}`,
|
|
59
|
-
...
|
|
59
|
+
...cioClientOptions,
|
|
60
60
|
eventDispatcher: { waitForBeacon: false },
|
|
61
61
|
});
|
|
62
62
|
// eslint-disable-next-line no-console
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { isAutocompleteSelectSubmit } from '../types';
|
|
2
|
+
// eslint-disable-next-line import/prefer-default-export
|
|
3
|
+
export const shopifyDefaults = {
|
|
4
|
+
selector: '#cio-autocomplete-ui-container',
|
|
5
|
+
placeholder: 'Search',
|
|
6
|
+
onSubmit(event, shopifySettings) {
|
|
7
|
+
/* Handle redirecting to a product page */
|
|
8
|
+
if (isAutocompleteSelectSubmit(event) && event.item.section === 'Products') {
|
|
9
|
+
const productUrl = event.item.data?.url;
|
|
10
|
+
if (productUrl) {
|
|
11
|
+
let url;
|
|
12
|
+
try {
|
|
13
|
+
url = new URL(productUrl);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
url = new URL(productUrl, location.origin);
|
|
17
|
+
}
|
|
18
|
+
const currentParams = new URLSearchParams(location.search);
|
|
19
|
+
currentParams.forEach((value, key) => {
|
|
20
|
+
if (!url.searchParams.has(key)) {
|
|
21
|
+
url.searchParams.set(key, value);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
window.location.href = url.toString();
|
|
25
|
+
}
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
/* Handle redirecting to a search page */
|
|
29
|
+
let query = '';
|
|
30
|
+
if (!isAutocompleteSelectSubmit(event) && event.query) {
|
|
31
|
+
query = event.query;
|
|
32
|
+
}
|
|
33
|
+
if (isAutocompleteSelectSubmit(event) && event.item.section === 'Search Suggestions') {
|
|
34
|
+
query = event.item.value;
|
|
35
|
+
}
|
|
36
|
+
if (!query) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const queryParams = new URLSearchParams(location.search);
|
|
40
|
+
const shopifySearchUrl = shopifySettings?.searchUrl || '';
|
|
41
|
+
queryParams.set('q', query);
|
|
42
|
+
window.location.href = location.origin + shopifySearchUrl + '?' + queryParams.toString();
|
|
43
|
+
},
|
|
44
|
+
};
|
package/lib/mjs/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default '1.
|
|
1
|
+
export default '1.27.0';
|
package/lib/types/constants.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export declare const zeroStateDescription = "- when the text input field has no
|
|
|
8
8
|
export declare const fullFeaturedAndStyledExampleDescription = "Using the default options, the library displays clean and minimal visual elements, with the intent of making it easy for consumers to easily extend and customize the styles to suit unique needs. The example below shows a full featured and styled example to demonstrate what is possible using Constructor.io's advanced Autocomplete UI library.";
|
|
9
9
|
export declare const apiKeyDescription = "Pass an `apiKey` to request results from constructor's servers";
|
|
10
10
|
export declare const cioJsClientDescription = "If you are already using an instance of the `ConstructorIOClient`, you can pass a `cioJsClient` instead of an `apiKey` to request results from constructor's servers\n\n> Note: when we say `cioJsClient`, we are referring to an instance of the [constructorio-client-javascript](https://www.npmjs.com/package/@constructor-io/constructorio-client-javascript)";
|
|
11
|
-
export declare const
|
|
11
|
+
export declare const cioClientOptionsDescription = "If you don't want to create an instance of the `ConstructorIOClient` but still want to customize some of the options, you can pass a `cioClientOptions` object. You can learn more about the possible values [here under the parameters section](https://constructor-io.github.io/constructorio-client-javascript/ConstructorIO.html).\n\n> Note: The legacy `cioJsClientOptions` prop is still supported for backwards compatibility, but `cioClientOptions` is now the preferred name.";
|
|
12
12
|
export declare const placeholderDescription = "Pass a `placeholder` to override the default input field placeholder text shown to users before they start typing their query";
|
|
13
13
|
export declare const searchSuggestionsDescription = "Override default `sections` to only suggest search terms";
|
|
14
14
|
export declare const productsDescription = "Override default `sections` to only suggested products";
|
|
@@ -5,7 +5,7 @@ import { Nullable } from '@constructor-io/constructorio-client-javascript/lib/ty
|
|
|
5
5
|
import { Item, OnSubmit } from '../types';
|
|
6
6
|
interface UseDownShiftOptions extends UseComboboxProps<Item> {
|
|
7
7
|
setQuery: React.Dispatch<React.SetStateAction<string>>;
|
|
8
|
-
onSubmit
|
|
8
|
+
onSubmit?: OnSubmit;
|
|
9
9
|
previousQuery?: string;
|
|
10
10
|
cioClient: Nullable<ConstructorIOClient>;
|
|
11
11
|
}
|
|
@@ -3,7 +3,7 @@ export declare const defaultSections: UserDefinedSection[];
|
|
|
3
3
|
declare const useNormalizedProps: (options: UseCioAutocompleteOptions) => {
|
|
4
4
|
sections: UserDefinedSection[];
|
|
5
5
|
zeroStateSections: UserDefinedSection[] | undefined;
|
|
6
|
-
|
|
6
|
+
cioClientOptions: import("@constructor-io/constructorio-client-javascript").ConstructorClientOptions | undefined;
|
|
7
7
|
advancedParameters: import("../types").AdvancedParameters | undefined;
|
|
8
8
|
};
|
|
9
9
|
export default useNormalizedProps;
|
package/lib/types/types.d.ts
CHANGED
|
@@ -13,7 +13,11 @@ export type CioClientConfig = {
|
|
|
13
13
|
*/
|
|
14
14
|
cioJsClient?: ConstructorIOClient;
|
|
15
15
|
/**
|
|
16
|
-
* Pass a `
|
|
16
|
+
* Pass a `cioClientOptions` object to customize the client's configurations w/o creating a new instance of `ConstructorIOClient`. You can learn more about the possible values [Here](https://constructor-io.github.io/constructorio-client-javascript/ConstructorIO.html)
|
|
17
|
+
*/
|
|
18
|
+
cioClientOptions?: ConstructorClientOptions;
|
|
19
|
+
/**
|
|
20
|
+
* @deprecated Use `cioClientOptions` instead.
|
|
17
21
|
*/
|
|
18
22
|
cioJsClientOptions?: ConstructorClientOptions;
|
|
19
23
|
};
|
|
@@ -32,7 +36,10 @@ type OptionalItemsComboboxProps<Item> = Partial<UseComboboxProps<Item>> & {
|
|
|
32
36
|
items?: Item[];
|
|
33
37
|
};
|
|
34
38
|
export type UseCioAutocompleteOptions = Omit<CioAutocompleteProps, 'children'>;
|
|
35
|
-
export type
|
|
39
|
+
export type ShopifySettings = {
|
|
40
|
+
searchUrl: string;
|
|
41
|
+
};
|
|
42
|
+
export type CioAutocompletePropsBase = CioClientConfig & OptionalItemsComboboxProps<Item> & {
|
|
36
43
|
/**
|
|
37
44
|
* Set to `false` to show suggestions only after a user clears their query,
|
|
38
45
|
* but not when they initially select the input
|
|
@@ -43,11 +50,6 @@ export type CioAutocompleteProps = CioClientConfig & OptionalItemsComboboxProps<
|
|
|
43
50
|
* i.e. <a href=getSearchResultsUrl([selected_search_suggestion])>[Search Suggestion]</a>
|
|
44
51
|
*/
|
|
45
52
|
getSearchResultsUrl?: (item: SearchSuggestion) => string;
|
|
46
|
-
/**
|
|
47
|
-
* Callback function that runs when the user submits a search.
|
|
48
|
-
* Usually used to trigger a redirect
|
|
49
|
-
*/
|
|
50
|
-
onSubmit: OnSubmit;
|
|
51
53
|
/**
|
|
52
54
|
* Callback function that runs when the user focuses on the input
|
|
53
55
|
*/
|
|
@@ -86,7 +88,51 @@ export type CioAutocompleteProps = CioClientConfig & OptionalItemsComboboxProps<
|
|
|
86
88
|
* Search input default value
|
|
87
89
|
*/
|
|
88
90
|
defaultInput?: string;
|
|
91
|
+
/**
|
|
92
|
+
* Configuration settings for Shopify integration. Used when `useShopifyDefaults` is enabled.
|
|
93
|
+
*
|
|
94
|
+
* Allows you to customize the search URL and other Shopify-specific behaviors.
|
|
95
|
+
*/
|
|
96
|
+
shopifySettings?: ShopifySettings;
|
|
97
|
+
};
|
|
98
|
+
export type CioAutocompletePropsWithShopifyDefaults = {
|
|
99
|
+
/**
|
|
100
|
+
* Callback function that runs when the user submits a search.
|
|
101
|
+
* Usually used to trigger a redirect.
|
|
102
|
+
* If provided, it will override Shopify defaults even when useShopifyDefaults is true.
|
|
103
|
+
*/
|
|
104
|
+
onSubmit?: OnSubmit;
|
|
105
|
+
/**
|
|
106
|
+
* Set to `true` to apply Shopify-specific defaults for `onSubmit` behavior.
|
|
107
|
+
*
|
|
108
|
+
* When enabled, the autocomplete will automatically handle navigation:
|
|
109
|
+
* - **Product selections**: Redirects to the product URL (preserving query parameters)
|
|
110
|
+
* - **Search Suggestions**: Redirects to the search results page with the selected query
|
|
111
|
+
* - **Manual search**: Redirects to the search results page with the entered query
|
|
112
|
+
*
|
|
113
|
+
* **Note**: If you provide a custom `onSubmit` handler, it will override the Shopify defaults.
|
|
114
|
+
*/
|
|
115
|
+
useShopifyDefaults: true;
|
|
116
|
+
};
|
|
117
|
+
export type CioAutocompletePropsWithoutShopifyDefaults = {
|
|
118
|
+
/**
|
|
119
|
+
* Callback function that runs when the user submits a search.
|
|
120
|
+
* Usually used to trigger a redirect.
|
|
121
|
+
*/
|
|
122
|
+
onSubmit: OnSubmit;
|
|
123
|
+
/**
|
|
124
|
+
* Set to `true` to apply Shopify-specific defaults for `onSubmit` behavior.
|
|
125
|
+
*
|
|
126
|
+
* When enabled, the autocomplete will automatically handle navigation:
|
|
127
|
+
* - **Product selections**: Redirects to the product URL (preserving query parameters)
|
|
128
|
+
* - **Search Suggestions**: Redirects to the search results page with the selected query
|
|
129
|
+
* - **Manual search**: Redirects to the search results page with the entered query
|
|
130
|
+
*
|
|
131
|
+
* **Note**: If you provide a custom `onSubmit` handler, it will override the Shopify defaults.
|
|
132
|
+
*/
|
|
133
|
+
useShopifyDefaults?: false;
|
|
89
134
|
};
|
|
135
|
+
export type CioAutocompleteProps = CioAutocompletePropsBase & (CioAutocompletePropsWithShopifyDefaults | CioAutocompletePropsWithoutShopifyDefaults);
|
|
90
136
|
/**
|
|
91
137
|
* AutocompleteSubmitEvent type is AutocompleteSelectSubmit or AutocompleteSearchSubmit.
|
|
92
138
|
* Use isAutocompleteSearchSubmit or isAutocompleteSelectSubmit type predicates to safely access event properties.
|
|
@@ -115,7 +161,7 @@ export declare const isAutocompleteSelectSubmit: (event: AutocompleteSubmitEvent
|
|
|
115
161
|
* @example if (isAutocompleteSearchSubmit(event)) { ... } // `item` and `originalQuery` are available
|
|
116
162
|
*/
|
|
117
163
|
export declare const isAutocompleteSearchSubmit: (event: AutocompleteSubmitEvent) => event is AutocompleteSearchSubmit;
|
|
118
|
-
export type OnSubmit = (event: AutocompleteSubmitEvent) => unknown;
|
|
164
|
+
export type OnSubmit = (event: AutocompleteSubmitEvent, shopifySettings?: ShopifySettings) => unknown;
|
|
119
165
|
export type DownshiftGetItemPropsOptions = GetItemPropsOptions<Item>;
|
|
120
166
|
export type DownshiftGetItemProps = (options: GetItemPropsOptions<Item>) => object;
|
|
121
167
|
export type ItemPropsOptions = DownshiftGetItemPropsOptions & {
|
|
@@ -26,7 +26,7 @@ export declare const functionStrings: {
|
|
|
26
26
|
renderItem: string;
|
|
27
27
|
};
|
|
28
28
|
export declare const disableStoryActions: (story: any) => void;
|
|
29
|
-
export declare const getCioClient: (apiKey?: string,
|
|
29
|
+
export declare const getCioClient: (apiKey?: string, cioClientOptions?: ConstructorClientOptions) => ConstructorIOClient | null;
|
|
30
30
|
export declare const getActiveSectionsWithData: (activeSections: UserDefinedSection[], sectionsResults: SectionsData, sectionsRefs: React.MutableRefObject<React.RefObject<HTMLLIElement>[]>, podsData: Record<string, PodData>) => Section[];
|
|
31
31
|
export declare const escapeRegExp: (string: string) => string;
|
|
32
32
|
export declare const getItemsForActiveSections: (activeSectionsWithData: Section[]) => Item[];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { CioAutocompleteProps } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Shopify-specific default configurations for the autocomplete component.
|
|
4
|
+
*
|
|
5
|
+
* These defaults are designed to work seamlessly with Shopify themes and handle
|
|
6
|
+
* navigation patterns common in Shopify stores.
|
|
7
|
+
*/
|
|
8
|
+
export interface ShopifyDefaults {
|
|
9
|
+
onSubmit: NonNullable<CioAutocompleteProps['onSubmit']>;
|
|
10
|
+
selector: string;
|
|
11
|
+
placeholder: string;
|
|
12
|
+
}
|
|
13
|
+
export declare const shopifyDefaults: ShopifyDefaults;
|
package/lib/types/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: "1.
|
|
1
|
+
declare const _default: "1.27.0";
|
|
2
2
|
export default _default;
|
package/package.json
CHANGED