From 9763bb068004515d9e58d17b414103a3e75a8a1d Mon Sep 17 00:00:00 2001 From: EXayer Date: Sat, 30 Oct 2021 15:10:43 +0300 Subject: [PATCH] Added ability using icons instead of images in options. --- README.md | 8 +++++--- lc_select.js | 28 ++++++++++++++++++++-------- lc_select.min.js | 28 +--------------------------- themes/dark.css | 7 ++++++- themes/dark_prefixed.css | 7 ++++++- themes/light.css | 7 ++++++- themes/light_prefixed.css | 7 ++++++- 7 files changed, 50 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index b5d70f3..4f674a8 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ LC Select has been created focusing on these aspects: a pure javascript (ES6) pl - (optional) searchbar with minimum fields threshold - (optional) maximum selectable options - complete keyboard events integration -- option images support +- option images, icons support - mobile ready - multilanguage ready @@ -50,18 +50,20 @@ new lc_select('select'); ## Specific HTML attributes -There are two optional attributes you can use to take advantage of plugin functionalities: +There are three 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 +- **data-icon:** to be used on *optgroup* or *option* HTML tags, sets option's icon (has priority over data-image) + ``` ``` diff --git a/lc_select.js b/lc_select.js index 076730a..04a8f1a 100644 --- a/lc_select.js +++ b/lc_select.js @@ -378,14 +378,16 @@ select.querySelectorAll('option').forEach(opt => { if(opt.selected) { - const img = (opt.hasAttribute('data-image')) ? '' : ''; - + const img = (opt.hasAttribute('data-image')) ? '' : '', + icon = opt.hasAttribute('data-icon') ? '' : ''; + + const image = icon.length ? icon : img; if(is_multiple) { - code += '
'+ img + opt.innerHTML +'
'; + code += '
'+ image + opt.innerHTML +'
'; } else { - const single_placeh_mode = (options.pre_placeh_opt && opt.hasAttribute('data-lcslt-placeh')) ? 'class="lcslt-placeholder"' : ''; - code = ''+ img + opt.innerHTML +''; + const single_placeh_mode = (options.pre_placeh_opt && opt.hasAttribute('data-lcslt-placeh')) ? 'class="lcslt-placeholder"' : ''; + code = ''+ image + opt.innerHTML +''; } sel_opts++; @@ -501,6 +503,7 @@ group_name : [map] { opt_val : { img : (string) , + icon : (string) , name : (string), selected: (bool), disabled: (bool) @@ -530,6 +533,7 @@ let obj = { img : (opt.hasAttribute('data-image')) ? opt.getAttribute('data-image').trim() : '', + icon : (opt.hasAttribute('data-icon')) ? opt.getAttribute('data-icon').trim() : '', name : opt.innerHTML, selected: opt.selected, disabled: opt.disabled, @@ -577,10 +581,16 @@ const dis_class = (disabled_groups.indexOf(group) !== -1) ? 'lcslt-disabled': ''; const optgroup = select.querySelector('optgroup[label="'+ group_key +'"]'), - img = (optgroup.hasAttribute('data-image') && optgroup.getAttribute('data-image')) ? '' : ''; + img = (optgroup.hasAttribute('data-image') && optgroup.getAttribute('data-image')) + ? '' + : '', + icon = (optgroup.hasAttribute('data-icon') && optgroup.getAttribute('data-icon')) + ? '' : ''; + + const image = icon.length ? icon : img; code += - '
  • '+ img + group_key +'' + + '
  • '+ image + group_key +'' + '