We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How make the code wait for dialog close?

I'm surprised this isn't the default behavior -- and that there are no apparent forum posts about it.

I want my code to display a dialog and wait for it to close before continuing the code processing.

Here's a contrived example.

dialogVisible = true
Dialog offers a Yes/No choice
User clicks one of the buttons
Code displays, "You clicked {button}".

I know I could just place the continuation code in the button click events, but I have reasons for not wanting to do that.

To me, "modal" means all processing stops until the modal dialog is closed.




10 Replies

VJ Vinitha Jeyakumar Syncfusion Team March 1, 2023 11:42 AM UTC

Hi Keith,

We suggest you to use the Predefined Dialog control, in which you can use the DialogService.ConfirmAsync method. It is used to get approval from the user, and it appears before any critical action. After get approval from the user the dialog will disappear automatically. Please check the below documentation for further details,


If it doesn't meet your requirements, can you please share us with your exact requirement and use case scenario for further validation.

Regards,
Vinitha


KA Keith A Price March 1, 2023 04:09 PM UTC

I'm placing custom content in the dialogs -- in some cases, they fill in a small form -- so I don't think I can use the predefined dialogs. No?


I came up with a workaround which feel really clunky, but it works.

I set the dialog's visibility to true and run an await.Delay(25) in a loop while visibility is still true.

Feels like there should be a clean way to have functionality like the predefined dialogs while being able to add any custom content inside it.

Intuitively, setting IsModal to true, feels like it should block code until the modal is closed.



VJ Vinitha Jeyakumar Syncfusion Team March 2, 2023 11:56 AM UTC

Hi Keith,


You can load custom content in predefined dialogs using the DialogOptions.ChildContent property.
Use the following code to customize the dialog content to render the custom TextBox component inside the prompt dialog to get the username from the user.

Code snippet:
@using Syncfusion.Blazor.Popups
@using Syncfusion.Blazor.Buttons
@using Syncfusion.Blazor.Inputs
@inject SfDialogService DialogService
    <div>
        <SfButton @onclick="@PromptBtn">Prompt</SfButton>
    </div>
@code {
    private async Task PromptBtn()
    {
        string promptText = await DialogService.PromptAsync(null, "Join Chat Group", new DialogOptions()
            {
             
        ChildContent = @<table class="Table">
                        <tbody>
                            <tr>
                                <td>
                                    <label>Enter your name: </label>
                                </td>
                                <td>
                                <SfTextBox Value="text" Type="InputType.Text"></SfTextBox>
                                </td>
                            </tr>
                        </tbody>
                    </table>
        });
    }

}


Regards,
Vinitha


KA Keith A Price March 2, 2023 04:24 PM UTC

How much customization can we do? Height? Width? Could we put a carousel in it? A List?



VJ Vinitha Jeyakumar Syncfusion Team March 3, 2023 01:32 PM UTC

Hi Keith,


Yes, you can customize the height, width and you can also add any controls like lists and carousel inside the childContent of the predefined dialogs. We have also prepared a sample for your reference using a carousel.

Please check the below API link to find the available options with the DialogOptions of predefined dialogs,


Regards,
Vinitha

Attachment: DialogServer69494_cfe2a432.zip


KA Keith A Price March 5, 2023 05:40 AM UTC

Awesome!

I've never seen child content set up via code, before.

What if I wanted to have a custom component as child content and that component needed to pass parameters, including, maybe an event callback.

Or what if I had a form as child content? And it had a submit button? How would that work in a dialog?

Is that possible?

If so, any chance you could show an example of that?



VJ Vinitha Jeyakumar Syncfusion Team March 8, 2023 02:59 PM UTC

Hi Keith ,


Currently we are validating your reported query. we will update you the further details in two business days on or before 10th March 2023.

Regards,
Vinitha


VJ Vinitha Jeyakumar Syncfusion Team March 14, 2023 12:29 PM UTC

Hi Keith,

We are facing increased complexity in achieving your requirement with Dialog control and we are working on to provide you with the custom sample for your requirement. So we need some additional time to validate on this and will update you at the earliest.

Regards,
Vinitha


KA Keith A Price March 15, 2023 05:14 AM UTC

Thanks for letting me know. Looking forward to seeing what you come up with! 



VJ Vinitha Jeyakumar Syncfusion Team March 15, 2023 10:28 AM UTC

Hi Keith,

Query "I want my code to display a dialog and wait for it to close before continuing the code processing."

We have prepared a custom sample that can make the execution of the code to wait, until the Dialog close using the TaskCompletionSource functionality. Please check the code and sample below,

Code snippet:
<SfDialog Width="250px" @ref="dialogObj" ShowCloseIcon="true" IsModal="true" @bind-Visible="@IsVisible">
    <DialogTemplates>
        <Header> Dialog </Header>
        <Content> This is a Dialog with button and primary button </Content>
    </DialogTemplates>
    <DialogButtons>
        <DialogButton Content="OK" IsPrimary="true" OnClick="@OkClick" />
        <DialogButton Content="Cancel" OnClick="@CancelClick" />
    </DialogButtons>
    <DialogEvents OnClose="DialogClose"></DialogEvents>
</SfDialog>
<span id="message">@ClickStatus</span>

@code {
    SfDialog dialogObj;
    private bool IsVisible { get; set; } = false;
    private string ClickStatus { get; set; }
    private bool IsConfirm { get; set; } = false;
    internal List<TaskCompletionSource<dynamic>> tasks = new List<TaskCompletionSource<dynamic>>();
    public async Task<bool> ConfirmAsync()
    {
        return await OpenAsync();
    }
    public Task<dynamic> OpenAsync(){
        TaskCompletionSource<dynamic> task = new TaskCompletionSource<dynamic>();
        tasks.Add(task);
        Open().ConfigureAwait(false);
        return task.Task;
    }
    public async Task Open()
    {
        this.IsVisible = true;
        await InvokeAsync(() => { StateHasChanged(); });
    }
    public void DialogClose()
    {
        TaskCompletionSource<dynamic> task = tasks.LastOrDefault();
        if (task != null && task.Task != null && !task.Task.IsCompleted)
        {
            tasks.Remove(task);
            task.SetResult(true);
        }
    }
    private async Task OpenDialog()
    {   
       bool isConfirm = await this.ConfirmAsync(); 
        //The below code will not execute and wait till the dialog to close
        //    
        if(this.IsConfirm)
        {            
            this.ClickStatus = "User clicked Ok";
        }
        else
        {
            this.ClickStatus = "User clicked Cancel";
        }
    }
    private void CancelClick()
    {
            this.IsVisible = false;
    }
    private void OkClick()
    {
        this.IsConfirm = true;      
        this.IsVisible = false;
    }
}

Note: Instead of using Predefined Dialog, the above solution will help you to achieve your requirements like using a Form or child component inside the Dialog control.

Sample attached below,

Please let us know if you have any concerns.

Regards,
Vinitha

Attachment: DialogServer_62ae9eb0.zip

Loader.
Live Chat Icon For mobile
Up arrow icon