dropdown/menu: add label property (with support easy key/value display)

dropdown/menu: add `icon` toggle property
This commit is contained in:
bordun 2020-10-03 18:34:37 +02:00
parent fa69dd74e2
commit 9f6bab467c
5 changed files with 49 additions and 4 deletions

View file

@ -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

View file

@ -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`
<inner>
<div class="text"></div>
<div class="icon"></div>
</inner>
`})
if (this.getProp('icon')) {
this.widget.appendChild(html`<div class="icon"></div>`)
} 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

View file

@ -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

View file

@ -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`
<inner>
<div class="text"></div>
<div class="icon"></div>
</inner>
`})
if (this.getProp('icon')) {
this.widget.appendChild(html`<div class="icon"></div>`)
} 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',

View file

@ -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;