-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
Autosuggest
remaining highlights
- Loading branch information
1 parent
f554c4f
commit f2c9f48
Showing
5 changed files
with
116 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
'braid-design-system': patch | ||
--- | ||
|
||
--- | ||
updated: | ||
- Autosuggest | ||
--- | ||
|
||
**Autosuggest**: Improve handing of `suggestionHighlight` prop when set to `remaining` | ||
|
||
Fixes a bug in `Autosuggest` when using `suggestionHighlight` prop set to `remaining`. | ||
If the input contained multiple words, the highlighted portion would be appended to the end of matching suggestions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/braid-design-system/src/lib/components/Autosuggest/reverseMatches.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { type Match, reverseMatches } from './reverseMatches'; | ||
|
||
describe('reverseMatches', () => { | ||
it('should return intervals for non-matching parts of the suggestion', () => { | ||
const suggestion = 'Apples etc'; | ||
const matches: Match[] = [ | ||
[2, 4], | ||
[6, 8], | ||
]; | ||
const expected: Match[] = [ | ||
[0, 2], | ||
[4, 6], | ||
[8, 10], | ||
]; | ||
|
||
expect(reverseMatches(suggestion, matches)).toEqual(expected); | ||
}); | ||
|
||
it('should handle no matches', () => { | ||
const suggestion = 'Apple'; | ||
const matches: Match[] = []; | ||
const expected: Match[] = [[0, 5]]; | ||
|
||
expect(reverseMatches(suggestion, matches)).toEqual(expected); | ||
}); | ||
|
||
it('should handle matches that cover the entire suggestion', () => { | ||
const suggestion = 'Apple'; | ||
const matches: Match[] = [[0, 5]]; | ||
const expected: Match[] = []; | ||
|
||
expect(reverseMatches(suggestion, matches)).toEqual(expected); | ||
}); | ||
|
||
it('should handle matches at the start of the suggestion', () => { | ||
const suggestion = 'Apple'; | ||
const matches: Match[] = [[0, 2]]; | ||
const expected: Match[] = [[2, 5]]; | ||
|
||
expect(reverseMatches(suggestion, matches)).toEqual(expected); | ||
}); | ||
|
||
it('should handle matches at the end of the suggestion', () => { | ||
const suggestion = 'Apple'; | ||
const matches: Match[] = [[3, 5]]; | ||
const expected: Match[] = [[0, 3]]; | ||
|
||
expect(reverseMatches(suggestion, matches)).toEqual(expected); | ||
}); | ||
|
||
it('should handle matches for a single character suggestion', () => { | ||
const suggestion = 'A'; | ||
const matches: Match[] = [[0, 1]]; | ||
const expected: Match[] = []; | ||
|
||
expect(reverseMatches(suggestion, matches)).toEqual(expected); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
packages/braid-design-system/src/lib/components/Autosuggest/reverseMatches.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export type Match = [number, number]; | ||
|
||
export function reverseMatches(suggestion: string, matches: Match[]): Match[] { | ||
const suggestionLength = suggestion.length; | ||
const reversedMatches: Match[] = []; | ||
|
||
let currentStart = 0; | ||
|
||
for (const [start, end] of matches) { | ||
if (currentStart < start) { | ||
reversedMatches.push([currentStart, start]); | ||
} | ||
|
||
currentStart = end; | ||
} | ||
|
||
if (currentStart < suggestionLength) { | ||
reversedMatches.push([currentStart, suggestionLength]); | ||
} | ||
|
||
return reversedMatches; | ||
} |