forked from dotnet/MobileBlazorBindings
-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support ReplaceChildElement in ElementManager
- Loading branch information
1 parent
42b983f
commit 974ccb9
Showing
11 changed files
with
337 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/BlazorBindings.UnitTests/Components/TestContainerComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using BlazorBindings.Core; | ||
using BlazorBindings.Maui.Extensions; | ||
using System.Collections.Immutable; | ||
|
||
namespace BlazorBindings.UnitTests.Components; | ||
|
||
public class TestContainerComponent : NativeControlComponentBase, IElementHandler, IContainerElementHandler | ||
{ | ||
private TestTargetElement _targetElement = new(); | ||
|
||
[Parameter] public int X { get; set; } | ||
[Parameter] public int Y { get; set; } | ||
[Parameter] public RenderFragment ChildContent { get; set; } | ||
|
||
protected override RenderFragment GetChildContent() => ChildContent; | ||
|
||
public override async Task SetParametersAsync(ParameterView parameters) | ||
{ | ||
await base.SetParametersAsync(parameters); | ||
_targetElement.X = X; | ||
_targetElement.Y = Y; | ||
} | ||
|
||
public object TargetElement => _targetElement; | ||
|
||
void IContainerElementHandler.AddChild(object child, int physicalSiblingIndex) | ||
{ | ||
_targetElement.Children = _targetElement.Children.Insert(physicalSiblingIndex, child.Cast<TestTargetElement>()); | ||
} | ||
|
||
void IContainerElementHandler.RemoveChild(object child, int physicalSiblingIndex) | ||
{ | ||
if (!Equals(_targetElement.Children[physicalSiblingIndex], child)) | ||
throw new InvalidOperationException("Unexpected child to remove."); | ||
|
||
_targetElement.Children = _targetElement.Children.RemoveAt(physicalSiblingIndex); | ||
} | ||
|
||
void IContainerElementHandler.ReplaceChild(int physicalSiblingIndex, object oldChild, object newChild) | ||
{ | ||
if (!Equals(_targetElement.Children[physicalSiblingIndex], oldChild)) | ||
throw new InvalidOperationException("Unexpected child to remove."); | ||
|
||
_targetElement.Children = _targetElement.Children.SetItem(physicalSiblingIndex, newChild.Cast<TestTargetElement>()); | ||
} | ||
|
||
// We want to track element state when it is added to parent, therefore mutable struct is used. | ||
public record struct TestTargetElement | ||
{ | ||
public TestTargetElement() : this(0, 0) { } | ||
|
||
public TestTargetElement(int x, int y) | ||
{ | ||
X = x; | ||
Y = y; | ||
Children = ImmutableList.Create<TestTargetElement>(); | ||
} | ||
|
||
public int X { get; set; } | ||
public int Y { get; set; } | ||
|
||
public ImmutableList<TestTargetElement> Children { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/BlazorBindings.UnitTests/NativeComponentRendererTests.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
@using BlazorBindings.UnitTests.Components; | ||
@using BlazorBindings.UnitTests.Elements; | ||
|
||
@inherits ElementTestBase | ||
|
||
@code | ||
{ | ||
[Test] | ||
public async Task TargetElementsShouldHavePropertiesSetWhenAddedToParents() | ||
{ | ||
var parent = await Render<TestContainerComponent.TestTargetElement>( | ||
@<TestContainerComponent X="1" Y="2"> | ||
<TestContainerComponent X="3" Y="4"> | ||
<WrapperWithCascadingValue> | ||
<TestContainerComponent X="5" Y="6" /> | ||
</WrapperWithCascadingValue> | ||
<TestContainerComponent X="7" Y="8" /> | ||
</TestContainerComponent> | ||
|
||
<TestContainerComponent X="9" Y="10" /> | ||
</TestContainerComponent> | ||
); | ||
|
||
Assert.That((parent.X, parent.Y), Is.EqualTo((1, 2))); | ||
|
||
var child0 = parent.Children[0]; | ||
var child1 = parent.Children[1]; | ||
|
||
Assert.That((child0.X, child0.Y), Is.EqualTo((3, 4))); | ||
Assert.That((child1.X, child1.Y), Is.EqualTo((9, 10))); | ||
|
||
var child00 = child0.Children[0]; | ||
var child01 = child0.Children[1]; | ||
|
||
Assert.That((child00.X, child00.Y), Is.EqualTo((5, 6))); | ||
Assert.That((child01.X, child01.Y), Is.EqualTo((7, 8))); | ||
} | ||
} |