58 lines
1.9 KiB
Plaintext
58 lines
1.9 KiB
Plaintext
@implements IDisposable
|
|||
|
|
|
||
|
|
@* In-page errors (b2b.next's WidmannErrorViewer): the validation messages of the cascading EditContext, then
|
||
|
|
AdditionalError (one message) and AdditionalErrors (a list; never join a list into one string), one <li> each.
|
||
|
|
Renders nothing when there is nothing to show. Messages are text, not markup: they can contain file names. *@
|
||
|
|
@if (HasContent)
|
||
|
|
{
|
||
|
|
<DAlert Color="@(Severity is ErrorViewerSeverity.Warning ? DAlertColor.Warning : DAlertColor.Error)" Variant="DAlertVariant.Soft"
|
||
|
|
class="@Cx("text-sm", ContainerClass)">
|
||
|
|
<ul class="list-inside list-disc">
|
||
|
|
@foreach (var message in Messages)
|
||
|
|
{
|
||
|
|
<li>@message</li>
|
||
|
|
}
|
||
|
|
</ul>
|
||
|
|
</DAlert>
|
||
|
|
}
|
||
|
|
|
||
|
|
@code {
|
||
|
|
[CascadingParameter] EditContext EditContext { get; set; }
|
||
|
|
[Parameter] public string ContainerClass { get; set; }
|
||
|
|
[Parameter] public ErrorViewerSeverity Severity { get; set; }
|
||
|
|
[Parameter] public string AdditionalError { get; set; }
|
||
|
|
[Parameter] public IEnumerable<string> AdditionalErrors { get; set; } = [];
|
||
|
|
|
||
|
|
EditContext subscribedEditContext;
|
||
|
|
|
||
|
|
IEnumerable<string> Messages =>
|
||
|
|
(EditContext?.GetValidationMessages() ?? [])
|
||
|
|
.Append(AdditionalError)
|
||
|
|
.Concat(AdditionalErrors)
|
||
|
|
.Where(message => !string.IsNullOrEmpty(message));
|
||
|
|
|
||
|
|
bool HasContent => Messages.Any();
|
||
|
|
|
||
|
|
static string Cx(params string[] classes) => DCx.Join(classes);
|
||
|
|
|
||
|
|
protected override void OnParametersSet()
|
||
|
|
{
|
||
|
|
if (EditContext == subscribedEditContext)
|
||
|
|
return;
|
||
|
|
|
||
|
|
Unsubscribe();
|
||
|
|
subscribedEditContext = EditContext;
|
||
|
|
if (subscribedEditContext is not null)
|
||
|
|
subscribedEditContext.OnValidationStateChanged += OnValidationStateChanged;
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnValidationStateChanged(object sender, ValidationStateChangedEventArgs e) => _ = InvokeAsync(StateHasChanged);
|
||
|
|
|
||
|
|
void Unsubscribe()
|
||
|
|
{
|
||
|
|
if (subscribedEditContext is not null)
|
||
|
|
subscribedEditContext.OnValidationStateChanged -= OnValidationStateChanged;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Dispose() => Unsubscribe();
|
||
|
|
}
|