switch/menu/dropdown: add a syntax that supports duplicate labels in values property

This commit is contained in:
jean-emmanuel 2022-11-29 08:24:14 +01:00
parent e7aa598f8b
commit 9197eea0ac
5 changed files with 23 additions and 4 deletions

View file

@ -8,6 +8,9 @@
- editor
- horizontal scrolling issues with maximized code editor
- widgets
- switch/menu/dropdown: add a syntax that supports duplicate labels in `values` property
## 1.20.0
- bug fixes

View file

@ -24,6 +24,7 @@ class Dropdown extends MenuBase {
values: {type: 'array|object', value: {'Value 1':1,'Value 2':2}, help: [
'`Array` of possible values to switch between : `[1,2,3]`',
'`Object` of label:value pairs. Numeric labels must be prepended or appended with a white space (or any other non-numeric character) otherwise the order of the values won\'t be kept',
'`{"labels": [], "values": []}` `object` where `labels` and `values` arrays must be of the same length. This syntax allows using the same label for different values.',
]}
}
})

View file

@ -22,12 +22,25 @@ class MenuBase extends Widget {
var values = this.getProp('values') || []
if (!Array.isArray(values) && !(typeof values === 'object' && values !== null)) {
values = values !== '' ? [values] : []
if (
typeof values === 'object' && values !== null &&
Array.isArray(values.labels) && Array.isArray(values.values) &&
Object.keys(values).length === 2 && values.labels.length === values.values.length
) {
this.values = values.values
this.keys = values.labels
} else {
if (!Array.isArray(values) && !(typeof values === 'object' && values !== null)) {
values = values !== '' ? [values] : []
}
this.values = !Array.isArray(values) ? Object.values(values) : values
this.keys = !Array.isArray(values) ? Object.keys(values) : this.values
}
this.values = !Array.isArray(values) ? Object.values(values) : values
this.keys = !Array.isArray(values) ? Object.keys(values) : this.values
this.objectInValues = this.values.some(x=>typeof x === 'object')
}

View file

@ -43,6 +43,7 @@ class Menu extends MenuBase {
values: {type: 'array|object', value: [1, 2, 3], help: [
'`Array` of possible values to switch between : `[1,2,3]`',
'`Object` of label:value pairs. Numeric labels must be prepended or appended with a white space (or any other non-numeric character) otherwise the order of the values won\'t be kept',
'`{"labels": [], "values": []}` `object` where `labels` and `values` arrays must be of the same length. This syntax allows using the same label for different values.',
]},
weights: {type: 'array', value: '', help: [
'`Array` of `number` defining the weights of each value in `values`',

View file

@ -27,6 +27,7 @@ class Switch extends MenuBase {
values: {type: 'array|object', value: {'Value 1':1,'Value 2':2}, help: [
'`Array` of possible values to switch between : `[1,2,3]`',
'`Object` of `"label":value` pairs. Numeric labels must be prepended or appended with a white space (or any other non-numeric character) otherwise the order of the values won\'t be kept',
'`{"labels": [], "values": []}` `object` where `labels` and `values` arrays must be of the same length. This syntax allows using the same label for different values.',
]},
mode: {type: 'string', value: 'tap', choices: ['tap', 'slide', 'click', 'flip'], help: [
'Interaction mode:',