fluentui-blazor: fix: Setting Loading in FluentButton bound to FluentInputFile breaks it

πŸ› Bug Report

After setting Loading in FluentButton bound to FluentInputFile, it won’t open the file browse dialog anymore.

πŸ’» Repro or Code Sample

                    <FluentButton
                        Id="form-browse-button"
                        Loading="_formUploading">
                        Upload...
                    </FluentButton>
                <FluentInputFile
                    Accept="application/pdf"
                    AnchorId="form-browse-button"
                    DragDropZoneVisible="false"
                    Id="form-file-uploader"
                    Mode="InputFileMode.SaveToTemporaryFolder"
                    OnCompleted="OnFormUploadCompleted"
                    OnProgressChange="OnFormUploading"/>
@code {
    bool _formUploading;
    void OnFormUploading()
    {
        _formUploading = true;
    }
    async Task OnFormUploadCompleted(IEnumerable<FluentInputFileEventArgs> files)
    {
        _formUploading = false;
    }
}

🌍 Your Environment

  • OS & Device: Windows on PC
  • Browser: Microsoft Edge, Mozilla FireFox
  • .NET and Fluent UI Blazor library Version: 8.0.400/4.10.1

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@vnbaaij if I have ever learnt one thing in Blazor than it is you should almost prevent manipulating the DOM using JS. Makes many stuff much harder to maintain in the longrun. In this case I thought it wouldn’t be an issue since the attribute will be removed everytime the loading spinner occurs which is excatly what this problem is about.

But if we can prevent using it, why not.

If you both agree with this approach then I will create a PR for this and add a new example for this. Not sure about tests though. I’Ve never created any for blazor components.

@dvoituron damn I hate JS. But you were right. I think the only way is by setting a data attribute then.

export function attachClickHandler(buttonId, fileInputId) {
    var button = document.getElementById(buttonId);
    var fileInput = document.getElementById(fileInputId);

    if (button && fileInput && !button.hasAttribute("data-click-handler-attached")) {

        button.addEventListener("click", function (e) {
            fileInput.click();
        });

        button.setAttribute("data-click-handler-attached", "true");
    }
}

@dvoituron yea but you can prevent this by removing the handler first.

export function attachClickHandler(buttonId, fileInputId) {
    var button = document.getElementById(buttonId);
    var fileInput = document.getElementById(fileInputId);

    if (button && fileInput) {
        button.removeEventListener("click");
        button.addEventListener("click", function (e) {
            fileInput.click();
        });
    }
}