@using System.ComponentModel.DataAnnotations
@if (IsOpen)
{
@if (!string.IsNullOrWhiteSpace(Description))
{
@Description
}
@if (!string.IsNullOrEmpty(_error))
{
@_error
}
}
@code {
[Parameter] public bool IsOpen { get; set; }
[Parameter] public EventCallback IsOpenChanged { get; set; }
[Parameter] public string Title { get; set; } = "Edit value";
[Parameter] public string? Description { get; set; }
[Parameter] public string Label { get; set; } = "Value";
[Parameter] public string? Value { get; set; }
///
/// Called when Accept is clicked.
/// May throw an exception (e.g. validation error).
///
[Parameter] public EventCallback OnSubmit { get; set; }
private FormModel _model = new();
private string? _error;
protected override void OnParametersSet()
{
if (IsOpen)
{
_error = null;
_model = new FormModel
{
Value = Value
};
}
}
private async Task HandleValidSubmit()
{
_error = null;
try
{
await OnSubmit.InvokeAsync(_model.Value!);
await Close();
}
catch (Exception ex)
{
// Show exception message instead of closing
_error = ex.Message;
}
}
private async Task Cancel()
{
await Close();
}
private async Task Close()
{
_model = new();
_error = null;
await IsOpenChanged.InvokeAsync(false);
}
private class FormModel
{
[Required]
public string? Value { get; set; }
}
}