From 9197eea0ac0259117f9e9e7b042c91a69a258a1e Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 29 Nov 2022 08:24:14 +0100 Subject: [PATCH] switch/menu/dropdown: add a syntax that supports duplicate labels in `values` property --- CHANGELOG.md | 3 +++ src/client/widgets/basics/dropdown.js | 1 + src/client/widgets/basics/menu-base.js | 21 +++++++++++++++++---- src/client/widgets/basics/menu.js | 1 + src/client/widgets/basics/switch.js | 1 + 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5b71491..ce024c17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/client/widgets/basics/dropdown.js b/src/client/widgets/basics/dropdown.js index a12ce9bc..ead94dd2 100644 --- a/src/client/widgets/basics/dropdown.js +++ b/src/client/widgets/basics/dropdown.js @@ -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.', ]} } }) diff --git a/src/client/widgets/basics/menu-base.js b/src/client/widgets/basics/menu-base.js index c5b2dd61..daacfb1f 100644 --- a/src/client/widgets/basics/menu-base.js +++ b/src/client/widgets/basics/menu-base.js @@ -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') } diff --git a/src/client/widgets/basics/menu.js b/src/client/widgets/basics/menu.js index 166bcd32..c0c0c021 100644 --- a/src/client/widgets/basics/menu.js +++ b/src/client/widgets/basics/menu.js @@ -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`', diff --git a/src/client/widgets/basics/switch.js b/src/client/widgets/basics/switch.js index 735ebc0a..8cafedd4 100644 --- a/src/client/widgets/basics/switch.js +++ b/src/client/widgets/basics/switch.js @@ -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:',