Skip to content

Commit 0f2b6fe

Browse files
committed
fix(select): assert Array.isArray when :is-multi, remove TS force-cast of v-model
Fixes #212
1 parent b62212c commit 0f2b6fe

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/Select.vue

+17-7
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const availableOptions = computed<GenericOption[]>(() => {
7373
7474
// Remove already selected values from the list of options, when in multi-select mode.
7575
const getNonSelectedOptions = (options: GenericOption[]) => options.filter(
76-
(option) => !(selected.value as OptionValue[]).includes(option.value),
76+
(option) => Array.isArray(selected.value) && !selected.value.includes(option.value),
7777
);
7878
7979
if (props.isSearchable && search.value) {
@@ -150,7 +150,12 @@ const setOption = (option: GenericOption) => {
150150
}
151151
152152
if (props.isMulti) {
153-
(selected.value as OptionValue[]).push(option.value);
153+
if (Array.isArray(selected.value)) {
154+
selected.value.push(option.value);
155+
}
156+
else if (!props.disableInvalidVModelWarn) {
157+
console.warn(`[vue3-select-component warn]: The v-model provided should be an array when using \`isMulti\` prop, instead it was: ${selected.value}`);
158+
}
154159
}
155160
else {
156161
selected.value = option.value;
@@ -171,8 +176,13 @@ const setOption = (option: GenericOption) => {
171176
172177
const removeOption = (option: GenericOption) => {
173178
if (props.isMulti && !props.isDisabled) {
174-
selected.value = (selected.value as OptionValue[]).filter((value) => value !== option.value);
175-
emit("optionDeselected", option);
179+
if (Array.isArray(selected.value)) {
180+
selected.value = selected.value.filter((value) => value !== option.value);
181+
emit("optionDeselected", option);
182+
}
183+
else if (!props.disableInvalidVModelWarn) {
184+
console.warn(`[vue3-select-component warn]: The v-model provided should be an array when using \`isMulti\` prop, instead it was: ${selected.value}`);
185+
}
176186
}
177187
};
178188
@@ -257,14 +267,14 @@ const handleNavigation = (e: KeyboardEvent) => {
257267
closeMenu();
258268
}
259269
260-
const hasSelectedValue = props.isMulti ? (selected.value as OptionValue[]).length > 0 : !!selected.value;
270+
const hasSelectedValue = props.isMulti && Array.isArray(selected.value) ? selected.value.length > 0 : !!selected.value;
261271
262272
// When pressing backspace with no search, remove the last selected option.
263273
if (e.key === "Backspace" && search.value.length === 0 && hasSelectedValue) {
264274
e.preventDefault();
265275
266-
if (props.isMulti) {
267-
selected.value = (selected.value as OptionValue[]).slice(0, -1);
276+
if (props.isMulti && Array.isArray(selected.value)) {
277+
selected.value = selected.value.slice(0, -1);
268278
}
269279
else {
270280
selected.value = undefined as OptionValue;

0 commit comments

Comments
 (0)