Multiple-Select-Vanilla
Description
Multiple-Select-Vanilla is a fork of the popular Multiple-Select (jQuery) library (thanks to @wenzhixin for this great project). This fork was based on its latest known version at the time, which was v1.5.2, but later updated to v1.7.0. The main difference from the original lib is that we dropped jQuery in favor of native browser code and this mean zero external dependency. As a bonus, a few extra features were also added to the library and you can see them listed below Changes vs Original lib.
This lib allows you to select multiple elements with checkboxes :).
To get started take a look at the Live demo for all available options and methods that the library offers.
Demo
Take a look at the Live demo to see all available options/methods (there's a lot).
You can also take a look at the "Used by" section below to see and try live real world Open Source projects taking advantage of this library.
Fully tested with
The Live demo website is also helpful to run a full suite of E2E tests by using Playwright, all project examples have dedicated Playwright tests.
Installation
npm install multiple-select-vanilla
Basic Usage
1. from a native <select>
<select class="multiple-select full-width" data-test="select1">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
<option value="4">Fourth</option>
</select>
and then initialize ms-select
import { multipleSelect } from 'multiple-select-vanilla';
const ms = multipleSelect('.multiple-select');
2. or from a data array of string/numbers or text/value pair (object array)
import { multipleSelect } from 'multiple-select-vanilla';
const ms = multipleSelect('.multiple-select', {
data: ['First', 'Second'] // native string/number/boolean
};
const ms = multipleSelect('.multiple-select', {
data: [
// text/value pair
{ text: 'First', value: 1 },
{ text: 'Second', value: 2 },
{ text: 'Third', value: 3 },
{ text: 'Fourth', value: 4 },
]
});
Changelog
CHANGELOG
LICENSE
MIT License
Changes vs Original lib (multiple-select)
Changes and new options:
- dropped jQuery requirement and rewrote with browser native code.
- rewritten in TypeScript to also provide typings support (
d.ts)
- make the lib CSP compliant (see CSP Compliance section below)
- add E2E tests, with Playwright, for all existing examples (over 80 of them)
- revamped the UI in v3.x to give it a more Modern Look:
- improved CSS styling by using SVG icons (CSS/SASS variables are also availables)
- new Dark Mode
- replace tabIndex with a more intuitive navigation & highlight by using arrow keys (or mouse hover)
- show 3 different states on multiple selections (none " ", partial "-" or all "🗸")
- add extra options:
autoAdjustDropHeight will automatically adjust the drop (up/down) height by available space (see demo)
autoAdjustDropPosition will find best position (top/bottom) by its available space (see demo)
autoAdjustDropWidthByTextSize automatically set the drop width size by reading the widest list option width
dataTest will add a data-test attribute on the .ms-parent and .ms-drop divs for easier E2E testing
useSelectOptionLabel will use the <option label=""> which can be used to display shorter text of selected options.
- example: display
"1,3" as label instead of "January,March" (see demo)
useSelectOptionLabelToHtml similar to useSelectOptionLabel but also renders HTML.
renderOptionLabelAsHtml will render selected options as HTML code (see demo)
sanitizer can be used to sanitize HTML code and prevent XSS cross-site scripting attacks (see demo).
- when none is provided, it will use the Sanitizer API (not supported by Safari)
showOkButton adds an "OK" button at the end of the multiple select option list (see demo)
showSearchClear show a clear filter button on the search filter input (see demo)
diacriticParser custom parser to normalize diacritic symbols when filtering select list (see demo)
darkMode to enable our new Dark Mode Theme (see demo)
infiniteScroll option (see demo)
onFilterClear callback that will be executed when the filter gets cleared (see demo)
onClose(reason) callback that will be executed when the dropdown closes (see demo)
preFilter provide a Filter predicate to pre-filter data (see demo)
preSort provide a Sort Comparer to pre-sort data (see demo)
lazyData provide a function with callback arguments to load data asynchronously (see demo)
CSP Compliance
The library is now CSP (Content Security Policy) compliant, there are however some exceptions to be aware of. When using any HTML strings as template (when using textTemplate, labelTemplate, renderOptionLabelAsHtml or useSelectOptionLabelToHtml), you will not be fully compliant unless you return TrustedHTML. You can achieve this by using the sanitizer method in combo with an external library like DOMPurify (recommended) to return TrustedHTML as shown below and with that in place you will be CSP compliant.
[!NOTE]
The library starting with version 5.2.0 will now default to the Sanitizer API (see default implementation) unless you bring your own logic by overriding the sanitizer option. Please note that the Sanitizer API is supported by Chrome, Edge, Firefox but not yet by Safari (which could be a reason to still use DOMPurify as sanitizer).
import DOMPurify from 'dompurify';
import { multipleSelect, MultipleSelectInstance } from 'multiple-select-vanilla';
const ms1 = multipleSelect('#select1', {
name: 'my-select',
single: false,
useSelectOptionLabelToHtml: true,
sanitizer: (html) => DOMPurify.sanitize(html, { RETURN_TRUSTED_TYPE: true }), // return TrustedHTML Type
data: [
{ text: '<i class="fa fa-star"></i> January', value: 1 },
{ text: '<i class="fa fa-star"></i> February', value: 2 },
]
});
with this code in place, we can now use the following CSP meta tag (which is what we use in the demo index.html)
<meta http-equiv="Content-Security-Policy" content="default-src 'self';style-src 'self' data:; img-src * 'self' data: https:; require-trusted-types-for 'script'; trusted-types dompurify">
[!NOTE]
in our demo we are only adding unsafe-inline simply because we are using Vite (which is not CSP compliant in Dev mode), but the library should work nonetheless without unsafe-inline.
Major Changes
version 3.0
- remove deprecated options
- new Modern Look by using SVG icons
version 4.0
- build ESM-Only and drop CJS (CommonJS) build (aka
require())
version 5.0
- Locale management has been refactored to remove usage of the global
window object. Locales are now provided via a modular registry and injected through options. The multiple-select- prefix has been removed from all locale import paths (single and merged) and no longer exists on the window object.
- Lazy loading API change: The
lazyData option now uses a callback signature: lazyData(resolve, reject) instead of returning a Promise. This allows for more flexible async data loading and error handling. See the demo and documentation for updated usage examples.
Migration Example:
// 1. load every locale individually, it could be imported in 2 ways (named import OR import on window object)
// named import
- import { Spanish } from 'multiple-select-vanilla/dist/locales/multiple-select-es-ES.js';
+ import { Spanish } from 'multiple-select-vanilla/dist/locales/es-ES.js';
// OR default import
- import 'multiple-select-vanilla/dist/locales/multiple-select-es-ES.js';
+ import Spanish from 'multiple-select-vanilla/dist/locales/es-ES.js';
// 2. or load all locales at once
- import 'multiple-select-vanilla/dist/locales/multiple-select-all-locales';
// named import
+ import { locales } from 'multiple-select-vanilla/dist/locales/all-locales.js';
// OR default import
+ import locales from 'multiple-select-vanilla/dist/locales/all-locales.js';
// 3. lazyData option (old vs new)
- lazyData: () => Promise<CollectionData>
+ lazyData: (resolve, reject) => void
See the Example09 for details on dynamic locale loading.
Installation / Structure
The library is now ESM Only, the library does include optional locales that can be loaded through imports or you can use and define your own custom texts as well. Here's the full library structure of the project
dist/
locales/
all-locales.js # all-in-1 locales
..
es-ES.js # Spanish locale
fr-FR.js # French locale
...
styles/ # CSS and SASS Stylings
css/
sass/
index.d.ts # d.ts Type Definitions
index.js # ESM import
Used by
This fork was created mostly to drop jQuery, and it is used by a few other Open Source projects of mine that I also maintain:
Contributions
Pull Request are welcome, feel free to contribute.
Development / Contributions
If you wish to contribute to the project, please follow the steps shown below:
Note: this project uses pnpm workspaces, you can install pnpm by following their installation or use NodeJS corepack enable