Skip to content

Commit 7c799f0

Browse files
authored
7732 - Single-Select und Combobox: Hervorhebung der Suchworte (#7818)
2 parents d2192ab + 0d1ca0a commit 7c799f0

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

packages/components/src/components/combobox/shadow.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export class KolCombobox implements ComboboxAPI {
228228
<CustomSuggestionsOptionFc
229229
index={index}
230230
option={option}
231+
searchTerm={this.state._value}
231232
ref={(el) => {
232233
if (el) this.refSuggestions[index] = el;
233234
}}

packages/components/src/components/single-select/shadow.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ export class KolSingleSelect implements SingleSelectAPI {
280280
<CustomSuggestionsOptionFc
281281
index={index}
282282
option={option.label}
283+
searchTerm={this._inputValue}
283284
ref={(el) => {
284285
if (el) this.refOptions[index] = el;
285286
}}

packages/components/src/functional-components/CustomSuggestionsOption/CustomSuggestionsOption.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ export type CustomSuggestionsProps = JSXBase.HTMLAttributes<HTMLLIElement> & {
66
index: number;
77
option: W3CInputValue;
88
selected: boolean;
9+
searchTerm?: string;
910
ref?: ((elm?: HTMLLIElement | undefined) => void) | undefined;
1011
};
1112

12-
const CustomSuggestionsOptionFc: FC<CustomSuggestionsProps> = ({ index, ref, selected, onClick, onMouseOver, onFocus, onKeyDown, option }) => {
13+
const CustomSuggestionsOptionFc: FC<CustomSuggestionsProps> = ({ index, ref, selected, onClick, onMouseOver, onFocus, onKeyDown, option, searchTerm }) => {
14+
const highlightSearchTerm = (text: string, searchTerm: string) => {
15+
if (!searchTerm?.trim()) return text;
16+
17+
const regex = new RegExp(`(${searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
18+
const parts = text.split(regex);
19+
20+
return parts.map((part, partIndex) => (regex.test(part) ? <mark key={partIndex}>{part}</mark> : part));
21+
};
22+
1323
return (
1424
<li
1525
id={`option-${index}`}
@@ -25,7 +35,7 @@ const CustomSuggestionsOptionFc: FC<CustomSuggestionsProps> = ({ index, ref, sel
2535
class="kol-custom-suggestions-option"
2636
onKeyDown={onKeyDown}
2737
>
28-
{option}
38+
{highlightSearchTerm(String(option), searchTerm || '')}
2939
</li>
3040
);
3141
};

0 commit comments

Comments
 (0)