Skip to content

Commit

Permalink
Toggle Headless UI components on button click (#8)
Browse files Browse the repository at this point in the history
* toggle menu, listbox and popover when clicking the button instead of just opening it

* add tests for #7
  • Loading branch information
DavidVollmers authored Aug 31, 2023
1 parent 589fe64 commit 9b60a93
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private void Click()

if (@event.CancellationToken.IsCancellationRequested) return;

Listbox.Open();
if (Listbox.IsOpen) Listbox.Close();
else Listbox.Open();
}
}
5 changes: 3 additions & 2 deletions packages/Tailwind/Ignis.Components.HeadlessUI/MenuButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ private void Click()
OnClick.InvokeAsync(@event);

if (@event.CancellationToken.IsCancellationRequested) return;

Menu.Open();

if (Menu.IsOpen) Menu.Close();
else Menu.Open();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private void Click()

if (@event.CancellationToken.IsCancellationRequested) return;

Popover.Open();
if (Popover.IsOpen) Popover.Close();
else Popover.Open();
}
}
46 changes: 46 additions & 0 deletions tests/Ignis.Tests.Components.HeadlessUI/ListboxTests.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@inherits TestContext

@code
{
[Fact]
public void Button_OnClick()
{
Services.AddIgnis();
Services.AddSingleton<IHostContext, TestHostContext>();

JSInterop.Mode = JSRuntimeMode.Loose;

var cut = Render(@<Listbox TValue="object">
<ListboxButton>
</ListboxButton>
</Listbox>);

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

var listbox = cut.FindComponent<Listbox<object>>();
Assert.True(listbox.Instance.IsOpen);

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

Assert.False(listbox.Instance.IsOpen);
}

[Fact]
public void Button_OnClick_PreventDefault()
{
Services.AddIgnis();
Services.AddSingleton<IHostContext, TestHostContext>();

JSInterop.Mode = JSRuntimeMode.Loose;

var cut = Render(@<Listbox TValue="object">
<ListboxButton OnClick="e => e.PreventDefault()">
</ListboxButton>
</Listbox>);

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

var listbox = cut.FindComponent<Listbox<object>>();
Assert.False(listbox.Instance.IsOpen);
}
}
46 changes: 46 additions & 0 deletions tests/Ignis.Tests.Components.HeadlessUI/MenuTests.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@inherits TestContext

@code
{
[Fact]
public void Button_OnClick()
{
Services.AddIgnis();
Services.AddSingleton<IHostContext, TestHostContext>();

JSInterop.Mode = JSRuntimeMode.Loose;

var cut = Render(@<Menu>
<MenuButton>
</MenuButton>
</Menu>);

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

var menu = cut.FindComponent<Menu>();
Assert.True(menu.Instance.IsOpen);

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

Assert.False(menu.Instance.IsOpen);
}

[Fact]
public void Button_OnClick_PreventDefault()
{
Services.AddIgnis();
Services.AddSingleton<IHostContext, TestHostContext>();

JSInterop.Mode = JSRuntimeMode.Loose;

var cut = Render(@<Menu>
<MenuButton OnClick="e => e.PreventDefault()">
</MenuButton>
</Menu>);

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

var menu = cut.FindComponent<Menu>();
Assert.False(menu.Instance.IsOpen);
}
}
4 changes: 4 additions & 0 deletions tests/Ignis.Tests.Components.HeadlessUI/PopoverTests.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

var popover = cut.FindComponent<Popover>();
Assert.True(popover.Instance.IsOpen);

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

Assert.False(popover.Instance.IsOpen);
}

[Fact]
Expand Down

0 comments on commit 9b60a93

Please sign in to comment.