Skip to content

Commit 7e6df5f

Browse files
author
Aegis Dev
committed
fix(preset-algolia): keep separator highlight when reverse-highlight siblings disagree
isPartHighlighted defaulted neighbor highlight states with `|| true`, which also overrode an existing `false` neighbor. That made both siblings always appear highlighted, so every non-alphanumeric separator was treated as highlighted-from-siblings regardless of its real neighbors. Use `?? true` so the default only applies when a neighbor is missing.
1 parent a5f3270 commit 7e6df5f

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

packages/autocomplete-preset-algolia/src/highlight/__tests__/isPartHighlighted.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ describe('isPartHighlighted', () => {
1616
).toEqual(true);
1717
});
1818

19+
test('does not inherit sibling state when siblings disagree', () => {
20+
// The separator (index 1) sits between a highlighted ("Amazon") and a
21+
// non-highlighted ("Fire") part. Since the siblings disagree, the separator
22+
// must keep its own `isHighlighted` value rather than being treated as
23+
// highlighted-from-siblings.
24+
expect(
25+
isPartHighlighted(
26+
[
27+
{ isHighlighted: true, value: 'Amazon' },
28+
{ isHighlighted: false, value: ' - ' },
29+
{ isHighlighted: false, value: 'Fire' },
30+
],
31+
1
32+
)
33+
).toEqual(false);
34+
});
35+
1936
test('returns the isHighlighted value with both siblings', () => {
2037
expect(
2138
isPartHighlighted(

packages/autocomplete-preset-algolia/src/highlight/isPartHighlighted.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ function unescape(value: string): string {
2525

2626
export function isPartHighlighted(parts: ParsedAttribute[], i: number) {
2727
const current = parts[i];
28-
const isNextHighlighted = parts[i + 1]?.isHighlighted || true;
29-
const isPreviousHighlighted = parts[i - 1]?.isHighlighted || true;
28+
const isNextHighlighted = parts[i + 1]?.isHighlighted ?? true;
29+
const isPreviousHighlighted = parts[i - 1]?.isHighlighted ?? true;
3030

3131
if (
3232
!hasLetterOrNumber.test(unescape(current.value)) &&

0 commit comments

Comments
 (0)