Giter Club home page Giter Club logo

lcweb-ita / lc-select Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 4.0 267 KB

Superlight vanilla javascript plugin, for modern web dropdowns. Supporting multi-options, search and images. Designed to be seamlessly themed

Home Page: https://lcweb.it/lc-select-javascript-plugin

License: MIT License

HTML 9.68% JavaScript 64.04% CSS 26.28%
javascript vanilla-javascript pure-javascript select select2 dropdown multiple-dropdown multilanguage-support es6-javascript

lc-select's Introduction

Superlight vanilla javascript dropdowns by LCweb

Need to jump off of jQuery (or any other) dependency? Other packages are too heavy to just tweak select fields?
LC Select has been created focusing on these aspects: a pure javascript (ES6) plugin offering many features in just 19KB (+ a 4KB theme).

Top features list:

  • sigle 20KB file, no dependencies, 100% pure javascript
  • two themes (light, dark) included. Designed to be themed with no efforts
  • themes mix support using prefixed selectors
  • supports both simple and multiple select fields
  • fully responsive, fits in any width
  • (optional) default placeholder for simple select fields
  • (optional) searchbar with minimum fields threshold
  • (optional) maximum selectable options
  • complete keyboard events integration
  • option images support
  • mobile ready
  • multilanguage ready

Tested on all mordern browsers (don't ask for old IE support please)
For live demos check: https://lcweb.it/lc-select-javascript-plugin


Installation & Usage

  1. include lc_select.min.js

  2. include a theme (eg. themes/light.css)

  3. initialize plugin targeting one/multiple select fields
    NB: first parameter may be a textual selector or a DOM object (yes, also jQuery objects)

<script type="text/javascript>
new lc_select('select');
</script>

Specific HTML attributes

There are two optional attributes you can use to take advantage of plugin functionalities:

  • data-placeholder: to be used on the select HTML tag, sets field placeholder in case of no option selected
    (check also pre_placeh_opt option for simple selects)

  • data-image: to be used on optgroup or option HTML tags, sets option's image

<select name="simple" data-placeholder="Select something please ..">
    <optgroup label="Europe" data-image="demo-img/eu.svg">
        <option value="ita" data-image="demo-img/ita.svg">Italy</option>
        <option value="fra" data-image="demo-img/fra.png">France</option>
        <option value="esp" data-image="demo-img/esp.png">Spain</option>
    </optgroup>
</select>

Options

Here are listed available options with default values

<script type="text/javascript>
new lc_select('select', {

    // (bool) whether to enable fields search
    enable_search : true, 
    
    // (int) minimum options number to show search
    min_for_search : 7,   
    
    // (bool) whether to automatically focus search field on desktop (NB: will break tabindex chain)
    autofocus_search: false,
    
    // (string) defines the wrapper width: "auto" to leave it up to CSS, "inherit" 
    // to statically copy input field width, or any other CSS sizing 
    wrap_width : 'auto',
    
    // (array) custom classes assigned to the field wrapper (.lcslt-wrap) and 
    // dropdown (#lc-select-dd)
    addit_classes : [], 
    
    // (bool) if true, on simple dropdowns without a selected value, prepend 
    // an empty option using placeholder text
    pre_placeh_opt : false, 
    
    // (int|false) defining maximum selectable options for multi-select
    max_opts : false, 
    
    // (function) triggered every time field value changes. Passes value and 
    // target field object as parameters
    on_change : null, // function(new_value, target_field) {},

    // (array) option used to translate script texts
    labels : [ 
        'search options',
        'add options',
        'Select options ..',
        '.. no matching options ..',
    ],
});
</script>

Mixing themes

The easiest and cleanest way to mix themes on the same webpage is

  1. using addit_classes option to specify the theme prefix

  2. include prefixed themes version (there are prefixed versions of dark/ligh themes in the "themes" folder)

<link href="themes/light_prefixed.css" rel="stylesheet" type="text/css">
<link href="themes/dark_prefixed.css" rel="stylesheet" type="text/css">

<script type="text/javascript>
new lc_select('.select-class-light', {
    addit_classes : ['lcslt-light']
});

new lc_select('.select-class-dark', {
    addit_classes : ['lcslt-dark']
});
</script>

Public Events

Alternatively to the "on_change" option, you can use the native "change" element event to track field changes

<script type="text/javascript>
document.querySelectorAll('select').forEach(function(el) {

    el.addEventListener('change', ...);
});
</script>

Extras

There are two extra events you can trigger on initialized elements:

  • lc-select-refresh: re-sync select field options and status with plugin instance (eg. when new fields are dynamically added)

  • lc-select-destroy: remove plugn instance, coming back to HTML select field

<script type="text/javascript>
const select = document.querySelector('select');

// initialize
new lc_select(select);

// re-sync
const resyncEvent = new Event('lc-select-refresh');
select.dispatchEvent(resyncEvent);

// destroy
const destroyEvent = new Event('lc-select-destroy');
select.dispatchEvent(destroyEvent);
</script>

Copyright © Luca Montanari - LCweb

lc-select's People

Contributors

lcweb-ita avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

lc-select's Issues

suggest: remove ".." for input search placeholder

I think your plugin would be more flexible if you remove double ".." form line 565:

'<input type="text" name="lcslt-search" value="" placeholder="'+ options.labels[0] +'" autocomplete="off" />' +

So you leave to dev the choice to use or not to use dots in the placeholder text. For example in Italy we use three dots, not two.

cheers

Option to set value via code?

Hi Luca,

I love LC-select and thinking about an option to set the selected value(s) via code. Do you plan something like that?

Greets
Kristof

Sort order for generated select list

Excellent plugin! The list generated by the plugin appears to be sorted based on the option's value rather than its text content. In my use case these are not necessarily the same (eg I have an ID for the option value and a label for the text) can the list be sorted/sortable by the text value instead? An example would be:

<option value="46176">Accidents, emergency response &amp; poison centres</option>
<option value="18938">Alternatives assessment &amp; substitution</option>
<option value="81484">Animal testing</option>

transfering custom data attributes

Hi, i am using your plugin on one of my projects.
I used to use a styled UL with custom JS code to achieve a similar effect but I was missing accessibility and arrows to navigate the options so I am migrating to LC-select.
In my original code, I have nested options ( think of a folder subtree)... for that and in order to style, I was giving a data-indentation attribute to each li.
When using your plugin, this custom attribute is understandably, not transferred to the elements.
Is there a way to achieve this already?
Would you be interested in adding it?

Thanks in advance...

bug: js error on resize

Hi,

when a select is opened, if you resize the page, it closes correctly but there is a js error in console when you try to reopen it.

But a part of that I love your plugin... good job.

Schermata a 2022-10-16 18-27-04

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.