Skip to content

Commit

Permalink
Restore OnRowFocus and OnCellFocus for DataGrid (#3097)
Browse files Browse the repository at this point in the history
Co-authored-by: Denis Voituron <[email protected]>
  • Loading branch information
vnbaaij and dvoituron authored Dec 25, 2024
1 parent 07eda9f commit eb5cd26
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
GridTemplateColumns="1fr 1fr"
TGridItem=SampleGridData
OnRowClick="HandleRowClick"
OnRowFocus="HandleRowFocus"
OnCellClick="HandleCellClick"
OnCellFocus="HandleCellFocus"
RowSize="@DataGridRowSize.Medium">
<TemplateColumn Title="Name">
<FluentTextField @bind-Value="@context!.Name"/>
Expand Down Expand Up @@ -37,12 +39,22 @@
};

private void HandleRowClick(FluentDataGridRow<SampleGridData> row)
{
DemoLogger.WriteLine($"Row clicked: {row.RowIndex}");
}

private void HandleRowFocus(FluentDataGridRow<SampleGridData> row)
{
DemoLogger.WriteLine($"Row focused: {row.RowIndex}");
}

private void HandleCellClick(FluentDataGridCell<SampleGridData> cell)
{
DemoLogger.WriteLine($"Cell focused: {cell.GridColumn}");
DemoLogger.WriteLine($"Cell clicked: {cell.GridColumn}");
}

private void HandleCellFocus(FluentDataGridCell<SampleGridData> cell)
{
DemoLogger.WriteLine($"Cell focused : {cell.GridColumn}");
}
}
13 changes: 1 addition & 12 deletions src/Core/Components/DataGrid/FluentDataGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public partial class FluentDataGrid<TGridItem> : FluentComponentBase, IHandleEve

/// <summary>
/// Gets or sets a callback when a row is focused.
/// As of 4.11 a row is a tr element with a 'display: contents'. Browsers can not focus such elements currently, but work is underway to fix that.
/// </summary>
[Parameter]
public EventCallback<FluentDataGridRow<TGridItem>> OnRowFocus { get; set; }
Expand Down Expand Up @@ -920,18 +921,6 @@ private void SaveStateToQueryString()
NavigationManager.NavigateTo(NavigationManager.GetUriWithQueryParameters(stateParams), replace: true);
}

private async Task HandleOnRowFocusAsync(DataGridRowFocusEventArgs args)
{
var rowId = args.RowId;
if (_internalGridContext.Rows.TryGetValue(rowId!, out var row))
{
if (row != null && row.RowType == DataGridRowType.Default)
{
await OnRowFocus.InvokeAsync(row);
}
}
}

public async Task OnKeyDownAsync(FluentKeyCodeEventArgs args)
{
if (args.ShiftKey == true && args.Key == KeyCode.KeyR)
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Components/DataGrid/FluentDataGridCell.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
tabindex="0"
@onkeydown="@HandleOnCellKeyDownAsync"
@onclick="@HandleOnCellClickAsync"
@onfocus="@HandleOnCellFocusAsync"
@attributes="AdditionalAttributes">
@ChildContent
</td>
Expand All @@ -23,6 +24,7 @@ else
@oncontextmenu:preventDefault="true"
@onkeydown="@HandleOnCellKeyDownAsync"
@onclick="@HandleOnCellClickAsync"
@onfocus="@HandleOnCellFocusAsync"
@attributes="AdditionalAttributes">
@ChildContent
</th>
Expand Down
8 changes: 8 additions & 0 deletions src/Core/Components/DataGrid/FluentDataGridCell.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ internal async Task HandleOnCellClickAsync()
}
}

internal async Task HandleOnCellFocusAsync()
{
if (CellType == DataGridCellType.Default)
{
await Grid.OnCellFocus.InvokeAsync(this);
}
}

internal async Task HandleOnCellKeyDownAsync(KeyboardEventArgs e)
{
if (!SelectColumn<TGridItem>.KEYBOARD_SELECT_KEYS.Contains(e.Code))
Expand Down
1 change: 1 addition & 0 deletions src/Core/Components/DataGrid/FluentDataGridRow.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@onkeydown="@(e => HandleOnRowKeyDownAsync(RowId, e))"
@onclick="@(e => HandleOnRowClickAsync(RowId))"
@ondblclick="@(e => HandleOnRowDoubleClickAsync(RowId))"
@onfocus="@HandleOnRowFocusAsync"
@attributes="AdditionalAttributes">
@ChildContent
</tr>
Expand Down
10 changes: 3 additions & 7 deletions src/Core/Components/DataGrid/FluentDataGridRow.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,11 @@ internal void Unregister(FluentDataGridCell<TGridItem> cell)
cells.Remove(cell.CellId!);
}

private async Task HandleOnCellFocusAsync(DataGridCellFocusEventArgs args)
internal async Task HandleOnRowFocusAsync()
{
var cellId = args.CellId;
if (cells.TryGetValue(cellId!, out var cell))
if (Grid.OnRowFocus.HasDelegate)
{
if (cell != null && cell.CellType == DataGridCellType.Default)
{
await Grid.OnCellFocus.InvokeAsync(cell);
}
await Grid.OnRowFocus.InvokeAsync(this);
}
}

Expand Down

0 comments on commit eb5cd26

Please sign in to comment.