question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Groups of radioboxes: Not all Python-Objects updated correctly?

See original GitHub issue

I am currently implementing an interface where I want to place a group of radioboxes (<input type='radio' ...>) where an user can select one-of-many.

I’ve started by copying the Checkbox from lona.html.data_binding_inputs to get the following:

class Radiobox(CheckBox):
    INPUT_ATTRIBUTE_NAME = 'checked'
    ATTRIBUTES = {
        'type': 'radio',
    }

    def __init__(self, value=False, disabled=False, readonly=False,
                 bubble_up=False, **kwargs):
        super().__init__(value, disabled, readonly, bubble_up, **kwargs)
        if 'input_delay' not in kwargs:
            self.events = [CHANGE]

    @property
    def value(self) -> bool:
        value = self.attributes.get(self.INPUT_ATTRIBUTE_NAME, False)
        if value == '':  # is possible after parsing HTML string
            return True
        return bool(value)

    @value.setter
    def value(self, new_value: bool) -> None:
        if not isinstance(new_value, bool):
            raise TypeError('value is a boolean property')
        self.attributes[self.INPUT_ATTRIBUTE_NAME] = new_value

Next step was to create a group of radioboxes like this:

Div(
    Radiobox(_id=1, name='groupname'),
    Radiobox(_id=2, name='groupname', checked=True),
)

When toggling the radiobox in the frontend to the other one I end up with both nodes on the python-side having checked=True. It seems as the other Radiobox does not emit an input_event to update it’s state.

On a high level I would expect both Radoboxes to update at the same time. What is the expected behavior here?

Workaround: My plan was to have Radiogroup-Widget anyway. So I have a single object that I can query for the current value and that creates the Radioboxes and Labels by itself. But since I can not rely on the .value of the radio-boxes I am currently tracking the state of the group by hand. And this feels wrong.

Here is my prototype:

class Radiogroup(Widget):
    def __init__(self, options, checked=None):
        """
        Creates a Group of radiobuttons.

        Arguments:
        * options: Dict of options
                   { 'id-name 1': 'Screen Name 1', ... }
        * checked: String with the <id> of the pre-checked item
        """
        self._mapping = {}
        div = Div()
        for ident, screenname in options.items():
            rb = Radiobox(_id=ident, name=f'radiogroup-{self.id}', bubble_up=True)
            if ident == checked:
                rb.attributes['checked'] = 'True'
            div.append(rb)
            div.append(Label(screenname, _for=ident))
            div.append(Br())
            self._mapping[rb.id] = ident
        self.nodes = [div]
        self._value = checked

    def handle_input_event(self, input_event):
        self._value = self._mapping[input_event.node.id]

    @property
    def value(self):
        return self._value

    @value.setter
    def value(self, new_value):
        for node in self.query_selector_all('input'):
            if self._mapping[node.id] == new_value:
                node.value = True
            else:
                node.vale = False

direction = Radiogroup(
    {
        'received': 'Payment received',
        'sent': 'Payment sent',
    },
    checked='received',
)

I would really like to see this Widget as part of Lona at some point. But I would like to clarify my understanding of the event-system before contributing any code 😃

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
fscherfcommented, Aug 23, 2022

@SmithChart, @maratori: I implemented a solution that is a combination of my proposal and maratoris backend-side synchronization approach: The RadioButton it self is self-contained, but unchecks all surrounding RadioButtons when it gets checked. I did also build a RadioButtonGroup around RadioButtons that implements properties like value, values and name

https://github.com/lona-web-org/lona/blob/fscherf/topic/radio-buttons/lona/html/data_binding/radio_buttons.py

0reactions
fscherfcommented, Oct 31, 2022

This seems to be a very similar problem like #276. I will revisit this when when we decided on the select api

Read more comments on GitHub >

github_iconTop Results From Across the Web

Radiobuttons do not behave correctly: GroupBoxes have no ...
Started several new test projects (FMX), and the same happens. Impossible to select several buttons from different group boxes. Everything ...
Read more >
Radio buttons not showing data correctly | OutSystems
Hello,. Trying demo like, I have an aggregate GetPhases, here using IsSelected attribute, using for radio button value on screen.
Read more >
<input type="radio"> - HTML: HyperText Markup Language
A radio group is defined by giving each of radio buttons in the group the same name . Once a radio group is...
Read more >
BPM UI toolkit: Radio Button Group - IBM
Radio Button Group creates a set of radio buttons that can be derived from a specified selection list or provided by a selection...
Read more >
Radio Buttons - Android Developers
You should use radio buttons for optional sets that are mutually exclusive if ... If it's not necessary to show all options side-by-side, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found