Skip to content

Commit 2e3d1f3

Browse files
authored
fix(infinitehits/hits): correctly deprecate __hitIndex (#5495)
* fix(infinitehits/hits): correctly deprecated __hitIndex JSDoc comments only work when in a docblock Also added a proper warning so string templates see this too. * fix: use omit in default template
1 parent 60ff5aa commit 2e3d1f3

File tree

11 files changed

+63
-17
lines changed

11 files changed

+63
-17
lines changed

packages/instantsearch.js/src/components/Answers/Answers.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
AnswersTemplates,
99
} from '../../widgets/answers/answers';
1010
import type { ComponentCSSClasses, Hit } from '../../types';
11+
import { warning } from '../../lib/utils';
1112

1213
export type AnswersComponentCSSClasses = ComponentCSSClasses<AnswersCSSClasses>;
1314

@@ -49,7 +50,7 @@ const Answers = ({
4950
/>
5051
) : (
5152
<ul className={cssClasses.list}>
52-
{hits.map((hit, position) => (
53+
{hits.map((hit, index) => (
5354
<Template
5455
{...templateProps}
5556
templateKey="item"
@@ -58,7 +59,13 @@ const Answers = ({
5859
key={hit.objectID}
5960
data={{
6061
...hit,
61-
__hitIndex: position,
62+
get __hitIndex() {
63+
warning(
64+
false,
65+
'The `__hitIndex` property is deprecated. Use the absolute `__position` instead.'
66+
);
67+
return index;
68+
},
6269
}}
6370
/>
6471
))}

packages/instantsearch.js/src/components/Hits/Hits.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { cx } from '@algolia/ui-components-shared';
55
import Template from '../Template/Template';
66
import type { SearchResults } from 'algoliasearch-helper';
77
import type { BindEventForHits, SendEventForHits } from '../../lib/utils';
8+
import { warning } from '../../lib/utils';
89
import type { PreparedTemplateProps } from '../../lib/templating';
910
import type { ComponentCSSClasses, Hit } from '../../types';
1011
import type { HitsCSSClasses, HitsTemplates } from '../../widgets/hits/hits';
@@ -54,7 +55,13 @@ const Hits = ({
5455
key={hit.objectID}
5556
data={{
5657
...hit,
57-
__hitIndex: index,
58+
get __hitIndex() {
59+
warning(
60+
false,
61+
'The `__hitIndex` property is deprecated. Use the absolute `__position` instead.'
62+
);
63+
return index;
64+
},
5865
}}
5966
bindEvent={bindEvent}
6067
sendEvent={sendEvent}

packages/instantsearch.js/src/components/InfiniteHits/InfiniteHits.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
InfiniteHitsTemplates,
1111
} from '../../widgets/infinite-hits/infinite-hits';
1212
import type { SendEventForHits, BindEventForHits } from '../../lib/utils';
13+
import { warning } from '../../lib/utils';
1314

1415
export type InfiniteHitsComponentCSSClasses =
1516
ComponentCSSClasses<InfiniteHitsCSSClasses>;
@@ -77,7 +78,7 @@ const InfiniteHits = ({
7778
)}
7879

7980
<ol className={cssClasses.list}>
80-
{hits.map((hit, position) => (
81+
{hits.map((hit, index) => (
8182
<Template
8283
{...templateProps}
8384
templateKey="item"
@@ -86,7 +87,13 @@ const InfiniteHits = ({
8687
key={hit.objectID}
8788
data={{
8889
...hit,
89-
__hitIndex: position,
90+
get __hitIndex() {
91+
warning(
92+
false,
93+
'The `__hitIndex` property is deprecated. Use the absolute `__position` instead.'
94+
);
95+
return index;
96+
},
9097
}}
9198
bindEvent={bindEvent}
9299
sendEvent={sendEvent}

packages/instantsearch.js/src/lib/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export * from './isPlainObject';
3535
export * from './isSpecialClick';
3636
export * from './logger';
3737
export * from './mergeSearchParameters';
38+
export * from './omit';
3839
export * from './noop';
3940
export * from './range';
4041
export * from './render-args';
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Creates a new object with the same keys as the original object, but without the excluded keys.
3+
* @param source original object
4+
* @param excluded keys to remove from the original object
5+
* @returns the new object
6+
*/
7+
export function omit<
8+
TSource extends Record<string, unknown>,
9+
TExcluded extends keyof TSource
10+
>(source: TSource, excluded: TExcluded[]): Omit<TSource, TExcluded> {
11+
if (source === null || source === undefined) {
12+
return source;
13+
}
14+
15+
type Output = Omit<TSource, TExcluded>;
16+
return Object.keys(source).reduce((target, key) => {
17+
if ((excluded as Array<keyof TSource>).indexOf(key) >= 0) {
18+
return target;
19+
}
20+
21+
const validKey = key as keyof Output;
22+
target[validKey] = source[validKey];
23+
24+
return target;
25+
}, {} as unknown as Output);
26+
}

packages/instantsearch.js/src/widgets/hits/__tests__/hits.test.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ describe('hits', () => {
8080
"matchLevel": "full"
8181
}
8282
},
83-
"__position": 1,
84-
"__hitIndex": 0
83+
"__position": 1
8584
}
8685
</li>
8786
<li
@@ -110,8 +109,7 @@ describe('hits', () => {
110109
"matchLevel": "full"
111110
}
112111
},
113-
"__position": 2,
114-
"__hitIndex": 1
112+
"__position": 2
115113
}
116114
</li>
117115
</ol>

packages/instantsearch.js/src/widgets/hits/defaultTemplates.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type { HitsComponentTemplates } from '../../components/Hits/Hits';
2+
import { omit } from '../../lib/utils';
23

34
const defaultTemplates: HitsComponentTemplates = {
45
empty() {
56
return 'No results';
67
},
78
item(data) {
8-
return JSON.stringify(data, null, 2);
9+
return JSON.stringify(omit(data, ['__hitIndex']), null, 2);
910
},
1011
};
1112

packages/instantsearch.js/src/widgets/hits/hits.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export type HitsTemplates = Partial<{
121121
*/
122122
item: TemplateWithBindEvent<
123123
Hit & {
124-
// @deprecated the index in the hits array, use __position instead, which is the absolute position
124+
/** @deprecated the index in the hits array, use __position instead, which is the absolute position */
125125
__hitIndex: number;
126126
}
127127
>;

packages/instantsearch.js/src/widgets/infinite-hits/__tests__/infinite-hits.test.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ describe('infiniteHits', () => {
8686
"matchLevel": "full"
8787
}
8888
},
89-
"__position": 1,
90-
"__hitIndex": 0
89+
"__position": 1
9190
}
9291
</li>
9392
<li
@@ -116,8 +115,7 @@ describe('infiniteHits', () => {
116115
"matchLevel": "full"
117116
}
118117
},
119-
"__position": 2,
120-
"__hitIndex": 1
118+
"__position": 2
121119
}
122120
</li>
123121
</ol>

packages/instantsearch.js/src/widgets/infinite-hits/defaultTemplates.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { omit } from '../../lib/utils';
12
import type { InfiniteHitsComponentTemplates } from '../../components/InfiniteHits/InfiniteHits';
23

34
const defaultTemplates: InfiniteHitsComponentTemplates = {
@@ -11,7 +12,7 @@ const defaultTemplates: InfiniteHitsComponentTemplates = {
1112
return 'Show more results';
1213
},
1314
item(data) {
14-
return JSON.stringify(data, null, 2);
15+
return JSON.stringify(omit(data, ['__hitIndex']), null, 2);
1516
},
1617
};
1718

0 commit comments

Comments
 (0)