112 lines
3.4 KiB
Plaintext
112 lines
3.4 KiB
Plaintext
@inject IJSRuntime JS
|
|
|
|
<div class="modal fade" id="@ModalId" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-top">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">@Title</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"
|
|
@onclick="OnCancel"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<p>@Message</p>
|
|
|
|
@if (Mode == ModalMode.Prompt)
|
|
{
|
|
<input @bind="UserInput" class="form-control" placeholder="@Placeholder" />
|
|
}
|
|
</div>
|
|
<div class="modal-footer">
|
|
@if (Mode == ModalMode.Message)
|
|
{
|
|
<button type="button" class="btn btn-primary" data-bs-dismiss="modal" @onclick="OnOk">OK</button>
|
|
}
|
|
else if (Mode == ModalMode.Confirm)
|
|
{
|
|
<button type="button" class="btn btn-primary" @onclick="OnConfirm">Yes</button>
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" @onclick="OnCancel">No</button>
|
|
}
|
|
else if (Mode == ModalMode.Prompt)
|
|
{
|
|
<button type="button" class="btn btn-primary" @onclick="OnPromptOk">OK</button>
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" @onclick="OnCancel">Cancel</button>
|
|
}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
@code {
|
|
private TaskCompletionSource<object?>? tcs;
|
|
private string ModalId { get; } = $"modal_{Guid.NewGuid():N}";
|
|
|
|
[Parameter]
|
|
public string Title { get; set; } = "Modal";
|
|
|
|
[Parameter]
|
|
public string Message { get; set; } = "";
|
|
|
|
[Parameter]
|
|
public string UserInput { get; set; } = "";
|
|
|
|
[Parameter]
|
|
public string Placeholder { get; set; } = "";
|
|
|
|
[Parameter]
|
|
public ModalMode Mode { get; set; } = ModalMode.Message;
|
|
|
|
/// <summary>
|
|
/// Mostra la modale ed aspetta risultato utente
|
|
/// </summary>
|
|
public async Task<T?> ShowAsync<T>()
|
|
{
|
|
tcs = new TaskCompletionSource<object?>();
|
|
await JS.InvokeVoidAsync("bootstrapModalHelper.show", $"#{ModalId}");
|
|
var result = await tcs.Task;
|
|
|
|
// Convert result to requested type
|
|
if (result is null)
|
|
return default;
|
|
return (T)result;
|
|
}
|
|
|
|
private async Task OnOk()
|
|
{
|
|
await JS.InvokeVoidAsync("bootstrapModalHelper.hide", $"#{ModalId}");
|
|
tcs?.TrySetResult(true);
|
|
}
|
|
|
|
private async Task OnConfirm()
|
|
{
|
|
await JS.InvokeVoidAsync("bootstrapModalHelper.hide", $"#{ModalId}");
|
|
tcs?.TrySetResult(true);
|
|
}
|
|
|
|
private async Task OnPromptOk()
|
|
{
|
|
await JS.InvokeVoidAsync("bootstrapModalHelper.hide", $"#{ModalId}");
|
|
tcs?.TrySetResult(UserInput);
|
|
}
|
|
|
|
private async Task OnCancel()
|
|
{
|
|
await JS.InvokeVoidAsync("bootstrapModalHelper.hide", $"#{ModalId}");
|
|
if (Mode == ModalMode.Prompt)
|
|
tcs?.TrySetResult(null);
|
|
else if (Mode == ModalMode.Confirm)
|
|
tcs?.TrySetResult(false);
|
|
else
|
|
tcs?.TrySetResult(null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Modalità display componente
|
|
/// </summary>
|
|
public enum ModalMode
|
|
{
|
|
Message,
|
|
Confirm,
|
|
Prompt
|
|
}
|
|
}
|