Skip to content

Commit 43d8ca3

Browse files
committed
fix: empty text on select filter
1 parent 5c97eea commit 43d8ca3

File tree

1 file changed

+11
-39
lines changed
  • packages/components-vue/src/components/select

1 file changed

+11
-39
lines changed

packages/components-vue/src/components/select/Filter.vue

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<datalist :id="selectFilterName">
33
<!-- Select is also used as fallback for older browsers -->
44
<SelectSimple
5-
v-model="selectModel"
5+
v-model="aliasModel"
66
v-bind="{
77
...$attrs,
88
..._.omit(props, 'modelValue'),
@@ -23,7 +23,7 @@
2323
</datalist>
2424
<div v-if="supportsDatalist" class="flx --flxRow --flx-start-center --gap-5" v-bind="$attrs">
2525
<InputText
26-
v-model="textModel"
26+
v-model="aliasModel"
2727
:list="selectFilterName"
2828
v-bind="{
2929
..._.omit(props, 'modelValue'),
@@ -40,7 +40,6 @@
4040
iconProps,
4141
}"
4242
class="--flx"
43-
@change="handleTextInput"
4443
/>
4544
<ActionLink
4645
v-if="modelValue && selectOptions.length > 1"
@@ -115,27 +114,29 @@
115114
return `select-filter_${seed.replaceAll(" ", "") || randomId}`;
116115
});
117116
const selectOptions = computed<iFormOption[]>(() => (props.options ?? []).map(toOption));
118-
const textModel = ref<string | number>("");
119117
/**
120118
* Prefers alias instead of value
121119
*/
122-
const selectModel = computed({
120+
const aliasModel = computed({
123121
get: () => {
124122
const option = selectOptions.value.find(({ value }) => value === props.modelValue);
125123
126124
// alias first
127125
return option?.alias ?? option?.value ?? "";
128126
},
129127
set(valueOrAlias: string | number) {
128+
// This assumes that aliases are distinct enough
129+
const deburr = (v: string | number) => _.deburr(String(v)).toLowerCase();
130+
const newModel = deburr(valueOrAlias);
130131
// look for alias first
131132
const option = selectOptions.value.find(({ alias, value }) => {
132-
return alias === valueOrAlias || value === valueOrAlias;
133-
});
133+
const match = deburr(alias ?? value);
134134
135-
if (!option) return emit("update:model-value", "");
135+
return match === newModel;
136+
});
136137
137-
emit("update:model-value", option.value);
138-
textModel.value = option.alias || option.value;
138+
// emit if valid
139+
if (option) emit("update:model-value", option.value);
139140
},
140141
});
141142
const isInvalid = computed<boolean>(() => {
@@ -149,37 +150,8 @@
149150
*/
150151
function resetModel() {
151152
emit("update:model-value", "");
152-
textModel.value = "";
153-
}
154-
155-
/**
156-
* Handle select input
157-
* @listenerOverride select filter requires specific event handling
158-
*/
159-
function handleTextInput(e: Event) {
160-
const { target } = e as Event & { target: HTMLSelectElement };
161-
const deburr = (v: string | number) => _.deburr(String(v)).toLowerCase();
162-
const newModel = deburr(target.value);
163-
164-
// look for alias first
165-
const option = selectOptions.value.find(({ value, alias }) => {
166-
const match = deburr(alias ?? value);
167-
168-
return match === newModel;
169-
});
170-
171-
if (!option) return emit("update:model-value", "");
172-
173-
emit("update:model-value", option.value);
174-
textModel.value = option.alias ?? option.value;
175153
}
176154
177155
// lifecycle
178156
if (isBrowser) supportsDatalist.value = !!HTMLDataListElement;
179-
// Populate text model
180-
if (props.modelValue) {
181-
const option = selectOptions.value.find(({ value }) => value === props.modelValue);
182-
183-
if (option) textModel.value = option.alias ?? option.value;
184-
}
185157
</script>

0 commit comments

Comments
 (0)