Vueform Multiselect
repository·main·Indexed 21 days ago
https://github.com/vueform/multiselectA highly customizable selection component for Vue.js supporting single select, multi-select, and tagging options. Features include async search, infinite scroll, object value support, grouped options, and compatibility with both Vue 3 and Vue 2 (with version constraints for Vue < 2.7).
What's inside @vueform/multiselect
- Use slots to customize the appearance of different parts of the Multiselect component, such as the placeholder, the options list, or individual tags.
Use Custom Slots for Options and Labels
mainMultiselect provides several slots to customize the rendering of different parts of the component:
singlelabel: Customize the rendering of the selected value in single select mode.multiplelabel: Customize the rendering of the selected values summary in multiple select mode.option: Customize the rendering of each option in the dropdown list.tag: Customize the rendering of selected tags in tags mode.
<!-- Example: Customizing the option slot --> <Multiselect v-model="value" label="name" :options="options" > <template v-slot:option="{ option }"> <img :src="option.icon"> {{ option.name }} </template> </Multiselect>Handle Async Default Values with `allowAbsent: true`
mainIf your asynchronous
:optionsreturns an array of strings (where the string is both the label and the value), you can use:allow-absent="true"to allowv-modelvalues that are not currently present in the loaded options list.<Multiselect mode="tags" v-model="value" :allow-absent="true" :resolve-on-load="false" :options="async (query) => await fetchLanguages(query)" /> <script> export default { data: () => ({ value: ['Java', 'JavaScript'] // These strings are not in the current options list }) } </script>Handle Async Default Values with `object: true`
mainWhen using asynchronous options with
resolveOnLoad: false, the component doesn't know the labels for values until the options are fetched. To provide default values in this scenario, set:object="true"and provide thev-modelvalues as objects containing bothlabelandvalueproperties.<template> <Multiselect mode="tags" v-model="value" :object="true" :resolve-on-load="false" :options="async (query) => await fetchLanguages(query)" /> </template> <script> export default { data: () => ({ value: [ { value: 'Java', label: 'Java' }, { value: 'JavaScript', label: 'JavaScript' }, ] }) } </script>Customize Multiselect with Tailwind CSS
mainTo use Multiselect with Tailwind CSS, follow these two steps:
- Configure Tailwind: Install
mini-svg-data-uriand add the required background images to yourtailwind.config.jsto handle the caret, spinner, and remove icons. - Import Theme: Import
@vueform/multiselect/themes/tailwind.cssin your main component's style block.
Alternatively, you can skip the CSS import and pass Tailwind classes directly to the component using the
:classesprop. Note that when using:classes, certain state-based classes (likedropdownToporcontainerActive) are automatically merged with their base classes (likedropdownorcontainer) by the component.// tailwind.config.js const svgToDataUri = require('mini-svg-data-uri') module.exports = { theme: { extend: { backgroundImage: (theme) => ({ 'multiselect-caret': `url("${svgToDataUri('<svg ...></svg>')}")`, 'multiselect-spinner': `url("${svgToDataUri('<svg ...></svg>')}")`, 'multiselect-remove': `url("${svgToDataUri('<svg ...></svg>')}")`, }) } } }<style> @import '@vueform/multiselect/themes/tailwind.css'; </style>- Configure Tailwind: Install
Load Async Options with Infinite Scroll
mainTo load large datasets efficiently, use
:infinite="true"and:resolve-on-load="false". This prevents the component from loading options on mount and instead loads them when the user opens the dropdown. Use the@openevent to trigger the initial resolution if no options are currently loaded.<Multiselect v-model="value" mode="tags" :resolve-on-load="false" :infinite="true" :limit="10" :options="async (query) => await fetchLanguages(query)" @open="(select$) => { if (select$.noOptions) { select$.resolveOptions() } } />Use Multiselect with Vue 3
mainTo use the component in a Vue 3 application, import
Multiselectfrom@vueform/multiselectand ensure you import the default theme CSS. Usev-modelfor data binding and:optionsto provide the selection list.<template> <div> <Multiselect v-model="value" :options="options" /> </div> </template> <script> import Multiselect from '@vueform/multiselect' export default { components: { Multiselect, }, data() { return { value: null, options: [ 'Batman', 'Robin', 'Joker', ] } } } </script> <style src="@vueform/multiselect/themes/default.css"></style>Install @vueform/multiselect via npm
mainTo use the Multiselect component in your project, install the package using npm:
npm install @vueform/multiselectUse Multiselect with Vue 2
mainFor Vue 2 projects, import the component from the specific Vue 2 distribution path:
@vueform/multiselect/dist/multiselect.vue2.js. You must also import the default theme CSS.<template> <div> <Multiselect v-model="value" :options="options" /> </div> </template> <script> import Multiselect from '@vueform/multiselect/dist/multiselect.vue2.js' export default { components: { Multiselect, }, data() { return { value: null, options: [ 'Batman', 'Robin', 'Joker', ] } } } </script> <style src="@vueform/multiselect/themes/default.css"></style>Customize Multiselect with CSS Variables
mainWhen using
default.css, you can customize the appearance of the Multiselect component by overriding CSS variables. You can apply these overrides globally using the:rootselector or on a per-instance basis by applying a custom class to the component and defining the variables within that class./* Global override */ :root { --ms-tag-bg: #059669; --ms-tag-color: #D1FAE5; --ms-tag-radius: 9999px; --ms-tag-font-weight: 400; } /* Instance-level override */ /* <Multiselect class="multiselect-green" ... /> */ .multiselect-green { --ms-tag-bg: #D1FAE5; --ms-tag-color: #059669; } /* Available variables include: */ /* --ms-font-size, --ms-bg, --ms-border-color, --ms-radius, --ms-tag-bg, --ms-tag-color, --ms-dropdown-bg, --ms-option-bg-selected, etc. */Configure search and filtering
mainWhen
searchable: trueis set, you can customize how users find options:trackBy: The property or properties to search against (defaults tolabel).searchFilter: A custom function(option, query, select$) => booleanto override the default search algorithm.regex: A regular expression to test the search input against.searchStart: Iftrue, matches must occur at the start of thetrackByvalues.strict: Iftrue, respects accents/diacritics in search (default:true).minChars: Minimum characters required before triggering an async refresh (default:0).delay: Milliseconds to wait after typing before refreshing async options (default:-1).
Configure value and label mapping
mainWhen using an array of objects for
options, use these props to map the object properties to the component's internal logic:valueProp: The property used as the option's value (default:'value').label: The property used for the display text (default:'label').trackBy: The property used for searching (defaults tolabel).disabledProp: The property used to determine if an option is disabled (default:'disabled').object: Iftrue, thev-modelwill store the entire option object. Iffalse, it stores only the value (default:false).