@constructor-io/constructorio-ui-autocomplete 1.23.24 → 1.23.26

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.
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const react_1 = tslib_1.__importDefault(require("react"));
5
+ function NoResults() {
6
+ return react_1.default.createElement("div", { className: 'cio-no-results-message' }, "No Results Found");
7
+ }
8
+ exports.default = NoResults;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const react_1 = tslib_1.__importStar(require("react"));
5
+ const CioAutocompleteProvider_1 = require("../CioAutocompleteProvider");
6
+ function CustomSectionItem(props) {
7
+ const { renderItem, item, query } = props;
8
+ const { getItemProps } = (0, react_1.useContext)(CioAutocompleteProvider_1.CioAutocompleteContext);
9
+ const ref = (0, react_1.useRef)(null);
10
+ // eslint-disable-next-line react-hooks/exhaustive-deps
11
+ const customElement = (0, react_1.useMemo)(() => renderItem({ item, query, getItemProps }), [item]);
12
+ const isDomElement = customElement instanceof HTMLElement;
13
+ const isReactNode = (0, react_1.isValidElement)(customElement);
14
+ (0, react_1.useEffect)(() => {
15
+ if (isDomElement && ref.current) {
16
+ ref.current.innerHTML = ''; // Clear previous content
17
+ ref.current.appendChild(customElement);
18
+ }
19
+ // eslint-disable-next-line react-hooks/exhaustive-deps
20
+ }, [item]);
21
+ if (isReactNode) {
22
+ return customElement;
23
+ }
24
+ if (isDomElement) {
25
+ return react_1.default.createElement("div", Object.assign({}, getItemProps(item), { ref: ref }));
26
+ }
27
+ return null;
28
+ }
29
+ exports.default = CustomSectionItem;
@@ -3,15 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  const react_1 = tslib_1.__importStar(require("react"));
5
5
  const SectionItem_1 = tslib_1.__importDefault(require("../SectionItem/SectionItem"));
6
+ const CustomSectionItem_1 = tslib_1.__importDefault(require("../SectionItem/CustomSectionItem"));
6
7
  const utils_1 = require("../../../utils");
7
8
  const CioAutocompleteProvider_1 = require("../CioAutocompleteProvider");
9
+ const NoResults_1 = tslib_1.__importDefault(require("../AutocompleteResults/NoResults"));
8
10
  // eslint-disable-next-line func-names
9
11
  const DefaultRenderSectionItemsList = function ({ section }) {
10
- var _a, _b;
11
- const { getSectionProps, query, getFormProps, advancedParameters, getItemProps } = (0, react_1.useContext)(CioAutocompleteProvider_1.CioAutocompleteContext);
12
+ var _a, _b, _c;
13
+ const { getSectionProps, query, getFormProps, advancedParameters } = (0, react_1.useContext)(CioAutocompleteProvider_1.CioAutocompleteContext);
12
14
  const { displayShowAllResultsButton, translations } = advancedParameters || {};
13
15
  const { onSubmit } = getFormProps();
14
- const { type, displayName } = section;
16
+ const { type, displayName, displayNoResultsMessage } = section;
15
17
  let sectionTitle = displayName;
16
18
  if (!sectionTitle) {
17
19
  switch (type) {
@@ -29,14 +31,19 @@ const DefaultRenderSectionItemsList = function ({ section }) {
29
31
  break;
30
32
  }
31
33
  }
32
- if (!((_a = section === null || section === void 0 ? void 0 : section.data) === null || _a === void 0 ? void 0 : _a.length))
34
+ // Display no results message
35
+ if (!((_a = section === null || section === void 0 ? void 0 : section.data) === null || _a === void 0 ? void 0 : _a.length) && displayNoResultsMessage)
36
+ return (react_1.default.createElement("li", Object.assign({}, getSectionProps(section)),
37
+ react_1.default.createElement("h5", { className: 'cio-section-name cio-sectionName', "aria-hidden": true }, (0, utils_1.camelToStartCase)(sectionTitle)),
38
+ react_1.default.createElement(NoResults_1.default, null)));
39
+ if (!((_b = section === null || section === void 0 ? void 0 : section.data) === null || _b === void 0 ? void 0 : _b.length))
33
40
  return null;
34
41
  // @deprecated `cio-sectionName` will be removed in the next major release
35
42
  return (react_1.default.createElement("li", Object.assign({}, getSectionProps(section)),
36
43
  react_1.default.createElement("h5", { className: 'cio-section-name cio-sectionName', "aria-hidden": true }, (0, utils_1.camelToStartCase)(sectionTitle)),
37
- react_1.default.createElement("ul", { className: 'cio-section-items', role: 'none' }, (_b = section === null || section === void 0 ? void 0 : section.data) === null || _b === void 0 ? void 0 : _b.map((item) => {
44
+ react_1.default.createElement("ul", { className: 'cio-section-items', role: 'none' }, (_c = section === null || section === void 0 ? void 0 : section.data) === null || _c === void 0 ? void 0 : _c.map((item) => {
38
45
  if (typeof (section === null || section === void 0 ? void 0 : section.renderItem) === 'function') {
39
- return section.renderItem({ item, query, getItemProps });
46
+ return (react_1.default.createElement(CustomSectionItem_1.default, { renderItem: section.renderItem, item: item, query: query, key: item.id }));
40
47
  }
41
48
  return (react_1.default.createElement(SectionItem_1.default, { item: item, key: item === null || item === void 0 ? void 0 : item.id, displaySearchTermHighlights: section.displaySearchTermHighlights }));
42
49
  })),
@@ -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.displaySearchTermHighlightsDescription = exports.recommendationsDescription = exports.sectionOrderDescription = exports.numResultsDescription = exports.contentDescription = exports.productsDescription = exports.searchSuggestionsDescription = exports.placeholderDescription = exports.cioJsClientOptionsDescription = exports.cioJsClientDescription = exports.apiKeyDescription = exports.fullFeaturedAndStyledExampleDescription = exports.zeroStateDescription = exports.userEventsDescription = exports.sectionsDescription = exports.hookDescription = exports.componentDescription = exports.apiKey = void 0;
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.cioJsClientOptionsDescription = 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
  /// //////////////////////////////
@@ -128,6 +128,7 @@ exports.numResultsDescription = `Override default \`numResults\` to only suggest
128
128
  exports.sectionOrderDescription = `Override default \`numResults\` to suggest products, then terms`;
129
129
  exports.recommendationsDescription = `Use constructor's recommendations service, with \`"type": "recommendations"\``;
130
130
  exports.displaySearchTermHighlightsDescription = `Use constructor's auto highlighting of words that match the search keyword, with \`"displaySearchTermHighlights": true\``;
131
+ exports.displayNoResultsMessageDescription = `Display a no results message for this section, with \`"displayNoResultsMessage": true\``;
131
132
  exports.customSectionDescription = `Use a custom section, by managing and passing your own data, with \`"type": "custom"\` and \`"data":[{...}]\``;
132
133
  exports.onFocusDescription = `Pass an \`onFocus\` callback function to execute some code each time the user applies focus to the text input field`;
133
134
  exports.onChangeDescription = `Pass an \`onChange\` callback function to execute some code each time the user changes the value of the text input field`;
@@ -244,5 +245,6 @@ exports.translationsDescription = `Pass a \`translations\` object to display tra
244
245
  `;
245
246
  exports.customRenderItemDescription = `Customize the rendering of individual items within a Section by providing a \`renderItem\` function. This function allows you to define how each item should be rendered.
246
247
  - Make sure to call \`getItemProps(item)\` and spread like this \`<div {...getItemProps(item)}/>\` on the container of each custom rendered item or else tracking will not work.
248
+ - Supports both ReactNode and HTMLElement as a return type
247
249
  `;
248
250
  exports.displayShowAllResultsButtonDescription = `Pass a boolean to \`displayShowAllResultsButton\` to display a button at the bottom of the Products section to show all results. This button will submit the form and trigger the \`onSubmit\` callback.`;
@@ -49,7 +49,7 @@ const useCioAutocomplete = (options) => {
49
49
  totalNumResultsPerSection,
50
50
  request,
51
51
  featureToggles: features,
52
- isOpen: isOpen && (items === null || items === void 0 ? void 0 : items.length) > 0,
52
+ isOpen,
53
53
  getMenuProps: () => (Object.assign(Object.assign({}, getMenuProps()), { className: 'cio-results', 'data-testid': 'cio-results', 'data-cnstrc-autosuggest': '' })),
54
54
  getLabelProps,
55
55
  openMenu,
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = '1.23.24';
3
+ exports.default = '1.23.26';
@@ -0,0 +1,4 @@
1
+ import React from 'react';
2
+ export default function NoResults() {
3
+ return React.createElement("div", { className: 'cio-no-results-message' }, "No Results Found");
4
+ }
@@ -0,0 +1,25 @@
1
+ import React, { useContext, useMemo, useEffect, useRef, isValidElement } from 'react';
2
+ import { CioAutocompleteContext } from '../CioAutocompleteProvider';
3
+ export default function CustomSectionItem(props) {
4
+ const { renderItem, item, query } = props;
5
+ const { getItemProps } = useContext(CioAutocompleteContext);
6
+ const ref = useRef(null);
7
+ // eslint-disable-next-line react-hooks/exhaustive-deps
8
+ const customElement = useMemo(() => renderItem({ item, query, getItemProps }), [item]);
9
+ const isDomElement = customElement instanceof HTMLElement;
10
+ const isReactNode = isValidElement(customElement);
11
+ useEffect(() => {
12
+ if (isDomElement && ref.current) {
13
+ ref.current.innerHTML = ''; // Clear previous content
14
+ ref.current.appendChild(customElement);
15
+ }
16
+ // eslint-disable-next-line react-hooks/exhaustive-deps
17
+ }, [item]);
18
+ if (isReactNode) {
19
+ return customElement;
20
+ }
21
+ if (isDomElement) {
22
+ return React.createElement("div", { ...getItemProps(item), ref: ref });
23
+ }
24
+ return null;
25
+ }
@@ -1,13 +1,15 @@
1
1
  import React, { useContext } from 'react';
2
2
  import SectionItem from '../SectionItem/SectionItem';
3
+ import CustomSectionItem from '../SectionItem/CustomSectionItem';
3
4
  import { camelToStartCase, translate } from '../../../utils';
4
5
  import { CioAutocompleteContext } from '../CioAutocompleteProvider';
6
+ import NoResults from '../AutocompleteResults/NoResults';
5
7
  // eslint-disable-next-line func-names
6
8
  const DefaultRenderSectionItemsList = function ({ section }) {
7
- const { getSectionProps, query, getFormProps, advancedParameters, getItemProps } = useContext(CioAutocompleteContext);
9
+ const { getSectionProps, query, getFormProps, advancedParameters } = useContext(CioAutocompleteContext);
8
10
  const { displayShowAllResultsButton, translations } = advancedParameters || {};
9
11
  const { onSubmit } = getFormProps();
10
- const { type, displayName } = section;
12
+ const { type, displayName, displayNoResultsMessage } = section;
11
13
  let sectionTitle = displayName;
12
14
  if (!sectionTitle) {
13
15
  switch (type) {
@@ -25,6 +27,11 @@ const DefaultRenderSectionItemsList = function ({ section }) {
25
27
  break;
26
28
  }
27
29
  }
30
+ // Display no results message
31
+ if (!section?.data?.length && displayNoResultsMessage)
32
+ return (React.createElement("li", { ...getSectionProps(section) },
33
+ React.createElement("h5", { className: 'cio-section-name cio-sectionName', "aria-hidden": true }, camelToStartCase(sectionTitle)),
34
+ React.createElement(NoResults, null)));
28
35
  if (!section?.data?.length)
29
36
  return null;
30
37
  // @deprecated `cio-sectionName` will be removed in the next major release
@@ -32,7 +39,7 @@ const DefaultRenderSectionItemsList = function ({ section }) {
32
39
  React.createElement("h5", { className: 'cio-section-name cio-sectionName', "aria-hidden": true }, camelToStartCase(sectionTitle)),
33
40
  React.createElement("ul", { className: 'cio-section-items', role: 'none' }, section?.data?.map((item) => {
34
41
  if (typeof section?.renderItem === 'function') {
35
- return section.renderItem({ item, query, getItemProps });
42
+ return (React.createElement(CustomSectionItem, { renderItem: section.renderItem, item: item, query: query, key: item.id }));
36
43
  }
37
44
  return (React.createElement(SectionItem, { item: item, key: item?.id, displaySearchTermHighlights: section.displaySearchTermHighlights }));
38
45
  })),
@@ -125,6 +125,7 @@ export const numResultsDescription = `Override default \`numResults\` to only su
125
125
  export const sectionOrderDescription = `Override default \`numResults\` to suggest products, then terms`;
126
126
  export const recommendationsDescription = `Use constructor's recommendations service, with \`"type": "recommendations"\``;
127
127
  export const displaySearchTermHighlightsDescription = `Use constructor's auto highlighting of words that match the search keyword, with \`"displaySearchTermHighlights": true\``;
128
+ export const displayNoResultsMessageDescription = `Display a no results message for this section, with \`"displayNoResultsMessage": true\``;
128
129
  export const customSectionDescription = `Use a custom section, by managing and passing your own data, with \`"type": "custom"\` and \`"data":[{...}]\``;
129
130
  export const onFocusDescription = `Pass an \`onFocus\` callback function to execute some code each time the user applies focus to the text input field`;
130
131
  export const onChangeDescription = `Pass an \`onChange\` callback function to execute some code each time the user changes the value of the text input field`;
@@ -240,5 +241,6 @@ export const translationsDescription = `Pass a \`translations\` object to displa
240
241
  `;
241
242
  export const customRenderItemDescription = `Customize the rendering of individual items within a Section by providing a \`renderItem\` function. This function allows you to define how each item should be rendered.
242
243
  - Make sure to call \`getItemProps(item)\` and spread like this \`<div {...getItemProps(item)}/>\` on the container of each custom rendered item or else tracking will not work.
244
+ - Supports both ReactNode and HTMLElement as a return type
243
245
  `;
244
246
  export const displayShowAllResultsButtonDescription = `Pass a boolean to \`displayShowAllResultsButton\` to display a button at the bottom of the Products section to show all results. This button will submit the form and trigger the \`onSubmit\` callback.`;
@@ -48,7 +48,7 @@ const useCioAutocomplete = (options) => {
48
48
  totalNumResultsPerSection,
49
49
  request,
50
50
  featureToggles: features,
51
- isOpen: isOpen && items?.length > 0,
51
+ isOpen,
52
52
  getMenuProps: () => ({
53
53
  ...getMenuProps(),
54
54
  className: 'cio-results',
@@ -1 +1 @@
1
- export default '1.23.24';
1
+ export default '1.23.26';
package/lib/styles.css CHANGED
@@ -191,4 +191,9 @@
191
191
  cursor: pointer;
192
192
  width: 15px;
193
193
  height: 15px;
194
- }
194
+ }
195
+
196
+ .cio-autocomplete .cio-section-search-suggestions .cio-no-results-message {
197
+ min-width: 160px;
198
+ margin: 15px 3px;
199
+ }
@@ -0,0 +1,2 @@
1
+ /// <reference types="react" />
2
+ export default function NoResults(): JSX.Element;
@@ -0,0 +1,13 @@
1
+ import { ReactNode } from 'react';
2
+ import { Item } from '../../../types';
3
+ interface CustomItemProps {
4
+ item: Item;
5
+ renderItem: (props: {
6
+ item: Item;
7
+ query: string;
8
+ getItemProps: (item: Item) => any;
9
+ }) => HTMLElement | ReactNode;
10
+ query: string;
11
+ }
12
+ export default function CustomSectionItem(props: CustomItemProps): JSX.Element | null;
13
+ export {};
@@ -17,6 +17,7 @@ export declare const numResultsDescription = "Override default `numResults` to o
17
17
  export declare const sectionOrderDescription = "Override default `numResults` to suggest products, then terms";
18
18
  export declare const recommendationsDescription = "Use constructor's recommendations service, with `\"type\": \"recommendations\"`";
19
19
  export declare const displaySearchTermHighlightsDescription = "Use constructor's auto highlighting of words that match the search keyword, with `\"displaySearchTermHighlights\": true`";
20
+ export declare const displayNoResultsMessageDescription = "Display a no results message for this section, with `\"displayNoResultsMessage\": true`";
20
21
  export declare const customSectionDescription = "Use a custom section, by managing and passing your own data, with `\"type\": \"custom\"` and `\"data\":[{...}]`";
21
22
  export declare const onFocusDescription = "Pass an `onFocus` callback function to execute some code each time the user applies focus to the text input field";
22
23
  export declare const onChangeDescription = "Pass an `onChange` callback function to execute some code each time the user changes the value of the text input field";
@@ -35,5 +36,5 @@ export declare const termsWithImagesAndCountsDescription = "Pass boolean flags f
35
36
  export declare const debounceDescription = "Pass an integer to `debounce` to override the recommended, default delay employed for debouncing autocomplete network requests between keystrokes as your users type into the text input field. The default value is 250, which results in a debounce delay of 250 milliseconds.";
36
37
  export declare const fetchZeroStateOnFocusDescription = "Pass a boolean to `fetchZeroStateOnFocus` to override the zero state fetching behavior from initial render to input focus.";
37
38
  export declare const translationsDescription = "Pass a `translations` object to display translatable words in your preferred language.\n\n- Current translatable keys:\n```\n {\n \"in\": \"...\",\n \"show all results\": \"...\"\n }\n```\n";
38
- export declare const customRenderItemDescription = "Customize the rendering of individual items within a Section by providing a `renderItem` function. This function allows you to define how each item should be rendered.\n- Make sure to call `getItemProps(item)` and spread like this `<div {...getItemProps(item)}/>` on the container of each custom rendered item or else tracking will not work.\n";
39
+ export declare const customRenderItemDescription = "Customize the rendering of individual items within a Section by providing a `renderItem` function. This function allows you to define how each item should be rendered.\n- Make sure to call `getItemProps(item)` and spread like this `<div {...getItemProps(item)}/>` on the container of each custom rendered item or else tracking will not work.\n- Supports both ReactNode and HTMLElement as a return type\n";
39
40
  export declare const displayShowAllResultsButtonDescription = "Pass a boolean to `displayShowAllResultsButton` to display a button at the bottom of the Products section to show all results. This button will submit the form and trigger the `onSubmit` callback.";
@@ -94,6 +94,7 @@ export type SectionConfiguration = {
94
94
  displayName?: string;
95
95
  numResults?: number;
96
96
  displaySearchTermHighlights?: boolean;
97
+ displayNoResultsMessage?: boolean;
97
98
  ref?: React.RefObject<HTMLElement>;
98
99
  renderItem?: (props: {
99
100
  item: Item;
@@ -1,2 +1,2 @@
1
- declare const _default: "1.23.24";
1
+ declare const _default: "1.23.26";
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.23.24",
3
+ "version": "1.23.26",
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",