Skip to content

Commit

Permalink
#383 - Generic component for sort descriptor settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Jun 5, 2022
1 parent eea05a7 commit 1c20e43
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
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>
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);
}
}
}
19 changes: 1 addition & 18 deletions src/Money.Blazor.Host/Pages/Users/Settings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,5 @@
</PropertyDialog>

<PropertyDialog @ref="SummarySortEditor" Model="@SummarySort" Title="Set summary sort">
<div class="form-group">
<label for="summary-sort-property">Field:</label>
<select class="form-control" id="summary-sort-property" @bind="SummarySort.Property">
@foreach (var item in SummarySort.Properties)
{
<option value="@item.Value">@item.Name</option>
}
</select>
</div>
<div class="form-group">
<label for="summary-sort-order">Direction:</label>
<select class="form-control" id="summary-sort-order" @bind="SummarySort.Direction">
@foreach (var item in SummarySort.Directions)
{
<option value="@item.Value">@item.Name</option>
}
</select>
</div>
<SortDescriptorEditor T="SummarySortType" @bind-Property="SummarySort.Property" @bind-Direction="SummarySort.Direction" />
</PropertyDialog>

0 comments on commit 1c20e43

Please sign in to comment.