-
Notifications
You must be signed in to change notification settings - Fork 499
/
infinite-hits.tsx
229 lines (213 loc) · 5.77 KB
/
infinite-hits.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import React, { render, unmountComponentAtNode } from 'preact-compat';
import cx from 'classnames';
import InfiniteHits from '../../components/InfiniteHits/InfiniteHits';
import defaultTemplates from './defaultTemplates';
import connectInfiniteHits, {
InfiniteHitsRenderer,
} from '../../connectors/infinite-hits/connectInfiniteHits';
import {
prepareTemplateProps,
getContainerNode,
warning,
createDocumentationLink,
createDocumentationMessageGenerator,
} from '../../lib/utils';
import { component } from '../../lib/suit';
import { withInsights, withInsightsListener } from '../../lib/insights';
import {
WidgetFactory,
Template,
Hit,
InsightsClientWrapper,
SearchResults,
} from '../../types';
const withUsage = createDocumentationMessageGenerator({
name: 'infinite-hits',
});
const suit = component('InfiniteHits');
const InfiniteHitsWithInsightsListener = withInsightsListener(InfiniteHits);
export type InfiniteHitsCSSClasses = {
/**
* The root element of the widget.
*/
root: string | string[];
/**
* The root container without results.
*/
emptyRoot: string | string[];
/**
* The list of results.
*/
list: string | string[];
/**
* The list item.
*/
item: string | string[];
/**
* The “Show previous” button.
*/
loadPrevious: string | string[];
/**
* The disabled “Show previous” button.
*/
disabledLoadPrevious: string | string[];
/**
* The “Show more” button.
*/
loadMore: string | string[];
/**
* The disabled “Show more” button.
*/
disabledLoadMore: string | string[];
};
export type InfiniteHitsTemplates = {
/**
* The template to use when there are no results.
*/
empty: Template<{ results: SearchResults }>;
/**
* The template to use for the “Show previous” label.
*/
showPreviousText: Template;
/**
* The template to use for the “Show more” label.
*/
showMoreText: Template;
/**
* The template to use for each result.
*/
item: Template<Hit>;
};
export type InfiniteHitsRendererWidgetParams = {
/**
* Escapes HTML entities from hits string values.
*
* @default `true`
*/
escapeHTML: boolean;
/**
* Enable the button to load previous results.
*
* @default `false`
*/
showPrevious: boolean;
/**
* Receives the items, and is called before displaying them.
* Useful for mapping over the items to transform, and remove or reorder them.
*/
transformItems: (items: any[]) => any[];
};
interface InfiniteHitsWidgetParams extends InfiniteHitsRendererWidgetParams {
/**
* The CSS Selector or `HTMLElement` to insert the widget into.
*/
container: string | HTMLElement;
/**
* The CSS classes to override.
*/
cssClasses?: Partial<InfiniteHitsCSSClasses>;
/**
* The templates to use for the widget.
*/
templates?: Partial<InfiniteHitsTemplates>;
}
type InfiniteHits = WidgetFactory<InfiniteHitsWidgetParams>;
const renderer = ({
cssClasses,
containerNode,
renderState,
templates,
showPrevious: hasShowPrevious,
}): InfiniteHitsRenderer<InfiniteHitsRendererWidgetParams> => (
{
hits,
results,
showMore,
showPrevious,
isFirstPage,
isLastPage,
instantSearchInstance,
insights,
},
isFirstRendering
) => {
if (isFirstRendering) {
renderState.templateProps = prepareTemplateProps({
defaultTemplates,
templatesConfig: instantSearchInstance.templatesConfig,
templates,
});
return;
}
render(
<InfiniteHitsWithInsightsListener
cssClasses={cssClasses}
hits={hits}
results={results}
hasShowPrevious={hasShowPrevious}
showPrevious={showPrevious}
showMore={showMore}
templateProps={renderState.templateProps}
isFirstPage={isFirstPage}
isLastPage={isLastPage}
insights={insights as InsightsClientWrapper}
/>,
containerNode
);
};
const infiniteHits: InfiniteHits = (
{
container,
escapeHTML,
transformItems,
templates = defaultTemplates,
cssClasses: userCssClasses = {},
showPrevious,
} = {} as InfiniteHitsWidgetParams
) => {
if (!container) {
throw new Error(withUsage('The `container` option is required.'));
}
warning(
// @ts-ignore: We have this specific check because unlike `hits`, `infiniteHits` does not support
// the `allItems` template. This can be misleading as they are very similar.
typeof templates.allItems === 'undefined',
`The template \`allItems\` does not exist since InstantSearch.js 3.
You may want to migrate using \`connectInfiniteHits\`: ${createDocumentationLink(
{ name: 'infinite-hits', connector: true }
)}.`
);
const containerNode = getContainerNode(container);
const cssClasses = {
root: cx(suit(), userCssClasses.root),
emptyRoot: cx(suit({ modifierName: 'empty' }), userCssClasses.emptyRoot),
item: cx(suit({ descendantName: 'item' }), userCssClasses.item),
list: cx(suit({ descendantName: 'list' }), userCssClasses.list),
loadPrevious: cx(
suit({ descendantName: 'loadPrevious' }),
userCssClasses.loadPrevious
),
disabledLoadPrevious: cx(
suit({ descendantName: 'loadPrevious', modifierName: 'disabled' }),
userCssClasses.disabledLoadPrevious
),
loadMore: cx(suit({ descendantName: 'loadMore' }), userCssClasses.loadMore),
disabledLoadMore: cx(
suit({ descendantName: 'loadMore', modifierName: 'disabled' }),
userCssClasses.disabledLoadMore
),
};
const specializedRenderer = renderer({
containerNode,
cssClasses,
templates,
showPrevious,
renderState: {},
});
const makeInfiniteHits = withInsights(connectInfiniteHits)(
specializedRenderer,
() => unmountComponentAtNode(containerNode)
);
return makeInfiniteHits({ escapeHTML, transformItems, showPrevious });
};
export default infiniteHits;