Skip to content

Commit

Permalink
MudDataGrid: Add FormFieldChanged event (#5689)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Technician committed Nov 8, 2022
1 parent 4acb6f8 commit a36b2d7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@namespace MudBlazor.UnitTests.TestComponents

<MudDialogProvider />

<MudDataGrid T="Item" Items="@_items" ReadOnly="false" EditMode="@DataGridEditMode.Form" EditTrigger="DataGridEditTrigger.OnRowClick" FormFieldChanged="((args) => FormFieldChangedEventArgs = args)">
<Columns>
<Column T="Item" Field="@nameof(Item.Name)" />
</Columns>
</MudDataGrid>

@code {
public MudBlazor.Utilities.FormFieldChangedEventArgs FormFieldChangedEventArgs { get; private set; }

private IEnumerable<Item>
_items = new List<Item>()
{
new Item("A"),
new Item("B"),
new Item("C")
};


public class Item
{
public string Name { get; set; }

public Item(string name)
{
Name = name;
}
}
}
19 changes: 19 additions & 0 deletions src/MudBlazor.UnitTests/Components/DataGridTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,25 @@ public async Task DataGridDialogEditTest()
//if no crash occurs, we know the datagrid is properly filtering out the GetOnly property when calling set
}

/// <summary>
/// DataGrid edit form should trigger the FormFieldChanged event
/// </summary>
[Test]
public async Task DataGridFormFieldChangedTest()
{
var comp = Context.RenderComponent<DataGridFormFieldChangedTest>();
var dataGrid = comp.FindComponent<MudDataGrid<DataGridFormFieldChangedTest.Item>>();
//open edit dialog
dataGrid.FindAll("tbody tr")[0].Click();

//edit data
comp.Find("div input").Change("J K Simmons");
comp.Instance.FormFieldChangedEventArgs.NewValue.Should().Be("J K Simmons");

var textfield = comp.FindComponent<MudTextField<string>>();
Assert.AreSame(comp.Instance.FormFieldChangedEventArgs.Field, textfield.Instance);
}

[Test]
public async Task DataGridVisualStylingTest()
{
Expand Down
2 changes: 1 addition & 1 deletion src/MudBlazor/Components/DataGrid/MudDataGrid.razor
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
</MudText>
</TitleContent>
<DialogContent>
<MudForm>
<MudForm FieldChanged="FormFieldChanged">
@foreach (var column in RenderedColumns)
{
var cell = new Cell<T>(this, column, _editingItem);
Expand Down
5 changes: 5 additions & 0 deletions src/MudBlazor/Components/DataGrid/MudDataGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ protected int numPages
/// </summary>
[Parameter] public EventCallback<T> CommittedItemChanges { get; set; }

/// <summary>
/// Callback is called when a field changes in the dialog MudForm. Only works in EditMode.Form
/// </summary>
[Parameter] public EventCallback<FormFieldChangedEventArgs> FormFieldChanged { get; set; }

#endregion

#region Parameters
Expand Down

0 comments on commit a36b2d7

Please sign in to comment.