Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,26 @@
Constructs an instance of <see cref="T:Microsoft.FluentUI.AspNetCore.Components.ColumnBase`1" />.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.ColumnBase`1.OnRowKeyDown">
<summary>
Gets or sets the event OnRowKeyDown
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.ColumnBase`1.OnRowClick">
<summary>
Gets or sets a callback when a row is clicked.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.ColumnBase`1.OnRowDoubleClick">
<summary>
Gets or sets a callback when a row is double-clicked.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.ColumnBase`1.OnCellClick">
<summary>
Gets or sets a callback when a cell is clicked.
</summary>
</member>
<member name="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1">
<summary>
Represents a sort order specification used within <see cref="T:Microsoft.FluentUI.AspNetCore.Components.FluentDataGrid`1"/>.
Expand Down Expand Up @@ -1741,6 +1761,11 @@
Gets or sets a callback when a row is double-clicked.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentDataGrid`1.OnRowKeyDown">
<summary>
Gets or sets a callback when a row is key down.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentDataGrid`1.RowClass">
<summary>
Optionally defines a class to be applied to a rendered row.
Expand Down Expand Up @@ -1773,11 +1798,6 @@
A default fragment is used if loading content is not specified.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentDataGrid`1.SelectColumns">
<summary>
Gets the first (optional) SelectColumn
</summary>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentDataGrid`1.#ctor">
<summary>
Constructs an instance of <see cref="T:Microsoft.FluentUI.AspNetCore.Components.FluentDataGrid`1"/>.
Expand Down Expand Up @@ -1871,9 +1891,6 @@
Gets a reference to the column that this cell belongs to.
</summary>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentDataGridCell`1.HandleOnCellClickAsync">
<summary />
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentDataGridRow`1.Item">
<summary>
Gets or sets the reference to the item that holds this row's values.
Expand Down Expand Up @@ -1905,15 +1922,6 @@
Gets or sets the owning <see cref="T:Microsoft.FluentUI.AspNetCore.Components.FluentDataGrid`1"/> component
</summary>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentDataGridRow`1.HandleOnRowClickAsync(System.String)">
<summary />
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentDataGridRow`1.HandleOnRowDoubleClickAsync(System.String)">
<summary />
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentDataGridRow`1.HandleOnRowKeyDownAsync(System.String,Microsoft.AspNetCore.Components.Web.KeyboardEventArgs)">
<summary />
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentDataGridRow`1.Microsoft#AspNetCore#Components#IHandleEvent#HandleEventAsync(Microsoft.AspNetCore.Components.EventCallbackWorkItem,System.Object)">
<summary />
</member>
Expand Down
20 changes: 20 additions & 0 deletions src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,24 @@ public ColumnBase()
{
HeaderContent = RenderDefaultHeaderContent;
}

/// <summary>
/// Gets or sets the event OnRowKeyDown
/// </summary>
internal EventCallback<DataGridRowKeyEventArgs<TGridItem>> OnRowKeyDown { get; set; }

/// <summary>
/// Gets or sets a callback when a row is clicked.
/// </summary>
internal EventCallback<FluentDataGridRow<TGridItem>> OnRowClick { get; set; }

/// <summary>
/// Gets or sets a callback when a row is double-clicked.
/// </summary>
internal EventCallback<FluentDataGridRow<TGridItem>> OnRowDoubleClick { get; set; }

/// <summary>
/// Gets or sets a callback when a cell is clicked.
/// </summary>
internal protected EventCallback<FluentDataGridCell<TGridItem>> OnCellClick { get; set; }
}
26 changes: 25 additions & 1 deletion src/Core/Components/DataGrid/Columns/SelectColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ public SelectColumn()
{
Width = "50px";
ChildContent = GetDefaultChildContent();

OnRowClick = EventCallback.Factory.Create<FluentDataGridRow<TGridItem>>(this, async (row) =>
{
if (SelectFromEntireRow)
{
await AddOrRemoveSelectedItemAsync(row.Item);
}
});

OnRowKeyDown = EventCallback.Factory.Create<DataGridRowKeyEventArgs<TGridItem>>(this, async (e) =>
{
if (KEYBOARD_SELECT_KEYS.Contains(e.Code))
{
await AddOrRemoveSelectedItemAsync(e.Row.Item);
}
});

OnCellClick = EventCallback.Factory.Create<FluentDataGridCell<TGridItem>>(this, async (cell) =>
{
if (!SelectFromEntireRow && cell.Column == this)
{
await AddOrRemoveSelectedItemAsync(cell.Item);
}
});
}

/// <summary>
Expand Down Expand Up @@ -206,7 +230,7 @@ public async Task ClearSelectionAsync()
}

/// <summary />
internal async Task AddOrRemoveSelectedItemAsync(TGridItem? item)
private async Task AddOrRemoveSelectedItemAsync(TGridItem? item)
{
if (item != null)
{
Expand Down
111 changes: 106 additions & 5 deletions src/Core/Components/DataGrid/FluentDataGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// ------------------------------------------------------------------------

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Web.Virtualization;
using Microsoft.FluentUI.AspNetCore.Components.DataGrid.Infrastructure;
using Microsoft.FluentUI.AspNetCore.Components.Infrastructure;
Expand Down Expand Up @@ -142,6 +143,13 @@ public partial class FluentDataGrid<TGridItem> : FluentComponentBase, IHandleEve
[Parameter]
public EventCallback<FluentDataGridRow<TGridItem>> OnRowDoubleClick { get; set; }

/// <summary>
/// Gets or sets a callback when a row is key down.
/// </summary>
[Parameter]
public EventCallback<DataGridRowKeyEventArgs<TGridItem>> OnRowKeyDown { get; set; }


/// <summary>
/// Optionally defines a class to be applied to a rendered row.
/// </summary>
Expand Down Expand Up @@ -178,11 +186,6 @@ public partial class FluentDataGrid<TGridItem> : FluentComponentBase, IHandleEve
[Inject] private IJSRuntime JSRuntime { get; set; } = default!;
[Inject] private IKeyCodeService KeyCodeService { get; set; } = default!;

/// <summary>
/// Gets the first (optional) SelectColumn
/// </summary>
internal IEnumerable<SelectColumn<TGridItem>> SelectColumns => _columns.Where(col => col is SelectColumn<TGridItem>).Cast< SelectColumn<TGridItem>>();

private ElementReference? _gridReference;
private Virtualize<(int, TGridItem)>? _virtualizeComponent;

Expand Down Expand Up @@ -681,4 +684,102 @@ private async Task ResetColumnWidthsAsync()
}
}

internal async Task HandleOnRowClickAsync(string rowId)
{
if (_internalGridContext.Rows.TryGetValue(rowId, out var row))
{
if (OnRowClick.HasDelegate)
{
await OnRowClick.InvokeAsync(row);
}

if (row != null && row.RowType == DataGridRowType.Default)
{
foreach (var item in _columns)
{
if (item.OnRowClick.HasDelegate)
{
await item.OnRowClick.InvokeAsync(row);
}
}
}
}
}

internal async Task HandleOnRowDoubleClickAsync(string rowId)
{
if (_internalGridContext.Rows.TryGetValue(rowId, out var row))
{
if (OnRowDoubleClick.HasDelegate)
{
await OnRowDoubleClick.InvokeAsync(row);
}

if (row != null && row.RowType == DataGridRowType.Default)
{
foreach (var item in _columns)
{
if (item.OnRowDoubleClick.HasDelegate)
{
await item.OnRowDoubleClick.InvokeAsync(row);
}
}
}
}
}

internal async Task HandleOnRowKeyDownAsync(string rowId, KeyboardEventArgs e)
{
if (_internalGridContext.Rows.TryGetValue(rowId, out var row))
{
DataGridRowKeyEventArgs<TGridItem> CreateDataEvent() => new()
{
AltKey = e.AltKey,
Code = e.Code,
CtrlKey = e.CtrlKey,
Key = e.Key,
Location = e.Location,
MetaKey = e.MetaKey,
Repeat = e.Repeat,
ShiftKey = e.ShiftKey,
Type = e.Type,
Row = row
};

if (OnRowKeyDown.HasDelegate)
{
await OnRowKeyDown.InvokeAsync(CreateDataEvent());
}

if (row != null && row.RowType == DataGridRowType.Default)
{
foreach (var item in _columns)
{
if (item.OnRowKeyDown.HasDelegate)
{
await item.OnRowKeyDown.InvokeAsync(CreateDataEvent());
}
}
}
}
}

internal async Task HandleOnCellClickAsync(FluentDataGridCell<TGridItem> cell)
{
if (OnCellClick.HasDelegate)
{
await OnCellClick.InvokeAsync(cell);
}

if (cell.CellType == DataGridCellType.Default)
{
foreach (var item in _columns)
{
if (item.OnCellClick.HasDelegate)
{
await item.OnCellClick.InvokeAsync(cell);
}
}
}
}
}
19 changes: 2 additions & 17 deletions src/Core/Components/DataGrid/FluentDataGridCell.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public partial class FluentDataGridCell<TGridItem> : FluentComponentBase
/// <summary>
/// Gets a reference to the column that this cell belongs to.
/// </summary>
private ColumnBase<TGridItem>? Column => Owner.Owner.Grid._columns.ElementAtOrDefault(GridColumn - 1);
public ColumnBase<TGridItem>? Column => Owner.Owner.Grid._columns.ElementAtOrDefault(GridColumn - 1);

protected string? StyleValue => new StyleBuilder(Style)
.AddStyle("height", $"{GridContext.Grid.ItemSize:0}px", () => !GridContext.Grid.Loading && GridContext.Grid.Virtualize && Owner.RowType == DataGridRowType.Default)
Expand All @@ -64,22 +64,7 @@ protected override void OnInitialized()
Owner.Register(this);
}

/// <summary />
internal async Task HandleOnCellClickAsync()
{
if (GridContext.Grid.OnCellClick.HasDelegate)
{
await GridContext.Grid.OnCellClick.InvokeAsync(this);
}

// If the cell is a checkbox cell, add or remove the item from the selected items list.
var selectColumn = Column as SelectColumn<TGridItem>;
if (CellType == DataGridCellType.Default && selectColumn != null && selectColumn.SelectFromEntireRow == false)
{
await selectColumn.AddOrRemoveSelectedItemAsync(Item);
}
}
internal Task HandleOnCellClickAsync() => GridContext.Grid.HandleOnCellClickAsync(this);

public void Dispose() => Owner.Unregister(this);

}
51 changes: 3 additions & 48 deletions src/Core/Components/DataGrid/FluentDataGridRow.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,56 +100,11 @@ private async Task HandleOnCellFocusAsync(DataGridCellFocusEventArgs args)
}
}

/// <summary />
internal async Task HandleOnRowClickAsync(string rowId)
{
if (Owner.Rows.TryGetValue(rowId, out var row))
{
if (Owner.Grid.OnRowClick.HasDelegate)
{
await Owner.Grid.OnRowClick.InvokeAsync(row);
}

if (row != null && row.RowType == DataGridRowType.Default)
{
foreach (var selColumn in Owner.Grid.SelectColumns.Where(i => i.SelectFromEntireRow))
{
await selColumn.AddOrRemoveSelectedItemAsync(Item);
}
}
}
}
internal Task HandleOnRowClickAsync(string rowId) => Owner.Grid.HandleOnRowClickAsync(rowId);

/// <summary />
internal async Task HandleOnRowDoubleClickAsync(string rowId)
{
if (Owner.Rows.TryGetValue(rowId, out var row))
{
if (Owner.Grid.OnRowDoubleClick.HasDelegate)
{
await Owner.Grid.OnRowDoubleClick.InvokeAsync(row);
}
}
}
internal Task HandleOnRowDoubleClickAsync(string rowId) => Owner.Grid.HandleOnRowDoubleClickAsync(rowId);

/// <summary />
internal async Task HandleOnRowKeyDownAsync(string rowId, KeyboardEventArgs e)
{
// Enter when a SelectColumn is defined.
if (SelectColumn<TGridItem>.KEYBOARD_SELECT_KEYS.Contains(e.Code))
{
if (Owner.Rows.TryGetValue(rowId, out var row))
{
if (row != null && row.RowType == DataGridRowType.Default)
{
foreach (var selColumn in Owner.Grid.SelectColumns)
{
await selColumn.AddOrRemoveSelectedItemAsync(Item);
}
}
}
}
}
internal Task HandleOnRowKeyDownAsync(string rowId, KeyboardEventArgs e) => Owner.Grid.HandleOnRowKeyDownAsync(rowId,e);

/// <summary />
Task IHandleEvent.HandleEventAsync(EventCallbackWorkItem callback, object? arg)
Expand Down
8 changes: 8 additions & 0 deletions src/Core/Events/DataGridRowKeyEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.AspNetCore.Components.Web;

namespace Microsoft.FluentUI.AspNetCore.Components;

public class DataGridRowKeyEventArgs<TGridItem> : KeyboardEventArgs
{
public FluentDataGridRow<TGridItem> Row { get; set; } = default!;
}