dropdown/menu: add label property (with support easy key/value display)
dropdown/menu: add `icon` toggle property
This commit is contained in:
parent
fa69dd74e2
commit
9f6bab467c
5 changed files with 49 additions and 4 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue