-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#383 - Generic component for sort descriptor settings.
- Loading branch information
Showing
3 changed files
with
76 additions
and
18 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/Money.Blazor.Host/Components/Settings/SortDescriptorEditor.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,20 @@ | ||
@typeparam T where T : struct | ||
|
||
<div class="form-group"> | ||
<label for="summary-sort-property">Field:</label> | ||
<select class="form-control" id="summary-sort-property" @onchange="OnPropertyChanged"> | ||
@foreach (var item in Properties) | ||
{ | ||
<option value="@item.Value" selected="@(item.Value.Equals(Property))">@item.Name</option> | ||
} | ||
</select> | ||
</div> | ||
<div class="form-group"> | ||
<label for="summary-sort-order">Direction:</label> | ||
<select class="form-control" id="summary-sort-order" @onchange="OnDirectionChanged"> | ||
@foreach (var item in Directions) | ||
{ | ||
<option value="@item.Value" selected="@(item.Value.Equals(Direction))">@item.Name</option> | ||
} | ||
</select> | ||
</div> |
55 changes: 55 additions & 0 deletions
55
src/Money.Blazor.Host/Components/Settings/SortDescriptorEditor.razor.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,55 @@ | ||
using Microsoft.AspNetCore.Components; | ||
using Money.Events; | ||
using Money.Models; | ||
using Money.Models.Sorting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Money.Components.Settings | ||
{ | ||
public partial class SortDescriptorEditor<T> | ||
{ | ||
protected List<(string Name, T Value)> Properties { get; set; } | ||
protected List<(string Name, SortDirection Value)> Directions { get; set; } | ||
|
||
[Parameter] | ||
public T Property { get; set; } | ||
|
||
[Parameter] | ||
public Action<T> PropertyChanged { get; set; } | ||
|
||
[Parameter] | ||
public SortDirection Direction { get; set; } | ||
|
||
[Parameter] | ||
public Action<SortDirection> DirectionChanged { get; set; } | ||
|
||
protected override void OnInitialized() | ||
{ | ||
Properties = new List<(string Name, T Value)>(); | ||
SortButton<T>.BuildItems(Properties); | ||
|
||
Directions = new List<(string Name, SortDirection Value)>(); | ||
SortButton<SortDirection>.BuildItems(Directions); | ||
} | ||
|
||
protected void OnPropertyChanged(ChangeEventArgs e) | ||
=> ParseAndRaise<T>(e, PropertyChanged); | ||
|
||
protected void OnDirectionChanged(ChangeEventArgs e) | ||
=> ParseAndRaise<SortDirection>(e, DirectionChanged); | ||
|
||
private void ParseAndRaise<TEnum>(ChangeEventArgs e, Action<TEnum> handler) | ||
where TEnum : struct | ||
{ | ||
string rawValue = e.Value?.ToString(); | ||
if (Enum.TryParse(rawValue, out TEnum value)) | ||
handler?.Invoke(value); | ||
} | ||
} | ||
} |
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