reflex-dev / reflex

🕸️ Web apps in pure Python 🐍
https://reflex.dev
Apache License 2.0
20.52k stars 1.18k forks source link

Change behavior of switch, checkbox #4067

Open Alek99 opened 2 months ago

Alek99 commented 2 months ago
class FormSwitchState(rx.State):
    form_data: dict = {}

    def handle_submit(self, form_data: dict):
        """Handle the form submit."""
        self.form_data = form_data

def switch_form_example():
    return rx.card(
        rx.vstack(
            rx.heading("Example Form"),
            rx.form.root(
                rx.hstack(
                    rx.switch(name="switch"),
                    rx.button("Submit", type="submit"),
                    width="100%",
                ),
                on_submit=FormSwitchState.handle_submit,
                reset_on_submit=True,
            ),
            rx.divider(),
            rx.hstack(
                rx.heading("Results:"),
                rx.badge(
                    FormSwitchState.form_data.to_string()
                ),
            ),
            align_items="left",
            width="100%",
        ),
        width="50%",
    )

Odd behaviour in switch I would expect it {"switch":"off"} or false but its empty in a form

https://github.com/user-attachments/assets/aa5eb99a-cf02-4304-bbc8-262f03a23d80

Alek99 commented 2 months ago

I would say it should set the form to True/False not off empty

Alek99 commented 2 months ago

same behavior for checkbox

Rydzard commented 1 month ago

Hello @Alek99 . Is this issue free ?