diff --git a/Radzen.Blazor.Tests/DataGridTests.cs b/Radzen.Blazor.Tests/DataGridTests.cs index fb0016a45..010784a79 100644 --- a/Radzen.Blazor.Tests/DataGridTests.cs +++ b/Radzen.Blazor.Tests/DataGridTests.cs @@ -3447,6 +3447,35 @@ namespace Radzen.Blazor.Tests Assert.False(string.IsNullOrEmpty(wrapper.GetAttribute("aria-activedescendant"))); } + [Fact] + public void DataGrid_ActiveDescendant_IsClearedWhenNoActiveRow() + { + using var ctx = new TestContext(); + ctx.JSInterop.Mode = JSRuntimeMode.Loose; + ctx.JSInterop.SetupModule("_content/Radzen.Blazor/Radzen.Blazor.js"); + + var component = ctx.RenderComponent>(parameterBuilder => + { + parameterBuilder.Add>(p => p.Data, new[] { new { Id = 1 }, new { Id = 2 } }); + parameterBuilder.Add(p => p.Columns, builder => + { + builder.OpenComponent(0, typeof(RadzenDataGridColumn)); + builder.AddAttribute(1, "Property", "Id"); + builder.CloseComponent(); + }); + }); + + var instance = component.Instance; + + Assert.False(string.IsNullOrEmpty(instance.GetActiveDescendantId())); + + instance.HasActiveDescendant = () => false; + Assert.Null(instance.GetActiveDescendantId()); + + instance.HasActiveDescendant = () => true; + Assert.False(string.IsNullOrEmpty(instance.GetActiveDescendantId())); + } + [Fact] public void DataGrid_AppliesAriaGridRolesToTableDescendants() { diff --git a/Radzen.Blazor/RadzenDataGrid.razor.cs b/Radzen.Blazor/RadzenDataGrid.razor.cs index 5bea99c6d..0675f3065 100644 --- a/Radzen.Blazor/RadzenDataGrid.razor.cs +++ b/Radzen.Blazor/RadzenDataGrid.razor.cs @@ -598,8 +598,15 @@ namespace Radzen.Blazor int focusedIndex = -1; int focusedCellIndex; + internal Func? HasActiveDescendant { get; set; } + internal string? GetActiveDescendantId() { + if (HasActiveDescendant != null && !HasActiveDescendant()) + { + return null; + } + return $"{GetId()}-active-item"; } diff --git a/Radzen.Blazor/RadzenDropDownDataGrid.razor.cs b/Radzen.Blazor/RadzenDropDownDataGrid.razor.cs index a8d02fe81..c5fc6bdfd 100644 --- a/Radzen.Blazor/RadzenDropDownDataGrid.razor.cs +++ b/Radzen.Blazor/RadzenDropDownDataGrid.razor.cs @@ -561,6 +561,11 @@ namespace Radzen.Blazor { isFirstRender = firstRender; + if (grid != null) + { + grid.HasActiveDescendant ??= () => selectedIndex >= 0; + } + if (firstRender) { if(Visible && LoadData.HasDelegate && Data == null)