Skip to content

Commit

Permalink
Added keyboard toggle to BooleanSelect component
Browse files Browse the repository at this point in the history
  • Loading branch information
jschick04 authored and bill-long committed Jul 18, 2024
1 parent c16b680 commit 488602f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/EventLogExpert/Shared/Components/BooleanSelect.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

<div aria-label="@AriaLabel" class="toggle" role="radiogroup">
<input aria-label="@DisabledString" checked="@(!Value)" id="@Id false" name="@Id" @onchange="UpdateValue" type="radio" value="false" />
<label data-single-color="@IsSingleColor.ToString().ToLower()" for="@Id false">@DisabledString</label>
<label data-single-color="@IsSingleColor.ToString().ToLower()" for="@Id false" @onkeydown="ToggleValue" tabindex="0">@DisabledString</label>

<input aria-label="@EnabledString" checked="@(Value)" id="@Id true" name="@Id" @onchange="UpdateValue" type="radio" value="true" />
<label data-single-color="@IsSingleColor.ToString().ToLower()" for="@Id true">@EnabledString</label>
<label data-single-color="@IsSingleColor.ToString().ToLower()" for="@Id true" @onkeydown="ToggleValue" tabindex="0">@EnabledString</label>
</div>
13 changes: 13 additions & 0 deletions src/EventLogExpert/Shared/Components/BooleanSelect.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using EventLogExpert.Shared.Base;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

namespace EventLogExpert.Shared.Components;

Expand All @@ -18,6 +19,18 @@ public sealed partial class BooleanSelect : BaseComponent<bool>

[Parameter] public bool IsSingleColor { get; set; }

private async Task ToggleValue(KeyboardEventArgs args)
{
switch (args)
{
case { Code: "Space" or "Enter" }:
Value = !Value;
await ValueChanged.InvokeAsync(Value);

break;
}
}

private async Task UpdateValue(ChangeEventArgs args)
{
if (!bool.TryParse(args.Value?.ToString(), out bool value)) { return; }
Expand Down

0 comments on commit 488602f

Please sign in to comment.