Skip to content

Commit 9fb4690

Browse files
committed
make itemComponent required in js
1 parent be5ab5c commit 9fb4690

File tree

7 files changed

+36
-102
lines changed

7 files changed

+36
-102
lines changed

examples/js/getting-started/src/app.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

examples/react/getting-started/src/App.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ export function App() {
6565
facetName="brand"
6666
limit={6}
6767
layoutComponent={Carousel}
68+
itemComponent={({ item }) => (
69+
<div>
70+
{item.facetName}:{item.facetValue}
71+
</div>
72+
)}
6873
/>
6974
</div>
7075
</div>

packages/instantsearch-ui-components/src/components/Carousel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import type {
1212
RecordWithObjectID,
1313
Renderer,
1414
SendEventForHits,
15+
TrendingFacetHit,
1516
} from '../types';
16-
import type { TrendingFacetHit } from 'algoliasearch';
1717

1818
export type CarouselProps<
1919
TObject,

packages/instantsearch-ui-components/src/components/TrendingFacets.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { cx } from '../lib';
55
import {
66
createDefaultEmptyComponent,
77
createDefaultHeaderComponent,
8-
createDefaultItemComponent,
98
} from './recommend-shared';
109

1110
import type {
@@ -18,8 +17,8 @@ import type {
1817
RecordWithObjectID,
1918
Renderer,
2019
SendEventForHits,
20+
TrendingFacetHit,
2121
} from '../types';
22-
import type { TrendingFacetHit } from 'algoliasearch';
2322

2423
type TrendingFacetLayoutProps<TClassNames extends Record<string, string>> = {
2524
classNames: TClassNames;
@@ -33,7 +32,7 @@ type TrendingFacetLayoutProps<TClassNames extends Record<string, string>> = {
3332
export type TrendingFacetsComponentProps<
3433
TComponentProps extends Record<string, unknown> = Record<string, unknown>
3534
> = {
36-
itemComponent?: (
35+
itemComponent: (
3736
props: RecommendItemComponentProps<TrendingFacetHit> & TComponentProps
3837
) => JSX.Element;
3938
items: TrendingFacetHit[];
@@ -69,10 +68,7 @@ export function createTrendingFacetsComponent({
6968
createElement,
7069
Fragment,
7170
}),
72-
itemComponent: ItemComponent = createDefaultItemComponent({
73-
createElement,
74-
Fragment,
75-
}),
71+
itemComponent: ItemComponent,
7672
layout: Layout = createListComponent({ createElement, Fragment }),
7773
items,
7874
status,

packages/instantsearch-ui-components/src/types/Recommend.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,21 @@ export type RecommendItemComponentProps<TObject> = {
100100

101101
// @TODO: use instantsearch status instead
102102
export type RecommendStatus = 'idle' | 'loading' | 'stalled' | 'error';
103+
104+
// for convenience, redefined here from instantsearch
105+
export type TrendingFacetHit = {
106+
/**
107+
* Recommendation score.
108+
*/
109+
_score: number;
110+
111+
/**
112+
* Facet attribute. To be used in combination with `facetValue`. If specified, only recommendations matching the facet filter will be returned.
113+
*/
114+
facetName: string;
115+
116+
/**
117+
* Facet value. To be used in combination with `facetName`. If specified, only recommendations matching the facet filter will be returned.
118+
*/
119+
facetValue: string;
120+
};

packages/instantsearch.js/src/widgets/trending-facets/trending-facets.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function createRenderer({
9999
/>
100100
);
101101
}
102-
: undefined;
102+
: () => <div></div>;
103103

104104
const emptyComponent = (
105105
templates.empty
@@ -180,11 +180,6 @@ export type TrendingFacetsTemplates = Partial<{
180180
> & { cssClasses: RecommendClassNames }
181181
>;
182182

183-
/**
184-
* Template to use for each result. This template will receive an object containing a single record.
185-
*/
186-
item: TemplateWithBindEvent<TrendingFacetHit>;
187-
188183
/**
189184
* Template to use to wrap all items.
190185
*/
@@ -201,7 +196,9 @@ export type TrendingFacetsTemplates = Partial<{
201196
cssClasses: Pick<TrendingFacetsCSSClasses, 'list' | 'item'>;
202197
}
203198
>;
204-
}>;
199+
}> & {
200+
item: TemplateWithBindEvent<TrendingFacetHit>;
201+
};
205202

206203
type TrendingFacetsWidgetParams = {
207204
/**
@@ -212,7 +209,7 @@ type TrendingFacetsWidgetParams = {
212209
/**
213210
* Templates to customize the widget.
214211
*/
215-
templates?: TrendingFacetsTemplates;
212+
templates: TrendingFacetsTemplates;
216213

217214
/**
218215
* CSS classes to add to the widget elements.
@@ -238,7 +235,7 @@ export default (function trendingFacets(
238235
threshold,
239236
escapeHTML,
240237
transformItems,
241-
templates = {},
238+
templates,
242239
cssClasses = {},
243240
} = widgetParams || {};
244241

packages/react-instantsearch/src/widgets/TrendingFacets.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createTrendingFacetsComponent } from 'instantsearch-ui-components';
2-
import React, { createElement, Fragment, useMemo } from 'react';
2+
import React, { createElement, Fragment } from 'react';
33
import { useInstantSearch, useTrendingFacets } from 'react-instantsearch-core';
44

55
import type {
@@ -24,7 +24,7 @@ export type TrendingFacetsProps = Omit<
2424
keyof UiProps
2525
> &
2626
UseTrendingFacetsProps & {
27-
itemComponent?: TrendingFacetsUiComponentProps['itemComponent'];
27+
itemComponent: TrendingFacetsUiComponentProps['itemComponent'];
2828
headerComponent?: TrendingFacetsUiComponentProps['headerComponent'];
2929
emptyComponent?: TrendingFacetsUiComponentProps['emptyComponent'];
3030
layoutComponent?: TrendingFacetsUiComponentProps['layout'];
@@ -70,17 +70,9 @@ export function TrendingFacets({
7070
})
7171
: undefined;
7272

73-
const _itemComponent: typeof itemComponent = useMemo(
74-
() =>
75-
itemComponent
76-
? (itemProps) => itemComponent({ ...itemProps })
77-
: undefined,
78-
[itemComponent]
79-
);
80-
8173
const uiProps: UiProps = {
8274
items,
83-
itemComponent: _itemComponent,
75+
itemComponent,
8476
headerComponent,
8577
emptyComponent,
8678
layout,

0 commit comments

Comments
 (0)