Skip to content

Commit b9f02f1

Browse files
authored
fix(select): isMulti with non-array v-model should return empty array in selectedOptions (#213)
* fix(select): isMulti with non-array v-model should return empty array in selectedOptions Fixes #212 * feat(select): add disableInvalidVModelWarn prop
1 parent 3583bc1 commit b9f02f1

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

Diff for: docs/props.md

+8
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ A unique identifier to be passed to the select control. Will be used on multiple
203203

204204
Aria attributes to be passed to the select control to improve accessibility.
205205

206+
## disableInvalidVModelWarn
207+
208+
**Type**: `boolean`
209+
210+
**Default**: `false`
211+
212+
When set to true, the component will not emit a `console.warn` because of an invalid `v-model` type when using `isMulti`. This is useful when using the component with dynamic `v-model` references.
213+
206214
## filterBy
207215

208216
**Type**:

Diff for: src/Select.vue

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const props = withDefaults(
2828
inputId: undefined,
2929
uid: uniqueId(),
3030
aria: undefined,
31+
disableInvalidVModelWarn: false,
3132
filterBy: (option: GenericOption, label: string, search: string) => label.toLowerCase().includes(search.toLowerCase()),
3233
getOptionValue: (option: GenericOption) => option.value,
3334
getOptionLabel: (option: GenericOption) => option.label,
@@ -87,10 +88,14 @@ const availableOptions = computed<GenericOption[]>(() => {
8788
const selectedOptions = computed(() => {
8889
if (props.isMulti) {
8990
if (!Array.isArray(selected.value)) {
90-
console.warn(`[vue3-select-component warn]: The v-model provided should be an array when using \`isMulti\` prop, instead it was: ${selected.value}`);
91+
if (!props.disableInvalidVModelWarn) {
92+
console.warn(`[vue3-select-component warn]: The v-model provided should be an array when using \`isMulti\` prop, instead it was: ${selected.value}`);
93+
}
94+
95+
return [];
9196
}
9297
93-
return (selected.value as OptionValue[]).map(
98+
return selected.value.map(
9499
(value) => props.options.find((option) => option.value === value)!,
95100
);
96101
}

Diff for: src/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ export type Props<GenericOption, OptionValue> = {
102102
required?: boolean;
103103
};
104104

105+
/**
106+
* When set to true, the component will not emit a `console.warn` because of an invalid
107+
* `v-model` type when using `isMulti`. This is useful when using the component with
108+
* dynamic `v-model` references.
109+
*/
110+
disableInvalidVModelWarn?: boolean;
111+
105112
/**
106113
* Callback to filter the options based on the search input. By default, it filters
107114
* the options based on the `label` property of the option. The label is retrieved

0 commit comments

Comments
 (0)