Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileEdit not removing file names after reset() #1597

Closed
RomanJahn opened this issue Dec 10, 2020 · 8 comments · Fixed by #1649
Closed

FileEdit not removing file names after reset() #1597

RomanJahn opened this issue Dec 10, 2020 · 8 comments · Fixed by #1649
Labels
Type: Bug 🐞 Something isn't working
Projects
Milestone

Comments

@RomanJahn
Copy link

I would like to remove the names of the files in the FileEdit component after uploading the files.
I tried Reset() manually as demonstrated in the docs but that doesn't work.
I also recognized that AutoReset is set to TRUE before I call Reset(), even though I set it to FALSE in the FileEdit-tag, as shown in the docs-example. Binding to a property also has no effect - it's always true. Only if I set AutoReset to false (from outside the component, which is not recommended), the AutoReset-bool stays false as expected.

My main question is: Is there a way of removing the file names manually?

Btw, I mainly used the example code.

Kind Regards

@entuto
Copy link

entuto commented Dec 11, 2020

Hello,

I have the same problem, I don't know how to delete the file name, since with Reset() it doesn't work.

@RomanJahn have you managed to fix it?

Thank you very much in advance.

@RomanJahn
Copy link
Author

@entuto No unfortunately not. Waiting for any suggestions for myself ;-)

@stsrki
Copy link
Collaborator

stsrki commented Dec 11, 2020

Internally Reset() only calls javascrot to clear the input value, like this: element.value = '';

I don't see a reason why it won't work. Maybe you need to also notify Blazor of the change. In that case, just use StateHasChanhed() after the Reset()

@entuto
Copy link

entuto commented Dec 11, 2020

In my code I already have StateHasChanged() right after Reset() but it doesn't work.

fileEdit.Reset();
StateHasChanged();

@RomanJahn
Copy link
Author

That was one of the first things I tried but did not change behavior.

@gerfen
Copy link

gerfen commented Dec 11, 2020

I have the same issue. Fundamentally the problem is that the input's sibling label element is not getting cleared. I came up with this fairly horrible hack to work around the issue. I created this JavaScript method:

window.setFileInputLabel = (id, text) => {
    var elem = document.getElementById(id);
    if (elem) {
        const parent = elem.parentElement;
        const label = parent.querySelector('label');
        if (label) {
          label.textContent = text;
        }
    }
}

To clear the label I call the method like so:

 await JavaScriptRuntime.InvokeAsync<string>("setFileInputLabel", new object?[] { FileEditRef.ElementId, string.Empty });

And finally in my FileEdit.Changed handler I set the label like so:

public async Task OnFileChanged(FileChangedEventArgs e)
{
       var files = string.Join(",", e.Files.Select(f => f.Name));
       await JavaScriptRuntime.InvokeAsync<string>("setFileInputLabel", new object?[] { FileEditRef.ElementId, files });
      ... elided for clarity
}

It's not the most elegant solution but it works.

@stsrki stsrki added the Type: Bug 🐞 Something isn't working label Dec 12, 2020
@stsrki stsrki added this to 🔙 Backlog in Support via automation Dec 12, 2020
@stsrki stsrki added this to the 0.9.2 milestone Dec 12, 2020
@entuto
Copy link

entuto commented Dec 14, 2020

@gerfen thank you very much. It works!

@RomanJahn
Copy link
Author

I have the same issue. Fundamentally the problem is that the input's sibling label element is not getting cleared. I came up with this fairly horrible hack to work around the issue. I created this JavaScript method:

window.setFileInputLabel = (id, text) => {
    var elem = document.getElementById(id);
    if (elem) {
        const parent = elem.parentElement;
        const label = parent.querySelector('label');
        if (label) {
          label.textContent = text;
        }
    }
}

To clear the label I call the method like so:

 await JavaScriptRuntime.InvokeAsync<string>("setFileInputLabel", new object?[] { FileEditRef.ElementId, string.Empty });

And finally in my FileEdit.Changed handler I set the label like so:

public async Task OnFileChanged(FileChangedEventArgs e)
{
       var files = string.Join(",", e.Files.Select(f => f.Name));
       await JavaScriptRuntime.InvokeAsync<string>("setFileInputLabel", new object?[] { FileEditRef.ElementId, files });
      ... elided for clarity
}

It's not the most elegant solution but it works.

Thanks a lot. Works.
Sorry for the delayed feedback!

@stsrki stsrki linked a pull request Dec 28, 2020 that will close this issue
@stsrki stsrki mentioned this issue Dec 28, 2020
21 tasks
@stsrki stsrki closed this as completed Dec 28, 2020
Support automation moved this from 🔙 Backlog to ✔ Done Dec 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug 🐞 Something isn't working
Projects
Support
  
✔ Done
Development

Successfully merging a pull request may close this issue.

4 participants