1.0.12 • Published 2 months ago

@ezmodus/select-picker v1.0.12

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

ezmodus select picker

Improve the select element with this new purely native Javascript written library.


Preface

I made this project, because I was updating one of my projects to Bootstrap 5 and I was using boostrap-select (https://github.com/snapappointments/bootstrap-select) which states "Now with bootstrap 5 support" on it's readme file (April 2022).

Unfortunately I noticed that this wasn't the case even living in 2023. I already started to refactor my project and I didn't want to rollback just because of this. Therefore I decided to write from scratch native JS version and also without bootstrap.

Because maybe someone else may be in the similar situation I decided to publish this to free use.

It works pretty much the same as bootstrap-select, but this also adds few extra features.


Release notes


Examples

Multi select with default search. Limited select count 1 (more picked changes the dropdown text)

Multi select with search

Multi select with disabled options and option with passed extra class with styling.

Multi select with search

Multi select with search and clear. Also some options has data-desc attribute given with text.

Multi select with search, clear and option desc


Install

npm install @ezmodus/select-picker --save

You can use prebuilt javascript and styling or do modifications first by loading from source.

// from node modules
// from dist (prebuilt, minified)
@import '~@ezmodus/select-picker/dist/style.css';

// SCSS
@import '~@ezmodus/select-picker/src/_variables.scss';
// change variable settings here
$ezmodus-dropdown-tick-color: 'red';
// load rest of the styling
@import '~@ezmodus/select-picker/src/style.scss';
// from node modules
// from dist (prebuilt, minified)
import '@ezmodus/select-picker/dist/select';

// from source if you want to modify ezmodusSelectPicker-class
import '@ezmodus/select-picker/src/select';

Usage, defaults and modifiers

Simple just add the class ezmodus-select-picker to select-element.

You can pass any other class to new dropdown button by just adding extra classes to select-element.

<!-- single select -->
<select class="ezmodus-select-picker">...</select>

<!-- multi selection -->
<select class="ezmodus-select-picker" multiple>...</select>

<!-- more settings -->
<!--
Any other class than picker itself in select-element
will be passed to dropdown button as extra class.

Any class given to option will be passed to menu li-element.

If option is set as disabled, then li-element has data-disabled="true",
otherwise the attribute does not exist.

These helps developer even further to manipulate dropdown and items.
-->
<select class="ezmodus-select-picker another-class"
    data-size="6"
    title="Choose your role"
    data-tick="false"
    data-search="true"
    data-search-focus="true"
    data-search-from="both"
    data-search-placeholder="Find..."
    data-search-no-results="No results for {0}"
    data-selected-count="5"
    data-selected-text="{0} selected"
    data-selected-max= "5"
    data-clear-show="true"
    data-clear-text="clear"
    data-menu-item-height=""
     multiple>
    <!-- my option data -->
    <option value="1">Superuser</option>
    <option value="2">Normal user</option>
    <option value="3" data-desc="Special user, this is injected as subtext">
        Test user
    </option>
</select>

These can be modified a lot to give more richer tools to use.

Variables

SELECT ATTRIBUTES

AttributeDefaultDescription
size1How many items are shown when dropdown opens. If select has more items than defined size then show scrollbar. This can also set with data-attribute.
titleSelectText, what to show on when no selection on dropdown
multipleActivates multiple selection support

SELECT DATA ATTRIBUTES

Add these attributes with prefix data-.

AttributeDefaultDescription
sizenullThis is another way to setup how many items are shown when menu opens. This is is preferable way to setup without worrying browser to default render select with size and then hides it. On page load selects' with size may look bigger before transforming them to select picker.
ticktrueShows tick on dropdown.
searchfalseIf 'true' then show search input for list items. Click here to see how search works
search-focustrueIf set, then automatically focus on search on desktop.
search-focus-mobilefalseIf set, then automatically focus on search on touch devices.
search-fromnullSearch from texts, if "values" then search looks values of "option value", if "both" then look for values or texts.
search-placeholderFilter...Placeholder text for search input.
search-no-resultsNo results matched "{0}"When with given string search does not find anything, this is the message to show, {0} is magic and will be replaced with given input.
selected-count1How many items are shown until dropdown text is replaced with "selected-text"
selected-text{0} selectedWhen too many items are selected this is the text shown in dropdown, {0} is magic and replaced with count of selected items.
selected-maxnullInteger, limits how many items are allowed to pick on multi select
clear-showfalseOnly for multiple, but if 'true' then show button before the list to clear/reset existing selections
clear-textclear selectionDefault text for the clear selection button
menu-item-heightnullThis is only for special cases where parent node is display:none. Picker normally uses offsetHeight to determine the height of the menu item. When select is already hidden, it cannot see the value correctly. If this is the case the default height is 31, but if you do custom styling then this is the way to correct it further.
dynamicnullInteger as percent, eg "80", between 0-100. Finds out the longest list item's text content and calculates width and sets new min-width to be 80 percent of that. This is useful to show more beautiful look when options themselves are short, but they have long data-desc value.

SELECT'S OPTION ATTRIBUTE

AttributeDefaultDescription
data-descText data to show as extra description with own styling. If texts are used then size of the menu is direcctive because items heights varies.

Development

Super simple setup

  • Git clone the project
  • Run npm install
  • Run npm run build|watch|production
  • View Demo under demo/index.html
1.0.12

2 months ago

1.0.11

3 months ago

1.0.10

4 months ago

1.0.9

7 months ago

1.0.8

7 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago