Finally, we need to update the event handling clauses:
def handle_event("change", %{"text" => value}, socket) do
socket = assign(socket, :text_value, value)
{:noreply, socket}
end
def handle_event("add", %{"text" => value}, socket) do
SalesModule.Data.create_new(value)
socket = assign(socket, :text_value, nil)
{:noreply, socket}
end
Basically the idea here is that we’re setting up the HTML to be derived from the assign, so that when on submit we set the assign to nil, the value goes away. This does require (or at least, is works best when) we also track all of the form changes along the way.
Clearing the field without a changeset set is probably something that is frequently needed for things like a live chat - which is very simple to do in liveview with pubsub, so personally I add it almost everywhere.
I use the same approach as @benwilson512 described, but add a phx-throttle on the input, to get an event as soon as someone starts typing, but rate limit the updates afterwards.
If the change tracking is only needed to clear the field on submit, one can even use a throttle value that is quite high, because to clear the field liveview only needs to know that the value changed from anything to nil.