Skip to content

Multi Select

This component provides a customizable multi-select dropdown with search functionality and the ability to remove selected items.

Installation

shell
npx shadcn-vue@latest add https://lib.adrianondik.com/r/multiselect.json

Usage

vue
<template>
    <MultiSelect />
</template>

<script setup lang="ts">
import MultiSelect from '@/Components/MultiSelect';
</script>

Features

  • Multi-select functionality.
  • Remove individual selected items using the "X" button.
  • Clear all selected items with a single button.
  • Filter options via a search input.
  • Automatic focus on the search input when the dropdown opens.
  • Closes the dropdown when clicking outside the component.
  • Accessibility: Enter and Space keys toggle dropdown, Escape closes it.

Props

PropTypeDescription
modelValue(string | number)[]Array of selected values, used for v-model binding.
optionsArray<{ value: string | number; label: string }>Available options to select from.
placeholderstringText displayed when no items are selected. Default: "Vybrať katerógie...".
searchPlaceholderstringText displayed in the search input. Default: "Hľadaj...".
noResultsTextstringText shown when no results match the search. Default: "Nenašli sa žiadne výsledky".
clearAllTextstringText for the "clear all" button. Default: "Vymazať všetky".

Emits

EventPayloadDescription
update:modelValue(string | number)[]Triggered when the selected values change (add/remove or clear all).

Reactive Variables

PropTypeDescription
isOpenbooleanTracks whether the dropdown is open or closed.
searchQuerystringValue of the search input.

Computed

NameDescription
filteredOptionsFilters the options based on searchQuery. Returns all options if the search input is empty.

Example

vue
<template>
  <div>
    <MultiSelect
      v-model="selectedCategories"
      :options="categoryOptions"
      placeholder="Select categories..."
      search-placeholder="Search categories..."
      no-results-text="No categories found"
      clear-all-text="Clear all selections"
    />

    <p class="mt-4">
      Selected Categories: {{ selectedCategories }}
    </p>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import MultiSelect from '@/Components/MultiSelect';

const selectedCategories = ref<(string | number)[]>([]);

const categoryOptions = [
  { value: 1, label: 'Technology' },
  { value: 2, label: 'Science' },
  { value: 3, label: 'Health' },
  { value: 4, label: 'Sports' },
  { value: 5, label: 'Entertainment' },
];
</script>