@constructor-io/constructorio-ui-autocomplete 1.26.0 → 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.
@@ -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',
@@ -29,7 +30,21 @@ exports.defaultSections = [
29
30
  ];
30
31
  const useCioAutocomplete = (options) => {
31
32
  const { sections, zeroStateSections, cioClientOptions, advancedParameters } = (0, useNormalizedProps_1.default)(options);
32
- const { onSubmit, onChange, openOnFocus, apiKey, cioJsClient, placeholder = 'What can we help you find today?', autocompleteClassName = 'cio-autocomplete', defaultInput, getSearchResultsUrl } = options, rest = tslib_1.__rest(options, ["onSubmit", "onChange", "openOnFocus", "apiKey", "cioJsClient", "placeholder", "autocompleteClassName", "defaultInput", "getSearchResultsUrl"]);
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
50
  const cioClient = (0, useCioClient_1.default)({
@@ -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
+ };
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = '1.26.0';
3
+ exports.default = '1.27.0';
@@ -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',
@@ -25,7 +26,21 @@ export const defaultSections = [
25
26
  ];
26
27
  const useCioAutocomplete = (options) => {
27
28
  const { sections, zeroStateSections, cioClientOptions, advancedParameters } = useNormalizedProps(options);
28
- const { onSubmit, onChange, openOnFocus, apiKey, cioJsClient, placeholder = 'What can we help you find today?', autocompleteClassName = 'cio-autocomplete', defaultInput, getSearchResultsUrl, ...rest } = 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
46
  const cioClient = useCioClient({
@@ -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
+ };
@@ -1 +1 @@
1
- export default '1.26.0';
1
+ export default '1.27.0';
@@ -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: OnSubmit;
8
+ onSubmit?: OnSubmit;
9
9
  previousQuery?: string;
10
10
  cioClient: Nullable<ConstructorIOClient>;
11
11
  }
@@ -36,7 +36,10 @@ type OptionalItemsComboboxProps<Item> = Partial<UseComboboxProps<Item>> & {
36
36
  items?: Item[];
37
37
  };
38
38
  export type UseCioAutocompleteOptions = Omit<CioAutocompleteProps, 'children'>;
39
- export type CioAutocompleteProps = CioClientConfig & OptionalItemsComboboxProps<Item> & {
39
+ export type ShopifySettings = {
40
+ searchUrl: string;
41
+ };
42
+ export type CioAutocompletePropsBase = CioClientConfig & OptionalItemsComboboxProps<Item> & {
40
43
  /**
41
44
  * Set to `false` to show suggestions only after a user clears their query,
42
45
  * but not when they initially select the input
@@ -47,11 +50,6 @@ export type CioAutocompleteProps = CioClientConfig & OptionalItemsComboboxProps<
47
50
  * i.e. <a href=getSearchResultsUrl([selected_search_suggestion])>[Search Suggestion]</a>
48
51
  */
49
52
  getSearchResultsUrl?: (item: SearchSuggestion) => string;
50
- /**
51
- * Callback function that runs when the user submits a search.
52
- * Usually used to trigger a redirect
53
- */
54
- onSubmit: OnSubmit;
55
53
  /**
56
54
  * Callback function that runs when the user focuses on the input
57
55
  */
@@ -90,7 +88,51 @@ export type CioAutocompleteProps = CioClientConfig & OptionalItemsComboboxProps<
90
88
  * Search input default value
91
89
  */
92
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;
93
134
  };
135
+ export type CioAutocompleteProps = CioAutocompletePropsBase & (CioAutocompletePropsWithShopifyDefaults | CioAutocompletePropsWithoutShopifyDefaults);
94
136
  /**
95
137
  * AutocompleteSubmitEvent type is AutocompleteSelectSubmit or AutocompleteSearchSubmit.
96
138
  * Use isAutocompleteSearchSubmit or isAutocompleteSelectSubmit type predicates to safely access event properties.
@@ -119,7 +161,7 @@ export declare const isAutocompleteSelectSubmit: (event: AutocompleteSubmitEvent
119
161
  * @example if (isAutocompleteSearchSubmit(event)) { ... } // `item` and `originalQuery` are available
120
162
  */
121
163
  export declare const isAutocompleteSearchSubmit: (event: AutocompleteSubmitEvent) => event is AutocompleteSearchSubmit;
122
- export type OnSubmit = (event: AutocompleteSubmitEvent) => unknown;
164
+ export type OnSubmit = (event: AutocompleteSubmitEvent, shopifySettings?: ShopifySettings) => unknown;
123
165
  export type DownshiftGetItemPropsOptions = GetItemPropsOptions<Item>;
124
166
  export type DownshiftGetItemProps = (options: GetItemPropsOptions<Item>) => object;
125
167
  export type ItemPropsOptions = DownshiftGetItemPropsOptions & {
@@ -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;
@@ -1,2 +1,2 @@
1
- declare const _default: "1.26.0";
1
+ declare const _default: "1.27.0";
2
2
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-ui-autocomplete",
3
- "version": "1.26.0",
3
+ "version": "1.27.0",
4
4
  "description": "Constructor.io Autocomplete UI library for web applications",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",