From 59658bc0abab4562aba721c0974238e8e224a60b Mon Sep 17 00:00:00 2001 From: bartbutenaers Date: Wed, 25 Sep 2024 21:31:10 +0100 Subject: [PATCH 1/3] button group status --- nodes/widgets/ui_button_group.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/nodes/widgets/ui_button_group.js b/nodes/widgets/ui_button_group.js index ad3849719..f94b16d01 100644 --- a/nodes/widgets/ui_button_group.js +++ b/nodes/widgets/ui_button_group.js @@ -9,9 +9,30 @@ module.exports = function (RED) { // which group are we rendering this widget const group = RED.nodes.getNode(config.group) + function findOptionByValue(val) { + const opt = config.options?.find(option => { + if (typeof (val) === 'object') { + return (JSON.stringify(val) === JSON.stringify(option.value)) + } else { + return option.value == val + } + }) + if (opt) { + return opt + } + return null + } + const evts = { onChange: true, beforeSend: function (msg) { + if (typeof msg.payload !== 'undefined') { + const option = findOptionByValue(msg.payload) + if (option) { + node.status({fill: 'blue', shape: 'dot', text: option.label}) + } + } + if (msg.ui_update) { const update = msg.ui_update if (typeof update.options !== 'undefined') { From be9e245711813a26b9a33af2425d8a2b8c54d77e Mon Sep 17 00:00:00 2001 From: bartbutenaers Date: Thu, 26 Sep 2024 16:57:01 +0100 Subject: [PATCH 2/3] button group lint fixes --- nodes/widgets/ui_button_group.js | 7 ++++--- ui/src/widgets/ui-button-group/UIButtonGroup.vue | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/nodes/widgets/ui_button_group.js b/nodes/widgets/ui_button_group.js index f94b16d01..41f43a2de 100644 --- a/nodes/widgets/ui_button_group.js +++ b/nodes/widgets/ui_button_group.js @@ -9,12 +9,13 @@ module.exports = function (RED) { // which group are we rendering this widget const group = RED.nodes.getNode(config.group) - function findOptionByValue(val) { + // Keep the code of this function in sync with the client-side function + function findOptionByValue (val) { const opt = config.options?.find(option => { if (typeof (val) === 'object') { return (JSON.stringify(val) === JSON.stringify(option.value)) } else { - return option.value == val + return option.value === val } }) if (opt) { @@ -29,7 +30,7 @@ module.exports = function (RED) { if (typeof msg.payload !== 'undefined') { const option = findOptionByValue(msg.payload) if (option) { - node.status({fill: 'blue', shape: 'dot', text: option.label}) + node.status({ fill: 'blue', shape: 'dot', text: option.label }) } } diff --git a/ui/src/widgets/ui-button-group/UIButtonGroup.vue b/ui/src/widgets/ui-button-group/UIButtonGroup.vue index 5aaeab2ac..f33a5f89d 100644 --- a/ui/src/widgets/ui-button-group/UIButtonGroup.vue +++ b/ui/src/widgets/ui-button-group/UIButtonGroup.vue @@ -118,6 +118,7 @@ export default { this.$socket.emit('widget-change', this.id, value) } }, + // Keep the code of this function in sync with the server-side function findOptionByValue: function (val) { const opt = this.options?.find(option => { if (typeof (val) === 'object') { From a8aa69b6617d64ec67d2d3bffd401f28b61feb66 Mon Sep 17 00:00:00 2001 From: bartbutenaers Date: Tue, 1 Oct 2024 20:38:56 +0200 Subject: [PATCH 3/3] Show value when no label available Co-authored-by: Joe Pavitt <99246719+joepavitt@users.noreply.github.com> --- nodes/widgets/ui_button_group.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodes/widgets/ui_button_group.js b/nodes/widgets/ui_button_group.js index 41f43a2de..14ba6d1ef 100644 --- a/nodes/widgets/ui_button_group.js +++ b/nodes/widgets/ui_button_group.js @@ -30,7 +30,7 @@ module.exports = function (RED) { if (typeof msg.payload !== 'undefined') { const option = findOptionByValue(msg.payload) if (option) { - node.status({ fill: 'blue', shape: 'dot', text: option.label }) + node.status({ fill: 'blue', shape: 'dot', text: option.label || option.value }) } }