Skip to content

Commit d4ede22

Browse files
committed
Added event trigger extension method for @ontoggle
1 parent d66c69d commit d4ede22

4 files changed

Lines changed: 80 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ The following section list all changes in 1.0.0 preview 01.
1111
### Added
1212
List of new features.
1313

14+
- Added support for triggering `@ontoggle` event handlers through a dedicated `Toggle()` method. By [@egil](https://github.com/egil) in [#248](https://github.com/egil/bUnit/pull/248).
15+
1416
### Changed
1517
List of changes in existing functionality.
1618

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#if NET5_0
2+
using System;
3+
using System.Threading.Tasks;
4+
using AngleSharp.Dom;
5+
6+
namespace Bunit
7+
{
8+
/// <summary>
9+
/// General event dispatch helper extension methods.
10+
/// </summary>
11+
public static class DetailsElementEventDispatcherExtensions
12+
{
13+
/// <summary>
14+
/// Raises the <c>@ontoggle</c> event on <paramref name="element"/>
15+
/// to the event handler.
16+
/// </summary>
17+
/// <param name="element">The element to raise the event on.</param>
18+
public static void Toggle(this IElement element)
19+
=> _ = ToggleAsync(element, EventArgs.Empty);
20+
21+
/// <summary>
22+
/// Raises the <c>@ontoggle</c> event on <paramref name="element"/>, passing the provided <paramref name="eventArgs"/>
23+
/// to the event handler.
24+
/// </summary>
25+
/// <param name="element">The element to raise the event on.</param>
26+
/// <param name="eventArgs">The event arguments to pass to the event handler.</param>
27+
/// <returns>A task that completes when the event handler is done.</returns>
28+
private static Task ToggleAsync(this IElement element, EventArgs eventArgs) => element.TriggerEventAsync("ontoggle", eventArgs);
29+
}
30+
}
31+
#endif
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<div>
2+
@if (detailsExpanded)
3+
{
4+
<p>Read the details carefully!</p>
5+
}
6+
<details id="details-toggle" @ontoggle="OnToggle">
7+
<summary>Summary</summary>
8+
<p>Detailed content</p>
9+
</details>
10+
</div>
11+
12+
@code {
13+
bool detailsExpanded;
14+
15+
void OnToggle()
16+
{
17+
detailsExpanded = !detailsExpanded;
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#if NET5_0
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Bunit;
8+
using Bunit.TestAssets.SampleComponents;
9+
using Shouldly;
10+
using Xunit;
11+
12+
namespace Bunit.EventDispatchExtensions
13+
{
14+
public class DetailsElementEventDispatcherExtensionsTest : TestContext
15+
{
16+
[Fact(DisplayName = "Toggle raises ontoggle events")]
17+
public void Test200()
18+
{
19+
var cut = RenderComponent<ToggleableDetails>();
20+
cut.FindAll("div > p").Count.ShouldBe(0);
21+
22+
cut.Find("details").Toggle();
23+
24+
cut.FindAll("div > p").Count.ShouldBe(1);
25+
}
26+
}
27+
}
28+
#endif

0 commit comments

Comments
 (0)