reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Is there any way to empty the input after submission?

Open asleep-henry opened this issue 2 years ago β€’ 4 comments

Question Can I empty the input after submission? Stale text remains in the input form after submission.

asleep-henry avatar Jan 18 '23 13:01 asleep-henry

Change state which binded to the value in submit callback?

FHU-yezi avatar Jan 18 '23 14:01 FHU-yezi

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),
  )

picklelo avatar Jan 18 '23 20:01 picklelo

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.

asleep-henry avatar Jan 19 '23 03:01 asleep-henry

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.

picklelo avatar Jan 21 '23 19:01 picklelo

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.

TimChild avatar Apr 12 '24 14:04 TimChild

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

masenf avatar Apr 12 '24 16:04 masenf