I have a LiveView file upload form. It has a single input to choose file. Everything works, file is being uploaded, but after it’s been uploaded the input still looks like has the file selected.
This is my form:
@impl true
def render(assigns) do
~L"""
<form id="<% @id %>-upload-form"
class="upload-component"
action="#"
phx-drop-target="<%= @uploads.file.ref %>"
phx-submit="save"
phx-change="validate"
phx-target="<%= @myself %>">
<%= live_file_input @uploads.file %>
<button type="submit">Upload</button>
</form>
"""
end
And, after I choose and upload file and hit “Upload”, it gets properly uplkoaded to backend but form input doesn’t re-set, and stays looking like this:
Three ways I found how to do it are less than ideal. I can do it either:
-
By installing a Hook on the form, which in turns does this.el.reset() when receives a message pushed from the liveview that upload finished.
-
By removing the file input from the liveview for 0.1second and then rendering it again, which obviously blinks to the user because it’s causing this re-rendering.
-
By installing a hook that does
input.value = ""
when receives messgae pushed from liveview that upload finished, similar to 1).
Is there a better way that I am missing?