Megabit / Blazorise

Blazorise is a component library built on top of Blazor with support for CSS frameworks like Bootstrap, Tailwind, Bulma, AntDesign, and Material.
https://blazorise.com/
Other
3.31k stars 533 forks source link

[Bug] Modal does not show when awaited? #3810

Closed yggdrasil-tynor closed 2 years ago

yggdrasil-tynor commented 2 years ago

Describe the bug The Model.Show is async, but Modal's do not show when awaiting the modal. The documentation is not very clear about this?

To Reproduce Steps to reproduce the behavior: await Modal.Show() - not working Modal.Show() - working

Should the Modal.Show() not be awaited?

stsrki commented 2 years ago

It should be. What is your version of Blazorise?

Also, can you post some code snippet?

yggdrasil-tynor commented 2 years ago

@stsrki Version 1.0.4. It used to to work before 1.0.

Codesnippet working:

public async Task Show(StrategyTestParameters parameters, Func<StrategyTestParameters, Task> onTest = null)
    {
        Model = parameters;
        await Validations.ClearAll();
        if (onTest == null)
        {
            OnTest = Close;
        }
        else
        {
            OnTest = async () =>
            {
                if (await Validations.ValidateAll())
                {
                    using (Loading())
                    {
                        await onTest(Model);
                    };
                    Close();
                }
            };
        }

        Modal.Show();
    }

Not working when awaiting the modal.

stsrki commented 2 years ago

Do you call this Show on a button click?

yggdrasil-tynor commented 2 years ago

@stsrki yes sir

<Button Color="Color.Primary" Clicked="@OnTestClicked">Test</Button>

and the eventhandler

    private Task OnTestClicked()
    {
        ExecuteActionAfterCheckDataChanged(async () =>
        {
            var tz = TimeZoneConverter.TZConvert.GetTimeZoneInfo(StrategyConfiguration.TimeZone);
            var now = DateTime.UtcNow;
            now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, DateTimeKind.Utc);

            var input = new StrategyTestParameters
            {
                From = TimeZoneInfo.ConvertTimeFromUtc(now.AddHours(-1), tz),
                To = TimeZoneInfo.ConvertTimeFromUtc(now, tz),
                Interval = TimeSpan.FromMinutes(5)
            };

            await StrategyTestModal.Show(input, async output =>
            {
                await TestStrategy(output);
            });
        });
        return Task.CompletedTask;
    }
stsrki commented 2 years ago

Hmm, I believe that somehow with the ExecuteActionAfterCheckDataChanged, you're breaking Blazor rendering, so the Modal internals is not triggered. Can you try adding await InvokeAsync(StateHasChanged) somewhere after the modal.Show?

yggdrasil-tynor commented 2 years ago

Hmm, I believe that somehow with the ExecuteActionAfterCheckDataChanged, you're breaking Blazor rendering, so the Modal internals is not triggered. Can you try adding await InvokeAsync(StateHasChanged) somewhere after the modal.Show?

You're completely right. I was synchronously waiting for a task in that method which broke it. Wait() instead of await. Thanks for the quick support :)