Skip to content

Commit

Permalink
#272 simplify Vue component
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Mar 17, 2023
1 parent c2caace commit 94d14e4
Showing 1 changed file with 25 additions and 33 deletions.
58 changes: 25 additions & 33 deletions nicegui/elements/select.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
export default {
props: ["options"],
template: `
<q-select
v-bind="$attrs"
:model-value="model"
:options="options"
@filter="filterFn"
@input-value="setModel"
>
<template v-for="(_, slot) in $slots" v-slot:[slot]="slotProps">
<slot :name="slot" v-bind="slotProps || {}" />
</template>
</q-select>
`,
setup(props) {
const stringOptions = Vue.ref(props.options);
const model = Vue.ref(null);
const options = Vue.ref(stringOptions.value);
const filterFn = (val, update, abort) => {
<q-select v-bind="$attrs" :options="filteredOptions" @filter="filterFn">
<template v-for="(_, slot) in $slots" v-slot:[slot]="slotProps">
<slot :name="slot" v-bind="slotProps || {}" />
</template>
</q-select>
`,
data() {
return {
initialOptions: this.options,
filteredOptions: this.options,
};
},
methods: {
filterFn(val, update, abort) {
update(() => {
const needle = val.toLocaleLowerCase();
options.value = stringOptions.value.filter((v) => v.label.toLocaleLowerCase().indexOf(needle) > -1);
this.filteredOptions = this.initialOptions.filter((v) => v.label.toLocaleLowerCase().indexOf(needle) > -1);
});
};
const setModel = (val) => (model.value = val);
Vue.watch(
() => props.options,
(newVal) => {
stringOptions.value = newVal;
options.value = stringOptions.value;
}
);
return {
model,
options,
filterFn,
setModel,
};
},
},
watch: {
options: {
handler(newOptions) {
this.initialOptions = newOptions;
this.filteredOptions = newOptions;
},
immediate: true,
},
},
};

0 comments on commit 94d14e4

Please sign in to comment.