Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
void OnCommand(CommandEventArgs args) {

Console.WriteLine("Command fired");
TheContent = $"Command '{args.CommandName}'";
var button = (Button)args.Sender; // Access the sender!
TheContent = $"Command '{args.CommandName}' from button with text '{button.Text}'";

}

Expand Down
56 changes: 56 additions & 0 deletions src/BlazorWebFormsComponents.Test/Button/SenderProperty.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@using Moq
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Routing
@inherits BunitContext

@code {

private object capturedSender = null;
private string commandName = string.Empty;
private object commandArg = null;

void OnCommand(CommandEventArgs args)
{
capturedSender = args.Sender;
commandName = args.CommandName;
commandArg = args.CommandArgument;
}

[Fact]
public void Button_Command_PopulatesSenderProperty()
{
// Arrange
Services.AddSingleton<LinkGenerator>(new Mock<LinkGenerator>().Object);
Services.AddSingleton<IHttpContextAccessor>(new Mock<IHttpContextAccessor>().Object);
capturedSender = null;
var cut = Render(@<Button CommandName="TestCommand" CommandArgument="@("TestArg")" OnCommand="OnCommand">Test Button</Button>);

// Act
cut.Find("button").Click();

// Assert
capturedSender.ShouldNotBeNull();
capturedSender.ShouldBeOfType<Button>();
commandName.ShouldBe("TestCommand");
commandArg.ShouldBe("TestArg");
}

[Fact]
public void Button_Command_SenderIsTheButtonInstance()
{
// Arrange
Services.AddSingleton<LinkGenerator>(new Mock<LinkGenerator>().Object);
Services.AddSingleton<IHttpContextAccessor>(new Mock<IHttpContextAccessor>().Object);
Button buttonRef = null;
capturedSender = null;
var cut = Render(@<Button @ref="buttonRef" CommandName="TestCmd" CommandArgument="@("Arg")" OnCommand="OnCommand" Text="Click" />);
var buttonInstance = cut.FindComponent<Button>().Instance;

// Act
cut.Find("button").Click();

// Assert
capturedSender.ShouldBe(buttonInstance);
}

}
61 changes: 61 additions & 0 deletions src/BlazorWebFormsComponents.Test/DataList/SenderProperty.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
@using SharedSampleObjects.Models
@using Moq
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Routing
@inherits BunitContext

@code {

private object capturedSender = null;
private object capturedItem = null;

void OnItemDataBound(DataListItemEventArgs args)
{
capturedSender = args.Sender;
capturedItem = args.Item;
}

[Fact]
public void DataList_ItemDataBound_PopulatesSenderProperty()
{
// Arrange
Services.AddSingleton<LinkGenerator>(new Mock<LinkGenerator>().Object);
Services.AddSingleton<IHttpContextAccessor>(new Mock<IHttpContextAccessor>().Object);
capturedSender = null;
capturedItem = null;
var widgets = Widget.SimpleWidgetList;

var cut = Render(@<DataList Items="widgets" OnItemDataBound="OnItemDataBound">
<ItemTemplate>
<span>@context.Name</span>
</ItemTemplate>
</DataList>);

// Assert - Sender should be set for each item
capturedSender.ShouldNotBeNull();
capturedSender.ShouldBeOfType<DataList<Widget>>();
capturedItem.ShouldNotBeNull();
}

[Fact]
public void DataList_ItemDataBound_SenderIsDataListInstance()
{
// Arrange
Services.AddSingleton<LinkGenerator>(new Mock<LinkGenerator>().Object);
Services.AddSingleton<IHttpContextAccessor>(new Mock<IHttpContextAccessor>().Object);
capturedSender = null;
var widgets = new[] { new Widget { Name = "Test Widget" } };

var cut = Render(@<DataList Items="widgets" OnItemDataBound="OnItemDataBound">
<ItemTemplate>
<span>@context.Name</span>
</ItemTemplate>
</DataList>);

var dataListInstance = cut.FindComponent<DataList<Widget>>().Instance;

// Assert
capturedSender.ShouldBe(dataListInstance);
}

}
5 changes: 5 additions & 0 deletions src/BlazorWebFormsComponents/AdCreatedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ public AdCreatedEventArgs(IDictionary adProperties)
public string ImageUrl { get; set; }

public string NavigateUrl { get; set; }

/// <summary>
/// The component that raised this event
/// </summary>
public object Sender { get; set; }
}
}
3 changes: 2 additions & 1 deletion src/BlazorWebFormsComponents/AdRotator.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ internal Advertisment GetActiveAdvertisment()
{
AlternateText = advertisment.AlternateText,
ImageUrl = advertisment.ImageUrl,
NavigateUrl = advertisment.NavigateUrl
NavigateUrl = advertisment.NavigateUrl,
Sender = this
};

AdCreated(adArgs);
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorWebFormsComponents/ButtonBaseComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected void Click()
{
if (!string.IsNullOrEmpty(CommandName))
{
var args = new CommandEventArgs(CommandName, CommandArgument);
var args = new CommandEventArgs(CommandName, CommandArgument) { Sender = this };
OnCommand.InvokeAsync(args);
OnBubbledEvent(this, args);
}
Expand Down
5 changes: 5 additions & 0 deletions src/BlazorWebFormsComponents/CommandEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public CommandEventArgs(CommandEventArgs args)

public object CommandArgument { get; set; }

/// <summary>
/// The component that raised this event
/// </summary>
public object Sender { get; set; }

}

}
4 changes: 2 additions & 2 deletions src/BlazorWebFormsComponents/DataList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<td style="@(even ? ItemStyle : AlternatingItemTemplate == null ? ItemStyle : AlternatingItemStyle)">@(even ? ItemTemplate(item) : AlternatingItemTemplate == null ? ItemTemplate(item) : AlternatingItemTemplate(item))</td>
</CascadingValue>

ItemDataBound(new DataListItemEventArgs(item));
ItemDataBound(new DataListItemEventArgs(item) { Sender = this });

idx++;
first = false;
Expand Down Expand Up @@ -123,7 +123,7 @@
}
<span style="@(even ? ItemStyle : AlternatingItemTemplate == null ? ItemStyle : AlternatingItemStyle)">@(even ? ItemTemplate(item) : AlternatingItemTemplate == null ? ItemTemplate(item) : AlternatingItemTemplate(item))</span>
</CascadingValue>
ItemDataBound(new DataListItemEventArgs(item));
ItemDataBound(new DataListItemEventArgs(item) { Sender = this });
first = false;
idx++;
} while ((idx - 1) % RepeatColumns != 0 && idx <= IndexedItems.Count());
Expand Down
5 changes: 5 additions & 0 deletions src/BlazorWebFormsComponents/DataListItemEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ public DataListItemEventArgs(object item)
}

public object Item { get; }

/// <summary>
/// The component that raised this event
/// </summary>
public object Sender { get; set; }
}
}
20 changes: 10 additions & 10 deletions src/BlazorWebFormsComponents/FormView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,35 +93,35 @@ private void HandleCommandArgs(CommandEventArgs args)
switch (args.CommandName.ToLowerInvariant())
{
case "cancel":
ModeChanging.InvokeAsync(new FormViewModeEventArgs() { NewMode = DefaultMode }).GetAwaiter().GetResult();
ModeChanging.InvokeAsync(new FormViewModeEventArgs() { NewMode = DefaultMode, Sender = this }).GetAwaiter().GetResult();
CurrentMode = DefaultMode;
break;
case "edit":
ModeChanging.InvokeAsync(new FormViewModeEventArgs() { NewMode = FormViewMode.Edit }).GetAwaiter().GetResult();
ModeChanging.InvokeAsync(new FormViewModeEventArgs() { NewMode = FormViewMode.Edit, Sender = this }).GetAwaiter().GetResult();
CurrentMode = FormViewMode.Edit;
break;
case "delete":
Exception caughtException = null;
try {
OnItemDeleting.InvokeAsync(new FormViewDeleteEventArgs(Position)).GetAwaiter().GetResult();
OnItemDeleting.InvokeAsync(new FormViewDeleteEventArgs(Position) { Sender = this }).GetAwaiter().GetResult();
} catch (Exception ex) {
caughtException = ex;
}
// do we do the deletion?
OnItemDeleted.InvokeAsync(new FormViewDeletedEventArgs(Position, caughtException)).GetAwaiter().GetResult();
OnItemDeleted.InvokeAsync(new FormViewDeletedEventArgs(Position, caughtException) { Sender = this }).GetAwaiter().GetResult();
CurrentMode = DefaultMode;
Position = (Position == 0) ? 0 : Position - 1;
break;
case "insert":
OnItemInserting.InvokeAsync(new FormViewInsertEventArgs("insert") { }).GetAwaiter().GetResult();
ModeChanging.InvokeAsync(new FormViewModeEventArgs() { NewMode = FormViewMode.Insert }).GetAwaiter().GetResult();
OnItemInserted.InvokeAsync(new FormViewInsertEventArgs("insert") { }).GetAwaiter().GetResult();
OnItemInserting.InvokeAsync(new FormViewInsertEventArgs("insert") { Sender = this }).GetAwaiter().GetResult();
ModeChanging.InvokeAsync(new FormViewModeEventArgs() { NewMode = FormViewMode.Insert, Sender = this }).GetAwaiter().GetResult();
OnItemInserted.InvokeAsync(new FormViewInsertEventArgs("insert") { Sender = this }).GetAwaiter().GetResult();
CurrentMode = FormViewMode.Insert;
break;
case "update":
OnItemUpdating.InvokeAsync(new FormViewUpdateEventArgs("update")).GetAwaiter().GetResult();
ModeChanging.InvokeAsync(new FormViewModeEventArgs() { NewMode = DefaultMode }).GetAwaiter().GetResult();
OnItemUpdated.InvokeAsync(new FormViewUpdatedEventArgs(0, null)).GetAwaiter().GetResult();
OnItemUpdating.InvokeAsync(new FormViewUpdateEventArgs("update") { Sender = this }).GetAwaiter().GetResult();
ModeChanging.InvokeAsync(new FormViewModeEventArgs() { NewMode = DefaultMode, Sender = this }).GetAwaiter().GetResult();
OnItemUpdated.InvokeAsync(new FormViewUpdatedEventArgs(0, null) { Sender = this }).GetAwaiter().GetResult();
CurrentMode = DefaultMode;
break;

Expand Down
5 changes: 5 additions & 0 deletions src/BlazorWebFormsComponents/FormViewDeleteEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public FormViewDeleteEventArgs(int rowIndex)

public IOrderedDictionary Values { get; internal set; }

/// <summary>
/// The component that raised this event
/// </summary>
public object Sender { get; set; }

}

}
5 changes: 5 additions & 0 deletions src/BlazorWebFormsComponents/FormViewDeletedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public FormViewDeletedEventArgs(int affectedRows, Exception ex)

public IOrderedDictionary Values { get; internal set; }

/// <summary>
/// The component that raised this event
/// </summary>
public object Sender { get; set; }


}

Expand Down
5 changes: 5 additions & 0 deletions src/BlazorWebFormsComponents/FormViewInsertEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public FormViewInsertEventArgs(string commandArgument)

public object CommandArgument { get; }

/// <summary>
/// The component that raised this event
/// </summary>
public object Sender { get; set; }

}

}
5 changes: 5 additions & 0 deletions src/BlazorWebFormsComponents/FormViewModeEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public class FormViewModeEventArgs : EventArgs

public FormViewMode NewMode { get; set; }

/// <summary>
/// The component that raised this event
/// </summary>
public object Sender { get; set; }

}

}
5 changes: 5 additions & 0 deletions src/BlazorWebFormsComponents/FormViewUpdateEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public FormViewUpdateEventArgs(object commandArgument)

public IOrderedDictionary NewValues { get; set; }

/// <summary>
/// The component that raised this event
/// </summary>
public object Sender { get; set; }


}

Expand Down
5 changes: 5 additions & 0 deletions src/BlazorWebFormsComponents/FormViewUpdatedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public FormViewUpdatedEventArgs(int affectedRows, Exception e = null)

public IOrderedDictionary NewValues { get; set; }

/// <summary>
/// The component that raised this event
/// </summary>
public object Sender { get; set; }


}

Expand Down
2 changes: 1 addition & 1 deletion src/BlazorWebFormsComponents/ListView.razor
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ else
@ItemSeparatorTemplate
}

ItemDataBound(new ListViewItemEventArgs(item));
ItemDataBound(new ListViewItemEventArgs(item) { Sender = this });
}

DataBound(EventArgs.Empty);
Expand Down
5 changes: 5 additions & 0 deletions src/BlazorWebFormsComponents/ListViewItemEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ public ListViewItemEventArgs(object item)
}

public object Item { get; }

/// <summary>
/// The component that raised this event
/// </summary>
public object Sender { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ public class AuthenticateEventArgs : EventArgs

public bool Authenticated { get; set; }

/// <summary>
/// The component that raised this event
/// </summary>
public object Sender { get; set; }

}
}
4 changes: 2 additions & 2 deletions src/BlazorWebFormsComponents/LoginControls/Login.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public partial class Login : BaseWebFormsComponent

private async Task ValidSubmit()
{
var loginCancelEventArgs = new LoginCancelEventArgs();
var loginCancelEventArgs = new LoginCancelEventArgs() { Sender = this };
await OnLoggingIn.InvokeAsync(loginCancelEventArgs);

if (loginCancelEventArgs.Cancel)
Expand All @@ -135,7 +135,7 @@ private async Task ValidSubmit()
else
{

var authenticateEventArgs = new AuthenticateEventArgs();
var authenticateEventArgs = new AuthenticateEventArgs() { Sender = this };
await OnAuthenticate.InvokeAsync(authenticateEventArgs);

if (authenticateEventArgs.Authenticated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ public class LoginCancelEventArgs : EventArgs

public bool Cancel { get; set; }

/// <summary>
/// The component that raised this event
/// </summary>
public object Sender { get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void LoginHandle(MouseEventArgs args)
private async Task LogoutHandle(MouseEventArgs args)
{

var logoutCancelEventArgs = new LoginCancelEventArgs();
var logoutCancelEventArgs = new LoginCancelEventArgs() { Sender = this };
await OnLoggingOut.InvokeAsync(logoutCancelEventArgs);

if (!logoutCancelEventArgs.Cancel)
Expand Down
Loading