0.0.10 • Published 4 years ago

ts-multiselect v0.0.10

Weekly downloads
22
License
MIT
Repository
gitlab
Last release
4 years ago

Ts-Multiselect

This is a very basic, lightweight dropdown with multi (and single) selection written in typescript (no jQuery).

Examples: StackBlitz

Installation

  • Add package with:
npm install ts-multiselect
  • Add styles to your global styles file:
@import "~ts-multiselect/dist/style.css";

Usage

  • Create an element and give him unique id:
<div id="my-multiselect"></div>
  • Instantiate Multiselect class and pass config:
import {Multiselect} from 'ts-multiselect';

const myMultiselect = new Multiselect({
  id: 'my-multiselect',
  options: [
    {
      value: 1,
      label: 'Awesome,
    },
  ],
});
  • Simply get selected items by:
console.log(myMultiselect.selected);

Config

Inputs:

FieldTypeDescriptionRequiredDefault
idstringUnique id for element where multiselect will be renderedYes
optionsIMultiselectOption[]An array of objects (see IMultiselectOption section)Yes[]
multiplebooleanAllow single or multiple selectionNotrue
singularNominativeLabelstringLabel for one selected item (see Language section)No
pluralNominativeLabelstringLabel for 2-4 selected items (see Language section)No
pluralGenitiveLabelstringLabel for 5+ selected items (see Language section)No
placeholderstringDisplayed in selection container when no item is selectedNo
headerLabelstringDisplayed above selection container (aka label)No
selectedany[]Default selection. Values should be the same as value field in optionsNo[]

Outputs:

FieldParametersDescriptionRequired
onDropdownOpen() => anyEmits event when dropdown opensNo
onDropdownClose(selectedItems: any[]) => anyEmits event when dropdown closesNo
onSelectionChange(selectedItems: any[]) => anyEmits event when selection is changedNo

Methods and properties:

FieldDescription
init()It creates elements and appends it to container. Ypu dont have to call it after class instantiate.
destroy()Removes all elements from DOM and all listeners. If you want to create the same multiselect just call init() method .
reset()Clear selection and show placeholder (if provided).
hide()Clear DOM but leave all references and current selection. You can simply render multiselect after hiding and it will looks like never hidden.
render()Show multiselect after hiding.
selectedGet selected items.
renderedDetermine if multiselect is rendered.

IMultiselectOption interface

Simply pass two fields to make an option:

FieldTypeDescriptionRequired
valueany (but preferred number or string due to initial select comparison)Value which will be passed in selected array.Yes
labelstringLabel to show in dropdown or in selection-containerYes

Language

When item is selected (single item in single mode or one item in a multiple mode) it will display an item label in selection container.

When 2 items (only in a multiple mode) are selected you will see 2 items in selection container by default. You can add plural forms in options (in Polish it would be for example):

singularNominativeLabel: 'porcja', // 1 porcja
pluralNominativeLabel: 'porcje', // 2 porcje
pluralGenitiveLabel: 'porcji', // 5 porcji

If it does not affect your language, you can leave these fields empty.

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago