From ddc6a992d8d5f6cb0ddc38a18377cdf79a04f3be Mon Sep 17 00:00:00 2001 From: bordun Date: Sat, 3 Oct 2020 18:34:37 +0200 Subject: [PATCH] dropdown/menu: add `label` property (with support easy key/value display) dropdown/menu: add `icon` toggle property --- CHANGELOG.md | 2 ++ src/client/widgets/basics/dropdown.js | 14 ++++++++++++-- src/client/widgets/basics/menu-base.js | 14 ++++++++++++++ src/client/widgets/basics/menu.js | 16 ++++++++++++++-- src/scss/widgets/basics.scss | 7 +++++++ 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bfb14f4..505a32d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ - remove `colorBg` for all widgets except containers - widgets background color is now transparent by default (can be overridden with css) - keyboard: added `colorBlack` and `colorWhite` properties to customize key colors + - dropdown/menu: add `label` property (with support easy key/value display) + - dropdown/menu: add `icon` toggle property ## 1.4.1 diff --git a/src/client/widgets/basics/dropdown.js b/src/client/widgets/basics/dropdown.js index d8389203..8735cce0 100644 --- a/src/client/widgets/basics/dropdown.js +++ b/src/client/widgets/basics/dropdown.js @@ -16,6 +16,8 @@ class Dropdown extends MenuBase { return super.defaults().extend({ style: { _separator_dropdown_style: 'Dropdown style', + label: {type: 'string|boolean', value: 'auto', help: 'Displayed text (defaults to current value). Keywords `%key` and `%value` will be replaced by the widget\'s selected key/value.'}, + icon: {type: 'boolean', value: 'true', help: 'Set to `false` to hide the dropdown icon'}, align: {type: 'string', value: 'center', choices: ['center', 'left', 'right'], help: 'Set to `left` or `right` to change text alignment (otherwise center)'}, }, class_specific: { @@ -33,10 +35,16 @@ class Dropdown extends MenuBase { super({...options, html: html`
-
+ `}) + if (this.getProp('icon')) { + this.widget.appendChild(html`
`) + } else { + this.container.classList.add('no-icon') + } + if (this.getProp('align') === 'left') this.widget.classList.add('left') if (this.getProp('align') === 'right') this.widget.classList.add('right') @@ -50,6 +58,7 @@ class Dropdown extends MenuBase { }) this.select.selectedIndex = -1 + this.selected = -1 this.container.classList.add('noselect') } @@ -76,8 +85,9 @@ class Dropdown extends MenuBase { this.value = this.values[i] if (!options.fromLocal) this.select.selectedIndex = i + this.selected = i - this.text.textContent = i === -1 ? '' : this.select.options[i].textContent + this.setLabel() if (document.activeElement === this.select && iOS) { // force menu close on ios diff --git a/src/client/widgets/basics/menu-base.js b/src/client/widgets/basics/menu-base.js index 4f5b9e73..bee59a06 100644 --- a/src/client/widgets/basics/menu-base.js +++ b/src/client/widgets/basics/menu-base.js @@ -1,4 +1,5 @@ var Widget = require('../common/widget'), + {iconify} = require('../../ui/utils'), {deepEqual} = require('../../utils') class MenuBase extends Widget { @@ -43,6 +44,19 @@ class MenuBase extends Widget { } + setLabel() { + + var string = this.getProp('label') === 'auto' ? '%value' : this.getProp('label'), + i = this.selected + + if (i > -1 && string) { + this.text.innerHTML = iconify(string.replace(/%value/g, JSON.stringify(this.value)).replace(/%key/g, this.keys[i])) + } else { + this.text.innerHTML = '' + } + + } + } module.exports = MenuBase diff --git a/src/client/widgets/basics/menu.js b/src/client/widgets/basics/menu.js index a79c5f92..9aaf17ac 100644 --- a/src/client/widgets/basics/menu.js +++ b/src/client/widgets/basics/menu.js @@ -24,6 +24,8 @@ class Menu extends MenuBase { }, style: { _separator_menu_style: 'Menu style', + label: {type: 'string|boolean', value: 'auto', help: 'Displayed text (defaults to current value). Keywords `%key` and `%value` will be replaced by the widget\'s selected key/value.'}, + icon: {type: 'boolean', value: 'true', help: 'Set to `false` to hide the dropdown icon'}, textAlign: {type: 'string', value: 'center', choices: ['center', 'left', 'right'], help: 'Set to `left` or `right` to change text alignment (otherwise center)'}, menuAlignV: {type: 'string', value: 'center', choices: ['center', 'top', 'bottom'], help: 'Set to `top` or `bottom` to change menu alignment (otherwise center)'}, menuAlignH: {type: 'string', value: 'center', choices: ['center', 'left', 'right'], help: 'Set to `left` or `right` to change menu alignment (otherwise center)'}, @@ -53,11 +55,16 @@ class Menu extends MenuBase { super({...options, html: html`
-
`}) + if (this.getProp('icon')) { + this.widget.appendChild(html`
`) + } else { + this.container.classList.add('no-icon') + } + this.widget.classList.add('text-align-' + this.getProp('textAlign')) this.widget.classList.add('menu-align-h-' + this.getProp('menuAlignH')) this.widget.classList.add('menu-align-v-' + this.getProp('menuAlignV')) @@ -295,7 +302,8 @@ class Menu extends MenuBase { this.selected = i DOM.each(this.menu, '.on', (el)=>{el.classList.remove('on')}) DOM.get(this.menu, '.item')[i].classList.add('on') - this.text.innerHTML = iconify(this.keys[i]) + + this.setLabel() } @@ -311,6 +319,9 @@ class Menu extends MenuBase { switch (propName) { + case 'label': + this.setLabel() + return case 'size': this.setSize() return @@ -346,6 +357,7 @@ class Menu extends MenuBase { } Menu.dynamicProps = Menu.prototype.constructor.dynamicProps.concat( + 'label', 'size', 'layout', 'columns', diff --git a/src/scss/widgets/basics.scss b/src/scss/widgets/basics.scss index 97049c10..3bf55174 100644 --- a/src/scss/widgets/basics.scss +++ b/src/scss/widgets/basics.scss @@ -171,6 +171,10 @@ --widget-padding: 1rem; --text-padding: calc(var(--widget-padding) + 8rem); + &.no-icon { + --text-padding: var(--widget-padding); + } + // z-index: initial!important; // contain: style size; @@ -434,6 +438,9 @@ --grid-columns: 2; overflow: hidden; + &.no-icon { + --text-padding: var(--widget-padding); + } &.on { z-index: 9999999999999!important;