How to make text on toggle-button widget configurable?

I have a working ToggleButton widget, but the texts “On” and “Off” are hardcoded:

uid: ButtonToggle
tags: []
    - context: item
      description: Item to use with this button.
      label: Item
      name: item
      required: true
      type: TEXT
      groupName: general
    - description: Text to show as title.
      label: Title
      name: Title
      required: false
      type: TEXT
    - description: Text to show as footer.
      label: Footer
      name: Footer
      required: false
      type: TEXT
    - description: Text to show on the 'ON' button.
      label: OnButton
      name: OnButton
      required: false
      type: TEXT
    - description: Text to show on the 'OFF' button.
      label: OffButton
      name: OffButton
      required: false
      type: TEXT
    - name: general
      label: Display options
timestamp: Sep 11, 2022, 2:48:26 PM
component: f7-card
  title: '=(props.Title) ? props.Title : ""'
  footer: '=(props.Footer) ? props.Footer : ""'
    min-width: 250px
    - component: f7-block
        class: bog
          display: flex
          flex-wrap: wrap
          justify-content: space-between
          align-content: space-between
          padding: 20px
          - component: oh-button
              outline: false
              action: toggle
              actionItem: =props.item
              actionCommand: ON
              actionCommandAlt: OFF
              color: white
              text: "=(items[props.item].state === 'ON') ? 'On' : 'Off'"
                width: 100px
                background: "=(items[props.item].state === 'ON') ? 'rgb(76,217,100)' : 'rgb(255,59,48)'"

I need to combine the item state with the configured button texts somehow.
So this line
text: “=(items[props.item].state === ‘ON’) ? ‘On’ : ‘Off’”
needs to be something like this
text: “=(items[props.item].state === ‘ON’) ? ‘props.OnButton’ : ‘props.OffButton’”

But this does not work. It only shows the text “props.OnButton” or “props.OffButton”.
I also tried using the ‘content’ property of the style to set the text, but this didn’t set any text at all :frowning:

                width: 100px
                background: "=(items[props.item].state === 'ON') ? 'rgb(76,217,100)' : 'rgb(255,59,48)'"
                content: "=(items[props.item].state === 'ON') ? 'props.OnButton' : 'props.OffButton'"

Any help appreciated!

If you have props.OnButton in single quotes, it will treat it as normal text and display the text.
Remove the single quotes and it will treat this correctly as variable and it will reference to the properties you have set

In addition instead of using props for on/off state, you could define metadata at an item level and better use items[props.item].displayState here

1 Like

Thank you!
This does work
text: “=(items[props.item].state === ‘ON’) ? props.OnButton : props.OffButton”