Date: Fri, 12 Apr 2024 11:53:35 +0700
Subject: [PATCH 14/15] feat(multi-select): refactor current inner html
---
src/_index.js | 6 +++---
src/data.js | 10 +++++----
src/layout.js | 56 ++++++++++++++++++++++++++-------------------------
3 files changed, 38 insertions(+), 34 deletions(-)
diff --git a/src/_index.js b/src/_index.js
index e035ba5..9a1babf 100644
--- a/src/_index.js
+++ b/src/_index.js
@@ -1,6 +1,6 @@
import {getSelectData, val} from "./data";
import {fireOnChangeEvent, init} from "./methods";
-import {getOptionHTML, updateDropdownHTML} from "./layout";
+import {getCurrentInnerHTML, getOptionHTML, updateDropdownHTML} from "./layout";
import {findObjectInArray, getOptionByValue, getSelectTag} from "./utils";
import {EventsManager, getOptionsFromAttribute} from "@phucbm/os-util";
import {CLASSES, ATTRS, DEFAULTS} from './configs'
@@ -144,7 +144,7 @@ class EasySelect{
if(this.selectTagData.length){
// update current
- this.current.innerHTML = getOptionHTML(this);
+ this.current.innerHTML = getCurrentInnerHTML(this);
// if not native select
if(!this.options.nativeSelect){
@@ -256,7 +256,7 @@ class EasySelect{
if(this.isDisabled) return;
// update current HTML
- this.current.innerHTML = getOptionHTML(this);
+ this.current.innerHTML = getCurrentInnerHTML(this);
const newValue = val(this);
const newValueArray = val(this, 'array');
diff --git a/src/data.js b/src/data.js
index d831258..2cdf304 100644
--- a/src/data.js
+++ b/src/data.js
@@ -47,7 +47,9 @@ export function getSelectData(context){
/**
* Get option data
- * @returns {{isSelected: boolean, index: *, id: string, label: *, value: (*|string|number|string[])}}
+ * @param context
+ * @param option
+ * @returns {{el: *, isSelected: *, index: *, id: string, label: string, isDisabled: (*|(() => boolean)|((setting: string) => boolean)|string|boolean), value: *}}
*/
export function getOptionData(context, option = undefined){
if(typeof option === 'undefined'){
@@ -55,13 +57,13 @@ export function getOptionData(context, option = undefined){
option = getSelectedOption(context.selectTag);
}
- const label = option.innerText;
- const value = option.value;
+ const label = option?.innerText;
+ const value = option?.value;
const index = getIndex(option);
const id = stringToSlug(value) + '-' + index;
const isSelected = val(context, 'array').includes(value); // tested with multi select
const el = option;
- const isDisabled = option.disabled;
+ const isDisabled = option?.disabled;
return {id, label, value, isSelected, isDisabled, index, el};
}
\ No newline at end of file
diff --git a/src/layout.js b/src/layout.js
index dff8436..1f41e65 100644
--- a/src/layout.js
+++ b/src/layout.js
@@ -11,10 +11,30 @@ import {CLASSES, ATTRS} from "./configs"
* @returns {string}
*/
export function getCurrentHTML(context){
+ return `${getCurrentInnerHTML(context)}
`;
+}
+
+export function getCurrentInnerHTML(context){
let html = '';
- html += ``;
- html += getOptionHTML(context);
- html += `
`;
+
+ if(context.options.multiple){
+ // current multiple
+ const selectedValues = val(context, 'array');
+ const labels = [];
+ selectedValues.forEach(value => {
+ const option = getOptionData(context, getOptionByValue(context, value));
+ labels.push(option.label);
+ });
+ html += ``;
+ html += ``;
+ html += labels.join(', ');
+ html += ``;
+ html += `
`;
+ }else{
+ // current single
+ html += getOptionHTML(context);
+ }
+
return html;
}
@@ -75,9 +95,6 @@ export function getDropdownHTML(context){
* @returns {string}
*/
export function getOptionHTML(context, option = undefined){
- // is current of multiple select
- const isMultipleCurrent = typeof option === 'undefined' && context.options.multiple;
-
// is active
// tested with multi select
const isActive = typeof option !== 'undefined' && val(context, 'array').includes(option['value']);
@@ -93,7 +110,7 @@ export function getOptionHTML(context, option = undefined){
let html = '';
html += ``;
- html += getOptionInnerHTML(context, option, isMultipleCurrent);
+ html += getOptionInnerHTML(context, option);
html += `
`;
return html;
}
@@ -102,35 +119,20 @@ export function getOptionHTML(context, option = undefined){
* Option inner HTML
* @param context
* @param option
- * @param isMultipleCurrent
* @returns {string}
*/
-export function getOptionInnerHTML(context, option, isMultipleCurrent = false){
+export function getOptionInnerHTML(context, option){
// return custom HTML if any
- let customHTML = context.options.customDropDownOptionHTML(option, isMultipleCurrent);
+ let customHTML = context.options.customDropDownOptionHTML(option);
if(customHTML) return customHTML;
-
let html = '';
// multiple select
if(context.options.multiple){
- if(isMultipleCurrent){
- // current
- const selectedValues = val(context, 'array');
- const labels = [];
- selectedValues.forEach(value => {
- const option = getOptionData(context, getOptionByValue(context, value));
- labels.push(option.label);
- });
- html += ``;
- html += labels.join(', ');
- html += ``;
- }else{
- // option
- html += ``;
- html += `${option['label']}`;
- }
+ // option
+ html += ``;
+ html += `${option['label']}`;
return html;
}
From 2a716f43f45cf89dd409651a703a8ff4ab08d474 Mon Sep 17 00:00:00 2001
From: phucbm
Date: Fri, 12 Apr 2024 13:48:30 +0700
Subject: [PATCH 15/15] feat(multi-select): use multipleLabel for empty value
---
src/layout.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/layout.js b/src/layout.js
index 1f41e65..37713df 100644
--- a/src/layout.js
+++ b/src/layout.js
@@ -1,4 +1,4 @@
-import {getOptionByValue} from "@/utils";
+import {getOptionByValue} from "./utils";
import {getOptionData, val} from "./data";
import {CLASSES, ATTRS} from "./configs"
@@ -27,7 +27,7 @@ export function getCurrentInnerHTML(context){
});
html += ``;
html += ``;
- html += labels.join(', ');
+ html += labels.length ? labels.join(', ') : context.options.multipleLabel;
html += ``;
html += `
`;
}else{