From 0778c150b60a24dafed2ddd0c8cf663a85efca1a Mon Sep 17 00:00:00 2001 From: Atanas Korchev Date: Mon, 25 Dec 2023 10:40:11 +0200 Subject: [PATCH] Cannot show RadzenAlert after the user closes it. --- Radzen.Blazor/Common.cs | 4 +- Radzen.Blazor/RadzenAlert.razor | 65 +--------- Radzen.Blazor/RadzenAlert.razor.cs | 143 +++++++++++++++------- RadzenBlazorDemos/Pages/AlertConfig.razor | 27 +++- 4 files changed, 131 insertions(+), 108 deletions(-) diff --git a/Radzen.Blazor/Common.cs b/Radzen.Blazor/Common.cs index ab0e096a8..20e995bab 100644 --- a/Radzen.Blazor/Common.cs +++ b/Radzen.Blazor/Common.cs @@ -1690,7 +1690,7 @@ namespace Radzen /// /// Satisfied if the current value is not . /// - IsNotEmpty, + IsNotEmpty, /// /// Custom operator if not need to generate the filter. /// @@ -2830,7 +2830,7 @@ namespace Radzen } return false; } - + /// /// Method to only replace first occurence of a substring in a string /// diff --git a/Radzen.Blazor/RadzenAlert.razor b/Radzen.Blazor/RadzenAlert.razor index 81db96180..ba3c8a1dd 100644 --- a/Radzen.Blazor/RadzenAlert.razor +++ b/Radzen.Blazor/RadzenAlert.razor @@ -1,11 +1,11 @@ @inherits RadzenComponentWithChildren -@if (GetVisible()) +@if (visible) {
@if (ShowIcon) { - + }
@if (!string.IsNullOrEmpty(Title)) @@ -13,7 +13,7 @@
@Title
}
- @if(ChildContent != null) + @if (ChildContent != null) { @ChildContent } @@ -26,64 +26,7 @@
@if (AllowClose) { - if (Shade == Shade.Light || Shade == Shade.Lighter) - { - if (AlertStyle == AlertStyle.Primary) - { - - } - else if (AlertStyle == AlertStyle.Secondary) - { - - } - else if (AlertStyle == AlertStyle.Light) - { - - } - else if (AlertStyle == AlertStyle.Base) - { - - } - else if (AlertStyle == AlertStyle.Dark) - { - - } - else if (AlertStyle == AlertStyle.Success) - { - - } - else if (AlertStyle == AlertStyle.Danger) - { - - } - else if (AlertStyle == AlertStyle.Warning) - { - - } - else if (AlertStyle == AlertStyle.Info) - { - - } - } - else - { - if (AlertStyle == AlertStyle.Light) - { - - } - else if (AlertStyle == AlertStyle.Base) - { - - } - else if (AlertStyle == AlertStyle.Dark) - { - - } - else - { - - } - } + }
} \ No newline at end of file diff --git a/Radzen.Blazor/RadzenAlert.razor.cs b/Radzen.Blazor/RadzenAlert.razor.cs index a515964e2..f26356729 100644 --- a/Radzen.Blazor/RadzenAlert.razor.cs +++ b/Radzen.Blazor/RadzenAlert.razor.cs @@ -99,10 +99,65 @@ namespace Radzen.Blazor return Size == AlertSize.ExtraSmall ? ButtonSize.ExtraSmall : ButtonSize.Small; } - bool? visible; - bool GetVisible() + Shade GetCloseButtonShade() { - return visible ?? Visible; + if (Shade == Shade.Light || Shade == Shade.Lighter) + { + return Shade.Darker; + } + else + { + return Shade.Default; + } + } + + ButtonStyle GetCloseButtonStyle() + { + if (Shade == Shade.Light || Shade == Shade.Lighter) + { + switch (AlertStyle) + { + case AlertStyle.Success: + return ButtonStyle.Success; + case AlertStyle.Danger: + return ButtonStyle.Danger; + case AlertStyle.Warning: + return ButtonStyle.Warning; + case AlertStyle.Info: + return ButtonStyle.Info; + case AlertStyle.Primary: + return ButtonStyle.Primary; + case AlertStyle.Secondary: + return ButtonStyle.Secondary; + case AlertStyle.Light: + case AlertStyle.Base: + return ButtonStyle.Dark; + default: + return ButtonStyle.Light; + } + } + else + { + switch (AlertStyle) + { + case AlertStyle.Light: + case AlertStyle.Base: + return ButtonStyle.Dark; + default: + return ButtonStyle.Light; + } + } + } + + + bool visible; + + /// + protected override void OnInitialized() + { + base.OnInitialized(); + + visible = Visible; } /// @@ -111,55 +166,59 @@ namespace Radzen.Blazor return $"rz-alert rz-alert-{GetAlertSize()} rz-variant-{Enum.GetName(typeof(Variant), Variant).ToLowerInvariant()} rz-{Enum.GetName(typeof(AlertStyle), AlertStyle).ToLowerInvariant()} rz-shade-{Enum.GetName(typeof(Shade), Shade).ToLowerInvariant()}"; } - string getIcon() + string GetIcon() { if (!string.IsNullOrEmpty(Icon)) { return Icon; } - else if (AlertStyle == AlertStyle.Primary) - { - return "lightbulb_outline"; - } - else if (AlertStyle == AlertStyle.Secondary) - { - return "lightbulb_outline"; - } - else if (AlertStyle == AlertStyle.Light) - { - return "lightbulb_outline"; - } - else if (AlertStyle == AlertStyle.Base) - { - return "lightbulb_outline"; - } - else if (AlertStyle == AlertStyle.Dark) - { - return "lightbulb_outline"; - } - else if (AlertStyle == AlertStyle.Success) - { - return "check_circle_outline"; - } - else if (AlertStyle == AlertStyle.Danger) - { - return "error_outline"; - } - else if (AlertStyle == AlertStyle.Warning) - { - return "warning_amber"; - } - else if (AlertStyle == AlertStyle.Info) - { - return "info_outline"; - } - return ""; + switch (AlertStyle) + { + case AlertStyle.Success: + return "check_circle_outline"; + case AlertStyle.Danger: + return "error_outline"; + case AlertStyle.Warning: + return "warning_amber"; + case AlertStyle.Info: + return "info_outline"; + default: + return "lightbulb_outline"; + } } - void Close() + async Task OnClose() { visible = false; + + await VisibleChanged.InvokeAsync(false); + await Close.InvokeAsync(null); + } + + /// + /// Gets or sets the callback which is invoked when the alert is shown or hidden. + /// + [Parameter] + public EventCallback VisibleChanged { get; set; } + + /// + /// Gets or sets the callback which is invoked when the alert is closed by the user. + /// + [Parameter] + public EventCallback Close { get; set; } + + /// + public override async Task SetParametersAsync(ParameterView parameters) + { + var visibleChanged = parameters.DidParameterChange(nameof(Visible), Visible); + + await base.SetParametersAsync(parameters); + + if (visibleChanged) + { + visible = Visible; + } } } } diff --git a/RadzenBlazorDemos/Pages/AlertConfig.razor b/RadzenBlazorDemos/Pages/AlertConfig.razor index 65abbc4b3..7cf5203b8 100644 --- a/RadzenBlazorDemos/Pages/AlertConfig.razor +++ b/RadzenBlazorDemos/Pages/AlertConfig.razor @@ -1,4 +1,6 @@ - +@inject NotificationService NotificationService + + Alert with close button @@ -6,9 +8,28 @@ Alert without close button - Alert with close button + Alert with title Alert without message icon - \ No newline at end of file + + Toggle alert visibility and handle the Close event. + + + +@code { + bool visible = true; + + string ToggleButtonText => visible ? "Hide alert" : "Show alert"; + + void OnToggle() + { + visible = !visible; + } + + void OnClose() + { + NotificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Info, Summary = "Closed", Detail = "Info" }); + } +} \ No newline at end of file