Skip to content

Commit 996c644

Browse files
authored
feat(select): invalid v-model with is-multi prop now returns all options (#215)
* feat(select): invalid v-model with is-multi prop now returns all options * feat(select): automatically convert v-model to array when undefined with is-multi #212 * chore: release v0.11.6-beta.1 * fix(select): use getOptionValue to determine selected options when using isMulti prop #212 (comment) * chore: release v0.11.6-beta.2
1 parent 2925379 commit 996c644

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue3-select-component",
33
"type": "module",
4-
"version": "0.11.5",
4+
"version": "0.11.6-beta.2",
55
"description": "A flexible & modern select-input control for Vue 3.",
66
"author": "Thomas Cazade <[email protected]>",
77
"license": "MIT",

src/Select.vue

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ const emit = defineEmits<{
4444
(e: "search", value: string): void;
4545
}>();
4646
47-
const selected = defineModel<OptionValue | OptionValue[]>({
48-
required: true,
49-
validator: (value, _props) => _props.isMulti ? Array.isArray(value) : !Array.isArray(value),
50-
});
47+
const selected = defineModel<OptionValue | OptionValue[]>({ required: true });
5148
5249
const containerRef = useTemplateRef("container");
5350
const inputRef = useTemplateRef("input");
@@ -72,8 +69,9 @@ const availableOptions = computed<GenericOption[]>(() => {
7269
}));
7370
7471
// Remove already selected values from the list of options, when in multi-select mode.
72+
// In case an invalid v-model is provided, we return all options since we can't know what options are valid.
7573
const getNonSelectedOptions = (options: GenericOption[]) => options.filter(
76-
(option) => Array.isArray(selected.value) && !selected.value.includes(option.value),
74+
(option) => Array.isArray(selected.value) ? !selected.value.includes(option.value) : true,
7775
);
7876
7977
if (props.isSearchable && search.value) {
@@ -85,7 +83,7 @@ const availableOptions = computed<GenericOption[]>(() => {
8583
return props.isMulti ? getNonSelectedOptions(options) : options;
8684
});
8785
88-
const selectedOptions = computed(() => {
86+
const selectedOptions = computed<GenericOption[]>(() => {
8987
if (props.isMulti) {
9088
if (!Array.isArray(selected.value)) {
9189
if (!props.disableInvalidVModelWarn) {
@@ -95,9 +93,9 @@ const selectedOptions = computed(() => {
9593
return [];
9694
}
9795
98-
return selected.value.map(
99-
(value) => props.options.find((option) => option.value === value)!,
100-
);
96+
return selected.value
97+
.map((selectedValue) => props.options.find((option) => props.getOptionValue(option) === selectedValue))
98+
.filter((option) => option !== undefined);
10199
}
102100
103101
const found = props.options.find((option) => props.getOptionValue(option) === selected.value);
@@ -153,8 +151,12 @@ const setOption = (option: GenericOption) => {
153151
if (Array.isArray(selected.value)) {
154152
selected.value.push(option.value);
155153
}
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}`);
154+
else {
155+
selected.value = [option.value];
156+
157+
if (!props.disableInvalidVModelWarn) {
158+
console.warn(`[vue3-select-component warn]: The v-model provided should be an array when using \`isMulti\` prop, instead it was: ${selected.value}. Since an option has been selected, the component automatically converted the v-model to an array.`);
159+
}
158160
}
159161
}
160162
else {

0 commit comments

Comments
 (0)