Skip to content

Commit

Permalink
Add reset by keyboard option to Rating (#3073)
Browse files Browse the repository at this point in the history
Co-authored-by: Denis Voituron <[email protected]>
  • Loading branch information
vnbaaij and dvoituron authored Dec 23, 2024
1 parent b189dc3 commit 40a321c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<FluentRating Max="5"
Label="Rate me!"
@bind-Value="@SelectedValue"
OnHoverValueChanged="@(i => HoverValue = i)" />
OnHoverValueChanged="@(i => HoverValue = i)"
AllowReset="true"/>
<FluentLabel>@LabelText</FluentLabel>
</FluentStack>

Expand Down
3 changes: 2 additions & 1 deletion src/Core/Components/Rating/FluentRating.razor
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
{
<input type="radio" checked="@(index == Value)" name="@GroupName"
value="@index"
@onfocus="@(() => OnClickAsync(index, fromFocus: true))" />
@onfocus="@(() => OnClickAsync(index, fromFocus: true))"
@onkeydown="@OnKeyDownAsync" />

<FluentIcon Value="@GetIcon(index)"
Width="@IconWidth"
Expand Down
18 changes: 16 additions & 2 deletions src/Core/Components/Rating/FluentRating.razor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using Microsoft.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components.Utilities;
// ------------------------------------------------------------------------
// MIT License - Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------------------

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.FluentUI.AspNetCore.Components.Utilities;

namespace Microsoft.FluentUI.AspNetCore.Components;

Expand Down Expand Up @@ -109,6 +114,15 @@ private async Task OnClickAsync(int value, bool fromFocus = false)
}
}

private async Task OnKeyDownAsync(KeyboardEventArgs args)
{
if (AllowReset && args.Key == " ")
{
await SetCurrentValueAsync(0);
await UpdateHoverValueAsync(null);
}
}

/// <summary />
private async Task OnMouseEnterAsync(int value)
{
Expand Down

0 comments on commit 40a321c

Please sign in to comment.