Skip to content

Commit

Permalink
feat(recommend): introduce FrequentlyBoughtTogether UI component (#…
Browse files Browse the repository at this point in the history
…6114)

Co-authored-by: Dhaya <154633+dhayab@users.noreply.github.com>
Co-authored-by: Aymeric Giraudet <aymeric.giraudet@algolia.com>
Co-authored-by: Sarah Dayan <5370675+sarahdayan@users.noreply.github.com>
  • Loading branch information
4 people committed May 21, 2024
1 parent b66ed89 commit ddde6d2
Show file tree
Hide file tree
Showing 12 changed files with 600 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ scripts/*/CHANGELOG.md
# private files
.env
.idea/
.vscode/

# Caches
.eslintcache
Expand Down
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ module.exports = (api) => {
// false positive (babel doesn't know types)
// this is actually only called on arrays
'String.prototype.includes',

// false positive (spread)
'Object.getOwnPropertyDescriptors',
];
if (defaultShouldInject && !exclude.includes(name)) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
{
"path": "packages/vue-instantsearch/vue2/umd/index.js",
"maxSize": "66.75 kB"
"maxSize": "67 kB"
},
{
"path": "packages/vue-instantsearch/vue3/umd/index.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/** @jsx createElement */

import { cx } from '../lib';

import { createDefaultHeaderComponent } from './recommend-shared/DefaultHeader';
import { createListViewComponent } from './recommend-shared/ListView';

import type {
RecommendTranslations,
Renderer,
ComponentProps,
RecommendComponentProps,
} from '../types';

export type FrequentlyBoughtTogetherProps<
TObject,
TComponentProps extends Record<string, unknown> = Record<string, unknown>
> = ComponentProps<'div'> & RecommendComponentProps<TObject, TComponentProps>;

export function createFrequentlyBoughtTogetherComponent({
createElement,
Fragment,
}: Renderer) {
return function FrequentlyBoughtTogether<TObject>(
userProps: FrequentlyBoughtTogetherProps<TObject>
) {
const {
classNames = {},
fallbackComponent: FallbackComponent = () => null,
headerComponent: HeaderComponent = createDefaultHeaderComponent({
createElement,
Fragment,
}),
itemComponent: ItemComponent,
view: View = createListViewComponent({ createElement, Fragment }),
items,
status,
translations: userTranslations,
sendEvent,
...props
} = userProps;

const translations: Required<RecommendTranslations> = {
title: 'Frequently bought together',
sliderLabel: 'Frequently bought together products',
...userTranslations,
};

if (items.length === 0 && status === 'idle') {
return <FallbackComponent />;
}

return (
<section
{...props}
className={cx('ais-FrequentlyBoughtTogether', classNames.root)}
>
<HeaderComponent
classNames={{
...classNames,
title: cx('ais-FrequentlyBoughtTogether-title', classNames.title),
}}
recommendations={items}
translations={translations}
/>

<View
classNames={{
...classNames,
container: cx(
'ais-FrequentlyBoughtTogether-container',
classNames.container
),
list: cx('ais-FrequentlyBoughtTogether-list', classNames.list),
item: cx('ais-FrequentlyBoughtTogether-item', classNames.item),
}}
translations={translations}
itemComponent={ItemComponent}
items={items}
sendEvent={sendEvent}
/>
</section>
);
};
}
3 changes: 1 addition & 2 deletions packages/instantsearch-ui-components/src/components/Hits.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/** @jsx createElement */
import { cx } from '../lib';

import type { ComponentProps, Renderer } from '../types';
import type { ComponentProps, Renderer, SendEventForHits } from '../types';

// Should be imported from a shared package in the future
type Hit = Record<string, unknown> & {
objectID: string;
};
type SendEventForHits = (...props: unknown[]) => void;
type Banner = {
image: {
urls: Array<{
Expand Down
Loading

0 comments on commit ddde6d2

Please sign in to comment.