reflex
reflex copied to clipboard
Is there any way to empty the input after submission?
Question Can I empty the input after submission? Stale text remains in the input form after submission.
Change state which binded to the value in submit callback?
Yes, try something like this:
class State(pc.State):
text: str
def clear_input(self):
self.text = ""
def index():
return pc.vstack(
pc.input(value=State.text, on_change=State.set_text),
pc.button("Clear Input", on_click=State.clear_input),
)
Thanks. But if you change the event to on_blur, any text doesn't show up in the input. I'd like to keep using on_blur.
There's not a way to do it with on_blur currently because we can't set the input value programmatically without setting the value.
We will try to think of a way to do this since it would be handy.
For anyone reaching this...
If the input is created with an id
rx.input(id='input_id', on_blur=...)
Then you can yield/return
rx.set_value('input_id', 'any text or empty string')
from an event handler method to update the value of the input.
Also, if you're using an rx.form, you can pass reset_on_submit=True; although this doesn't work for some of the more exotic elements, like a pininput