Elegant Multi-Select Component With Autocomplete – SelectPure

Category: Form , Javascript , Recommended | May 8, 2018
Author: maksymddd
Views Total: 2,641
Official Page: Go to website
Last Update: May 8, 2018
License: MIT

Preview:

Elegant Multi-Select Component With Autocomplete – SelectPure

Description:

SelectPure is a pure JavaScript (es6) library to create elegant single or multiple select controls with support for autocomplete and dynamic data rendering.

Also can be used as a tag & token management tool for your web app.

How to use it:

Install the SelectPure.

# Yarn
$ yarn add select-pure

# NPM
$ npm install select-pure --save

Import the SelectPure library.

import SelectPure from "select-pure";

Or directly include the bundled JavaScript on the webpage.

<script src="bundle.min.js"></script>

Create a container in which you want to render the select control.

<span class="example"></span>

Define an array of options for the select.

const myOptions = [
  {
    label: "New York",
    value: "NY",
  },
  {
    label: "Washington",
    value: "WA",
  },
  {
    label: "California",
    value: "CA",
  },
  {
    label: "New Jersey",
    value: "NJ",
  },
  {
    label: "North Carolina",
    value: "NC",
  },
],

Create a new select control inside the container element you just created.

var instance = new SelectPure(".example", {
    options: myOptions
});

Enable the Multi-Select functionality.

var instance = new SelectPure(".example", {
    options: myOptions,
    multiple: true // default: false
});

Enable the Autocomplete functionality.

var instance = new SelectPure(".example", {
    options: myOptions,
    autocomplete: true // default: false
});

Set the pre-selected value(s).

var instance = new SelectPure(".example", {
    options: myOptions,
    value: ["NY", "CA"]
});

Customize the icon displayed in the selected options.

var instance = new SelectPure(".example", {
    options: myOptions,
    icon: "fa fa-times" // uses Font Awesome
});

Execute a callback function on change.

var instance = new SelectPure(".example", {
    options: myOptions,
    onChange: value => { console.log(value); }
});

The default CSS styles.

.select-wrapper {
  margin: auto;
  max-width: 600px;
  width: calc(100% - 40px);
}

.select-pure__select {
  align-items: center;
  background: #f9f9f8;
  border-radius: 4px;
  border: 1px solid rgba(0, 0, 0, 0.15);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
  box-sizing: border-box;
  color: #363b3e;
  cursor: pointer;
  display: flex;
  font-size: 16px;
  font-weight: 500;
  justify-content: left;
  min-height: 44px;
  padding: 5px 10px;
  position: relative;
  transition: 0.2s;
  width: 100%;
}

.select-pure__options {
  border-radius: 4px;
  border: 1px solid rgba(0, 0, 0, 0.15);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
  box-sizing: border-box;
  color: #363b3e;
  display: none;
  left: 0;
  max-height: 221px;
  overflow-y: scroll;
  position: absolute;
  top: 50px;
  width: 100%;
  z-index: 5;
}

.select-pure__select--opened .select-pure__options {
  display: block;
}

.select-pure__option {
  background: #fff;
  border-bottom: 1px solid #e4e4e4;
  box-sizing: border-box;
  height: 44px;
  line-height: 25px;
  padding: 10px;
}

.select-pure__option--selected {
  color: #e4e4e4;
  cursor: initial;
  pointer-events: none;
}

.select-pure__option--hidden {
  display: none;
}

.select-pure__selected-label {
  background: #5e6264;
  border-radius: 4px;
  color: #fff;
  cursor: initial;
  display: inline-block;
  margin: 5px 10px 5px 0;
  padding: 3px 7px;
}

.select-pure__selected-label:last-of-type {
  margin-right: 0;
}

.select-pure__selected-label i {
  cursor: pointer;
  display: inline-block;
  margin-left: 7px;
}

.select-pure__selected-label i:hover {
  color: #e4e4e4;
}

.select-pure__autocomplete {
  background: #f9f9f8;
  border-bottom: 1px solid #e4e4e4;
  border-left: none;
  border-right: none;
  border-top: none;
  box-sizing: border-box;
  font-size: 16px;
  outline: none;
  padding: 10px;
  width: 100%;
}

You Might Be Interested In:


3 thoughts on “Elegant Multi-Select Component With Autocomplete – SelectPure

  1. tom

    selectpure.min.js:1 Uncaught TypeError: Cannot read property ‘map’ of undefined
    at Object._setValue (selectpure.min.js:1)
    at new (selectpure.min.js:1)
    at HTMLDocument. (grid.js:53)
    _setValue @ selectpure.min.js:1
    (anonymous) @ selectpure.min.js:1
    (anonymous) @ grid.js:53

    Reply
  2. rgreed

    Found a bug on mozilla:

    After deleting the last entry in multi select, text is highlited.

    fix adding:

    .select-pure__option::-moz-selection{
    color: inherit;
    }

    to your css

    Reply

Leave a Reply