In RazorCompiler, support attribute values of type UIEventHandler

This commit is contained in:
Steve Sanderson 2018-01-16 16:15:46 +00:00
parent 3a7b6b2178
commit 7bbb2b6660
4 changed files with 36 additions and 39 deletions

View File

@ -6,5 +6,5 @@ namespace Microsoft.Blazor.RenderTree
/// <summary>
/// Handles an event raised for a <see cref="RenderTreeNode"/>.
/// </summary>
public delegate void UIEventHandler(UIEventArgs eventInfo);
public delegate void UIEventHandler(UIEventArgs eventArgs);
}

View File

@ -179,7 +179,7 @@ namespace Microsoft.Blazor.Build.Test
+ "<elem attr=@myValue />");
// Assert
Assert.Collection(GetRenderTree(component).Where(NotWhitespace),
Assert.Collection(GetRenderTree(component),
node => AssertNode.Element(node, "elem", 1),
node => AssertNode.Attribute(node, "attr", "My string"));
}
@ -193,7 +193,7 @@ namespace Microsoft.Blazor.Build.Test
+ "<elem attr=@myValue />");
// Assert
Assert.Collection(GetRenderTree(component).Where(NotWhitespace),
Assert.Collection(GetRenderTree(component),
node => AssertNode.Element(node, "elem", 1),
node => AssertNode.Attribute(node, "attr", "123"));
}
@ -207,11 +207,31 @@ namespace Microsoft.Blazor.Build.Test
+ "<elem attr=\"Hello, @myValue.ToUpperInvariant() with number @(myNum*2)!\" />");
// Assert
Assert.Collection(GetRenderTree(component).Where(NotWhitespace),
Assert.Collection(GetRenderTree(component),
node => AssertNode.Element(node, "elem", 1),
node => AssertNode.Attribute(node, "attr", "Hello, WORLD with number 246!"));
}
[Fact]
public void SupportsAttributesWithEventHandlerValues()
{
// Arrange/Act
var component = CompileToComponent(
"<elem attr=@MyHandleEvent />"
+ @"@functions {
void MyHandleEvent(Microsoft.Blazor.RenderTree.UIEventArgs eventArgs) {}
}");
// Assert
Assert.Collection(GetRenderTree(component),
node => AssertNode.Element(node, "elem", 1),
node =>
{
Assert.Equal(RenderTreeNodeType.Attribute, node.NodeType);
Assert.NotNull(node.AttributeEventHandlerValue);
});
}
private static bool NotWhitespace(RenderTreeNode node)
=> node.NodeType != RenderTreeNodeType.Text
|| !string.IsNullOrWhiteSpace(node.TextContent);

View File

@ -1,35 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Blazor.Components;
using Microsoft.Blazor.RenderTree;
namespace BasicTestApp
{
public class CounterComponent : IComponent
{
private int currentCount = 0;
public void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement("h1");
builder.AddText("Counter");
builder.CloseElement();
builder.OpenElement("p");
builder.AddText("Current count: ");
builder.AddText(currentCount.ToString());
builder.CloseElement();
builder.OpenElement("button");
builder.AddAttribute("onclick", OnButtonClicked);
builder.AddText("Click me");
builder.CloseElement();
}
private void OnButtonClicked(UIEventArgs eventInfo)
{
currentCount++;
}
}
}

View File

@ -0,0 +1,12 @@
<h1>Counter</h1>
<p>Current count: @currentCount.ToString()</p>
<button onclick=@OnButtonClicked>Click me</button>
@functions {
int currentCount = 0;
void OnButtonClicked(Microsoft.Blazor.RenderTree.UIEventArgs eventArgs)
{
currentCount++;
}
}