Skip to content

Commit 6b633ba

Browse files
committed
feat: added suggestion picked to suggestion submit event
This was added because we often want to know what suggestion item the user picked, but by the time of the submit event the active suggestion is undefined because the suggestions were closed.
1 parent a72e977 commit 6b633ba

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

src/components/LibInput/LibInput.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ defineOptions({
173173
})
174174
const $slots = useSlots()
175175
const emit = defineEmits<{
176-
(e: "submit", val: string): void
176+
(e: "submit", val: string, suggestion?: any): void
177177
(e: "input", val: InputEvent): void
178178
(e: "keydown", val: KeyboardEvent): void
179179
(e: "blur", val: FocusEvent): void
@@ -301,9 +301,9 @@ const suggestionProps = computed(() => ({
301301
inputValue: inputValue.value,
302302
isValid: props.isValid,
303303
"onUpdate:inputValue": (e: string) => inputValue.value = e,
304-
onSubmit: (e: string) => {
304+
onSubmit: (e: string, suggestion: any) => {
305305
$modelValue.value = e
306-
emit("submit", e)
306+
emit("submit", e, suggestion)
307307
if ($values.value) {
308308
$values.value.push(e)
309309
}

src/components/shared/props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export type SuggestionsProps<T = any> = {
6969
}
7070
export type SuggestionsOptions<T> = SuggestionsProps<T> & MultiValueProps
7171
export interface SuggestionsEmits {
72-
(e: "submit", val: string): void
72+
(e: "submit", val: string, suggestion?: any): void
7373
(e: "update:isOpen", val: boolean): void
7474
(e: "update:activeSuggestion", val: number): void
7575
}

src/composables/useSuggestions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-console */
22
import { isBlank } from "@alanscodelog/utils/isBlank.js"
33
import { isObject } from "@alanscodelog/utils/isObject.js"
4-
import { computed, type Ref, ref, watch } from "vue"
4+
import { computed, type Ref, ref, toRaw, watch } from "vue"
55

66
import { type SuggestionsEmits,type SuggestionsOptions } from "../components/shared/props.js"
77

@@ -110,12 +110,13 @@ export function useSuggestions<TSuggestion>(
110110
if (debug) console.log("enterSuggestion", num)
111111
if (filteredSuggestions.value === undefined) return
112112

113-
const val = suggestionLabel.value(filteredSuggestions.value[num])
113+
const suggestion = filteredSuggestions.value[num]
114+
const val = suggestionLabel.value(suggestion)
114115

115116
$modelValue.value = val
116117
$inputValue.value = val
117118
closeSuggestions()
118-
emit("submit", $modelValue.value)
119+
emit("submit", val, toRaw(suggestion))
119120
}
120121

121122
const enterSelected = (): void => {

0 commit comments

Comments
 (0)