From dd433ee4b4d3a67eabaa9cacef7883c7c8c26167 Mon Sep 17 00:00:00 2001 From: Tijmen van Nesselrooij Date: Wed, 28 Jun 2023 22:30:11 +0200 Subject: [PATCH] Improve example with autocomplete buttons --- src/App.vue | 2 - src/components/HelloWorld.vue | 38 ------------- src/components/Inputs/AutoCompleteInput.vue | 60 ++++++++++++++++----- src/components/Inputs/TextInput.vue | 15 ++++-- src/components/PriceChecker.vue | 23 +++++--- 5 files changed, 75 insertions(+), 63 deletions(-) delete mode 100644 src/components/HelloWorld.vue diff --git a/src/App.vue b/src/App.vue index 422f1fa..5e83816 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,5 +1,4 @@ @@ -12,7 +11,6 @@ import PriceChecker from "./components/PriceChecker.vue"; - diff --git a/src/components/HelloWorld.vue b/src/components/HelloWorld.vue deleted file mode 100644 index 10cf048..0000000 --- a/src/components/HelloWorld.vue +++ /dev/null @@ -1,38 +0,0 @@ - - - - - diff --git a/src/components/Inputs/AutoCompleteInput.vue b/src/components/Inputs/AutoCompleteInput.vue index 22e7fb3..ff0479a 100644 --- a/src/components/Inputs/AutoCompleteInput.vue +++ b/src/components/Inputs/AutoCompleteInput.vue @@ -7,9 +7,12 @@ const props = withDefaults( modelValue: string; options: string[]; alwaysShowSuggestions?: boolean; + disabled?: boolean; + name?: string; }>(), { alwaysShowSuggestions: false, + disabled: false, } ); @@ -23,11 +26,34 @@ const onModelValueChange = (value: string) => { }; const hasFocus = ref(false); +const showSuggestions = ref(false); +let timeOutId = 0; + +const onFocus = () => { + hasFocus.value = true; + showSuggestions.value = true; + + clearTimeout(timeOutId); +}; + +const onBlur = () => { + hasFocus.value = false; + + // Hide the suggestions later to give the click handlers time to react + timeOutId = setTimeout(() => { + showSuggestions.value = false; + }, 100); +}; + +const onButtonClick = (value: string) => { + if (value !== props.modelValue) { + emits("update:modelValue", value); + } +}; const hasMatch = computed(() => props.options.includes(props.modelValue) ); - // If we would simply emit an auto complete event on update model value it would: // - not allow the parent to reject the new value without causing inconsistent state // - not handle the case where the parent is the one changing the value, forcing it to implement the autocomplete logic itself as well if needed @@ -42,16 +68,26 @@ watch( diff --git a/src/components/Inputs/TextInput.vue b/src/components/Inputs/TextInput.vue index 94e734c..64689d1 100644 --- a/src/components/Inputs/TextInput.vue +++ b/src/components/Inputs/TextInput.vue @@ -1,7 +1,14 @@