Skip to content

Commit ea6ca05

Browse files
committed
Merge branch 'dzungtran-feat-improve-combobox-continuous-suggestions'
2 parents 205a96f + aa464ce commit ea6ca05

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/lib/forms/floating-label/FloatingLabelInput.svelte

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
return;
3838
}
3939
40-
const searchTerm = ((value as string) || "").toLowerCase();
40+
const fullSearchTerm = ((value as string) || '').toLowerCase();
41+
const lastSpaceIndex = fullSearchTerm.lastIndexOf(' ');
42+
const searchTerm = lastSpaceIndex === -1 ? fullSearchTerm : fullSearchTerm.substring(lastSpaceIndex + 1);
4143
4244
if (searchTerm === "" && !backspaceUsed) {
4345
filteredSuggestions = [];
@@ -111,7 +113,15 @@
111113
}
112114
113115
function selectItem(item: string) {
114-
value = item;
116+
const currentValue = (value as string) || '';
117+
const lastSpaceIndex = currentValue.lastIndexOf(' ');
118+
119+
if (lastSpaceIndex === -1) {
120+
value = item + ' '; // Replace the whole value if no space, add trailing space
121+
} else {
122+
value = currentValue.substring(0, lastSpaceIndex + 1) + item + ' '; // Replace last word, add trailing space
123+
}
124+
115125
if (onSelect) onSelect(item);
116126
filteredSuggestions = [];
117127
selectedIndex = -1;

src/lib/forms/input-field/Input.svelte

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
return;
5353
}
5454
55-
const searchTerm = ((value as string) || "").toLowerCase();
55+
const fullSearchTerm = ((value as string) || '').toLowerCase();
56+
const lastSpaceIndex = fullSearchTerm.lastIndexOf(' ');
57+
const searchTerm = lastSpaceIndex === -1 ? fullSearchTerm : fullSearchTerm.substring(lastSpaceIndex + 1);
5658
5759
// Show suggestions if:
5860
// 1. There's actual input text, OR
@@ -169,7 +171,14 @@
169171
}
170172
171173
function selectItem(item: string) {
172-
value = item;
174+
const currentValue = (value as string) || '';
175+
const lastSpaceIndex = currentValue.lastIndexOf(' ');
176+
177+
if (lastSpaceIndex === -1) {
178+
value = item + ' '; // Replace the whole value if no space, add trailing space
179+
} else {
180+
value = currentValue.substring(0, lastSpaceIndex + 1) + item + ' '; // Replace last word, add trailing space
181+
}
173182
174183
if (onSelect) {
175184
onSelect(item);

0 commit comments

Comments
 (0)