Modal - added EventCallback for confirm click (working) but will try to rewrite it
This commit is contained in:
		
							parent
							
								
									858dd59688
								
							
						
					
					
						commit
						69413494c5
					
				
							
								
								
									
										19
									
								
								src/Connected.Components/Components/ModalDialog.razor
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/Connected.Components/Components/ModalDialog.razor
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
			
		||||
@if (IsVisible)
 | 
			
		||||
{
 | 
			
		||||
    <div class="modal fade show" tabindex="-1">
 | 
			
		||||
        <div class="modal-dialog modal-dialog-scrollable modal-dialog-centered">
 | 
			
		||||
            <div class="modal-content">
 | 
			
		||||
                <div class="modal-header">
 | 
			
		||||
                    <h3 class="modal-title">@Title</h3>
 | 
			
		||||
                </div>
 | 
			
		||||
                <div class="modal-body">
 | 
			
		||||
                    @Content
 | 
			
		||||
                </div>
 | 
			
		||||
                <div class="modal-footer">
 | 
			
		||||
                    <button type="button" class="btn btn-secondary" @onclick="@(()=>CloseModal())">@CloseButtonText</button>
 | 
			
		||||
                    <button type="button" class="btn btn-core" @onclick="@(()=>CloseModal(true))">@OKButtonText</button>
 | 
			
		||||
                </div>
 | 
			
		||||
            </div>
 | 
			
		||||
        </div>
 | 
			
		||||
    </div>
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										77
									
								
								src/Connected.Components/Components/ModalDialog.razor.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								src/Connected.Components/Components/ModalDialog.razor.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,77 @@
 | 
			
		||||
using Connected.Services.Modal;
 | 
			
		||||
using Microsoft.AspNetCore.Components;
 | 
			
		||||
 | 
			
		||||
namespace Connected.Components;
 | 
			
		||||
public partial class ModalDialog: IDisposable
 | 
			
		||||
{
 | 
			
		||||
	[Inject] ModalDialogService? ModalService { get; set; }
 | 
			
		||||
 | 
			
		||||
	protected bool IsVisible { get; set; }
 | 
			
		||||
	protected string? Title { get; set; }
 | 
			
		||||
	protected RenderFragment? Content { get; set; }
 | 
			
		||||
 | 
			
		||||
	protected EventCallback OnConfirm { get; set; }
 | 
			
		||||
 | 
			
		||||
	protected override void OnInitialized()
 | 
			
		||||
	{
 | 
			
		||||
		ModalService.OnShow += ShowModal;
 | 
			
		||||
		ModalService.OnClose += CloseModal;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void ShowModal(string title, RenderFragment content, EventCallback o, string CancelButtonText = "Cancel", string OKButtonText = "OK")
 | 
			
		||||
	{
 | 
			
		||||
		Title = title;
 | 
			
		||||
		Content = content;
 | 
			
		||||
		IsVisible = true;
 | 
			
		||||
 | 
			
		||||
		this.OKButtonText = OKButtonText;
 | 
			
		||||
		this.CancelButtonText = CancelButtonText;
 | 
			
		||||
 | 
			
		||||
		OnConfirm = o;
 | 
			
		||||
 | 
			
		||||
		StateHasChanged();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void ShowModal(string title, MarkupString content, EventCallback o, string CancelButtonText = "Cancel", string OKButtonText = "OK")
 | 
			
		||||
	{
 | 
			
		||||
		Title = title;
 | 
			
		||||
		Content = new RenderFragment(x => x.AddContent(1, content));
 | 
			
		||||
		IsVisible = true;
 | 
			
		||||
 | 
			
		||||
		this.OKButtonText = OKButtonText;
 | 
			
		||||
		this.CancelButtonText = CancelButtonText;
 | 
			
		||||
 | 
			
		||||
		OnConfirm = o;
 | 
			
		||||
 | 
			
		||||
		StateHasChanged();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void CloseModal(bool OkClicked = false)
 | 
			
		||||
	{
 | 
			
		||||
		IsVisible = false;
 | 
			
		||||
		Title = "";
 | 
			
		||||
		Content = null;
 | 
			
		||||
		if (OkClicked)
 | 
			
		||||
			OnConfirm.InvokeAsync();
 | 
			
		||||
		StateHasChanged();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void Dispose()
 | 
			
		||||
	{
 | 
			
		||||
		if (ModalService is not null)
 | 
			
		||||
		{
 | 
			
		||||
			ModalService.OnShow -= ShowModal;
 | 
			
		||||
			ModalService.OnClose -= CloseModal;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	[Parameter]
 | 
			
		||||
	public EventCallback OnOkClick { get; set; }
 | 
			
		||||
 | 
			
		||||
	[Parameter]
 | 
			
		||||
	public string OKButtonText { get; set; } ="OK";
 | 
			
		||||
 | 
			
		||||
	[Parameter]
 | 
			
		||||
	public string CancelButtonText { get; set; } = "Cancel";
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,24 @@
 | 
			
		||||
using Microsoft.AspNetCore.Components;
 | 
			
		||||
 | 
			
		||||
namespace Connected.Services.Modal;
 | 
			
		||||
public class ModalDialogService
 | 
			
		||||
{
 | 
			
		||||
	public event Action<string, RenderFragment, EventCallback, string, string> OnShow;
 | 
			
		||||
	public event Action<bool> OnClose;
 | 
			
		||||
 | 
			
		||||
	public void ShowDialog(string title, RenderFragment content, EventCallback OnConfirm, string CancelButtonText="Cancel", string OKButtonText="OK")
 | 
			
		||||
	{
 | 
			
		||||
		OnShow?.Invoke(title, content, OnConfirm, OKButtonText, CancelButtonText);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void ShowDialog(string title, MarkupString contentMarkup, EventCallback OnConfirm, string CancelButtonText = "Cancel", string OKButtonText = "OK")
 | 
			
		||||
	{
 | 
			
		||||
		var content = new RenderFragment(x => x.AddContent(1, contentMarkup));
 | 
			
		||||
		OnShow?.Invoke(title, content, OnConfirm, OKButtonText, CancelButtonText);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void Close(bool OkClicked)
 | 
			
		||||
	{
 | 
			
		||||
		OnClose?.Invoke(OkClicked);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,10 @@
 | 
			
		||||
using Connected.Services.Modal;
 | 
			
		||||
using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
 | 
			
		||||
namespace Connected.Services;
 | 
			
		||||
 | 
			
		||||
public static class ServiceCollectionExtensions
 | 
			
		||||
{
 | 
			
		||||
	public static IServiceCollection AddModalDialogService(this IServiceCollection services)
 | 
			
		||||
		 => services.AddScoped<ModalDialogService>();
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user