Skip to content

Commit 43746b6

Browse files
ZSnakemocca102esezen
authored
[CI-3883] Support custom pricing for product suggestions (#173)
* [CI-3883] Add an optional parameter to sections configuration for a custom renderItem function * Update customRenderItemDescription description Co-authored-by: Enes Kutay SEZEN <eneskutaysezen@gmail.com> * remove import Co-authored-by: Enes Kutay SEZEN <eneskutaysezen@gmail.com> * Address comments * lint --------- Co-authored-by: Islam Moustafa <58053149+mocca102@users.noreply.github.com> Co-authored-by: Enes Kutay SEZEN <eneskutaysezen@gmail.com> Co-authored-by: mocca102 <islaam.agamy@gmail.com>
1 parent d2077eb commit 43746b6

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

src/components/Autocomplete/SectionItemsList/SectionItemsList.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ type SectionItemsListProps = {
1616

1717
// eslint-disable-next-line func-names
1818
const DefaultRenderSectionItemsList: RenderSectionItemsList = function ({ section }) {
19-
const { getSectionProps, getFormProps, advancedParameters } = useContext(CioAutocompleteContext);
19+
const { getSectionProps, query, getFormProps, advancedParameters } =
20+
useContext(CioAutocompleteContext);
2021
const { displayShowAllResultsButton, translations } = advancedParameters || {};
2122
const { onSubmit } = getFormProps();
2223
const { type, displayName } = section;
@@ -48,13 +49,18 @@ const DefaultRenderSectionItemsList: RenderSectionItemsList = function ({ sectio
4849
{camelToStartCase(sectionTitle)}
4950
</h5>
5051
<ul className='cio-section-items' role='none'>
51-
{section?.data?.map((item) => (
52-
<SectionItem
53-
item={item}
54-
key={item?.id}
55-
displaySearchTermHighlights={section.displaySearchTermHighlights}
56-
/>
57-
))}
52+
{section?.data?.map((item) => {
53+
if (typeof section?.renderItem === 'function') {
54+
return section.renderItem({ item, query });
55+
}
56+
return (
57+
<SectionItem
58+
item={item}
59+
key={item?.id}
60+
displaySearchTermHighlights={section.displaySearchTermHighlights}
61+
/>
62+
);
63+
})}
5864
</ul>
5965
{displayShowAllResultsButton &&
6066
type === 'autocomplete' &&

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,6 @@ export const translationsDescription = `Pass a \`translations\` object to displa
256256
\`\`\`
257257
`;
258258

259+
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.
260+
`;
259261
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.`;

src/stories/Autocomplete/Component/Sections.stories.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react';
12
import { CioAutocomplete } from '../../../index';
23
import { argTypes } from '../argTypes';
34
import { stringifyWithDefaults } from '../../../utils';
@@ -13,6 +14,7 @@ import {
1314
apiKey,
1415
onSubmitDefault as onSubmit,
1516
displaySearchTermHighlightsDescription,
17+
customRenderItemDescription,
1618
} from '../../../constants';
1719
import { ComponentTemplate, getComponentStoryParams, addComponentStoryDescription } from '.';
1820

@@ -200,3 +202,32 @@ addComponentStoryDescription(
200202
`const args = ${stringifyWithDefaults(RenderCustomSection.args)}`,
201203
customSectionDescription
202204
);
205+
206+
export const CustomRenderItem = ComponentTemplate.bind({});
207+
CustomRenderItem.args = {
208+
apiKey,
209+
onSubmit,
210+
sections: [
211+
{
212+
indexSectionName: 'Products',
213+
renderItem: ({ item, query }) => (
214+
<div>
215+
<a href={item.data?.url}>
216+
<h3>{item.value}</h3>
217+
<img src={item.data?.image_url} alt={item.value} />
218+
</a>
219+
<p>{`$ ${item.data?.price}`}</p>
220+
<p>Query: {query}</p>
221+
</div>
222+
),
223+
},
224+
{
225+
indexSectionName: 'Search Suggestions',
226+
},
227+
],
228+
};
229+
addComponentStoryDescription(
230+
CustomRenderItem,
231+
`const args = ${stringifyWithDefaults(CustomRenderItem.args)}`,
232+
customRenderItemDescription
233+
);

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export type SectionConfiguration = {
121121
// This property will only take effect when using the component and not the hook
122122
displaySearchTermHighlights?: boolean;
123123
ref?: React.RefObject<HTMLElement>;
124+
renderItem?: (props: { item: Item; query: string }) => ReactNode;
124125
};
125126

126127
export interface AutocompleteSectionConfiguration extends SectionConfiguration {

src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ ${templateCode}
115115

116116
export const functionStrings = {
117117
onSubmit: `(submitEvent) => console.dir(submitEvent)`,
118+
renderItem: `({ item, query }) => (
119+
<div>
120+
<a href={item.data?.url}>
121+
<h3>{item.value}</h3>
122+
<img src={item.data?.image_url} alt={item.value} />
123+
</a>
124+
<p>{item.data?.price}</p>
125+
</div>
126+
)`,
118127
};
119128

120129
export const stringifyWithDefaults = (obj) => {

0 commit comments

Comments
 (0)