Last active
March 17, 2022 21:43
-
-
Save irozgar/8927b610fed1f72facb9009b3f0688d7 to your computer and use it in GitHub Desktop.
Searchable select with menuselect and vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script setup> | |
import { ref, watch } from 'vue'; | |
const options = [ | |
"Barcelona", | |
"Madrid", | |
"Sevilla", | |
"Valencia", | |
"Vitoria", | |
"La Rioja", | |
]; | |
const filteredOptions = ref([...options]); | |
const searchTerm = ref(""); | |
const selectedOption = ref("Barcelona"); | |
watch(searchTerm, (newValue) => { | |
filteredOptions.value = options.filter((option) => option.toLowerCase().includes(newValue.toLowerCase())); | |
}); | |
function selectOption($event) { | |
selectedOption.value = $event.target.value | |
} | |
</script> | |
<template> | |
<p>Selected value: {{ selectedOption }}</p> | |
<p>Filtered options: {{ filteredOptions }} </p> | |
<selectmenu @change="selectOption($event)"> | |
<div slot="listbox"> | |
<popup behavior="listbox"> | |
<input type="text" @change.stop v-model="searchTerm" /> | |
<option v-for="option in filteredOptions">{{ option }}</option> | |
</popup> | |
</div> | |
</selectmenu> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
// Using vue Options API | |
import { defineComponent } from "vue"; | |
const options = [ | |
"Barcelona", | |
"Madrid", | |
"Sevilla", | |
"Valencia", | |
"Vitoria", | |
"La Rioja", | |
]; | |
export default defineComponent({ | |
data() { | |
return { | |
options, | |
searchTerm: "", | |
selectedOption: options[0] || undefined, | |
} | |
}, | |
methods: { | |
selectOption($event) { | |
this.selectedOption = $event.target.value; | |
}, | |
}, | |
computed: { | |
filteredOptions() { | |
return options.filter((option) => option.toLowerCase().includes(this.searchTerm.toLowerCase())); | |
} | |
} | |
}) | |
</script> | |
<template> | |
<p>Selected value: {{ selectedOption }}</p> | |
<p>Filtered options: {{ filteredOptions }} </p> | |
<selectmenu @change="selectOption($event)"> | |
<div slot="listbox"> | |
<popup behavior="listbox"> | |
<input type="text" v-model="searchTerm" /> | |
<option v-for="option in filteredOptions">{{ option }}</option> | |
</popup> | |
</div> | |
</selectmenu> | |
</template> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A simple POC of a searchable select using the HTML element selectmenu. It's an experimental element available in Chrome Canary by enabling Experimental Web Platform features in
about:flags
.More info an some examples here: https://css-tricks.com/the-selectmenu-element/