Skip to content

Commit 220a7f3

Browse files
committed
fix: fixed some bugs with suggestions when they aren't restricted or filtered
1 parent ddca781 commit 220a7f3

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

src/components/LibInput/LibInput.stories.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ export const AutosuggestSelectLike = {
9999
restrictToSuggestions: true,
100100
},
101101
}
102+
export const AutosuggestSelectLikeShowAllUnrestricted = {
103+
...WithAutosuggest,
104+
args: {
105+
...WithAutosuggest.args,
106+
restrictToSuggestions: false,
107+
suggestionsFilter: (_input: string, items: string[]) => items,
108+
},
109+
}
102110
export const AutosuggestObjectOptions = {
103111
...WithAutosuggest,
104112
args: {

src/components/LibSuggestions/LibSuggestions.stories.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ export const AlwaysShowAllSuggestions = {
9999
suggestionsFilter: (_input: string, items: string[]) => items,
100100
},
101101
}
102+
export const AlwaysShowAllSuggestionsAndNoRestrictToSuggestions = {
103+
...Primary,
104+
args: {
105+
...Primary.args,
106+
restrictToSuggestions: false,
107+
suggestionsFilter: (_input: string, items: string[]) => items,
108+
},
109+
}
102110
export const CustomSuggestionsObject = {
103111
...Primary,
104112
args: {

src/components/LibSuggestions/LibSuggestions.vue

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
px-1
2121
user-select-none
2222
cursor-pointer
23+
px-2
2324
`,
2425
index=== activeSuggestion && `bg-accent-200 dark:bg-accent-800`
2526
)"
@@ -29,7 +30,7 @@
2930
:key="item"
3031
@mouseover="activeSuggestion = index"
3132
@mousedown="activeSuggestion = index; mousedown = true;"
32-
@mouseup="activeSuggestion = index; mousedown = true;"
33+
@mouseup="activeSuggestion = index; mousedown = false;"
3334
@click="setSelected()"
3435
>
3536
<slot name="item" :item="item" :index="index">
@@ -191,7 +192,13 @@ const openSuggestions = (): void => {
191192
if (!openable.value) return
192193
$isOpen.value = true
193194
// see delay close
194-
if (activeSuggestion.value === -1) activeSuggestion.value = 0
195+
if (activeSuggestion.value === -1) {
196+
if (exactlyMatchingSuggestion.value) {
197+
activeSuggestion.value = suggestionsList.value?.indexOf(exactlyMatchingSuggestion.value) ?? -1
198+
} else {
199+
activeSuggestion.value = 0
200+
}
201+
}
195202
// activeSuggestion.value = 0
196203
}
197204
watch(() => props.canOpen, val => {
@@ -211,21 +218,25 @@ watch(() => $modelValue.value, () => {
211218
$inputValue.value = getStringValue($modelValue.value)
212219
})
213220
watch(() => $inputValue.value, () => {
214-
if (props.restrictToSuggestions) {
215-
if (exactlyMatchingSuggestion.value !== undefined) {
216-
$modelValue.value = $inputValue.value
217-
}
218-
} else {
219-
$modelValue.value = $inputValue.value
221+
if (props.restrictToSuggestions && !exactlyMatchingSuggestion.value) {
222+
return
220223
}
224+
$modelValue.value = $inputValue.value
225+
const i = suggestionsList.value?.indexOf(exactlyMatchingSuggestion.value) ?? -1
226+
if (
227+
i === -1
228+
&& props.restrictToSuggestions
229+
&& exactlyMatchingSuggestion.value !== undefined
230+
) return
231+
activeSuggestion.value = i
221232
})
222233
watchPostEffect((): void => {
223234
if (!openable.value) {
224235
closeSuggestions()
225236
return
226237
}
227238
if (suggestionAvailable.value) {
228-
if (!exactlyMatchingSuggestion.value || moreThanOneSuggestionAvailable.value) {
239+
if (!$isOpen.value && (!exactlyMatchingSuggestion.value || moreThanOneSuggestionAvailable.value)) {
229240
openSuggestions()
230241
}
231242
} else if (isValidSuggestion.value) {
@@ -234,7 +245,7 @@ watchPostEffect((): void => {
234245
})
235246
236247
237-
const setSuggestion = (num: number): void => {
248+
function setSuggestion(num: number): void {
238249
if (fullSuggestionsList.value === undefined) return
239250
const val = suggestionLabel.value(fullSuggestionsList.value[num])
240251

0 commit comments

Comments
 (0)