Skip to content

feat(select): invalid v-model with is-multi prop now returns all options #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vue3-select-component",
"type": "module",
"version": "0.11.5",
"version": "0.11.6-beta.2",
"description": "A flexible & modern select-input control for Vue 3.",
"author": "Thomas Cazade <[email protected]>",
"license": "MIT",
Expand Down
24 changes: 13 additions & 11 deletions src/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ const emit = defineEmits<{
(e: "search", value: string): void;
}>();

const selected = defineModel<OptionValue | OptionValue[]>({
required: true,
validator: (value, _props) => _props.isMulti ? Array.isArray(value) : !Array.isArray(value),
});
const selected = defineModel<OptionValue | OptionValue[]>({ required: true });

const containerRef = useTemplateRef("container");
const inputRef = useTemplateRef("input");
Expand All @@ -72,8 +69,9 @@ const availableOptions = computed<GenericOption[]>(() => {
}));

// Remove already selected values from the list of options, when in multi-select mode.
// In case an invalid v-model is provided, we return all options since we can't know what options are valid.
const getNonSelectedOptions = (options: GenericOption[]) => options.filter(
(option) => Array.isArray(selected.value) && !selected.value.includes(option.value),
(option) => Array.isArray(selected.value) ? !selected.value.includes(option.value) : true,
);

if (props.isSearchable && search.value) {
Expand All @@ -85,7 +83,7 @@ const availableOptions = computed<GenericOption[]>(() => {
return props.isMulti ? getNonSelectedOptions(options) : options;
});

const selectedOptions = computed(() => {
const selectedOptions = computed<GenericOption[]>(() => {
if (props.isMulti) {
if (!Array.isArray(selected.value)) {
if (!props.disableInvalidVModelWarn) {
Expand All @@ -95,9 +93,9 @@ const selectedOptions = computed(() => {
return [];
}

return selected.value.map(
(value) => props.options.find((option) => option.value === value)!,
);
return selected.value
.map((selectedValue) => props.options.find((option) => props.getOptionValue(option) === selectedValue))
.filter((option) => option !== undefined);
}

const found = props.options.find((option) => props.getOptionValue(option) === selected.value);
Expand Down Expand Up @@ -153,8 +151,12 @@ const setOption = (option: GenericOption) => {
if (Array.isArray(selected.value)) {
selected.value.push(option.value);
}
else if (!props.disableInvalidVModelWarn) {
console.warn(`[vue3-select-component warn]: The v-model provided should be an array when using \`isMulti\` prop, instead it was: ${selected.value}`);
else {
selected.value = [option.value];

if (!props.disableInvalidVModelWarn) {
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.`);
}
}
}
else {
Expand Down