-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
| ||
namespace COMET.Web.Common.Components.CardView | ||
{ | ||
using System.Text.RegularExpressions; | ||
|
||
using FastMember; | ||
|
||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Rendering; | ||
|
||
public class CardField<T> : ComponentBase | ||
Check warning on line 11 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / build
Check warning on line 11 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / build
Check warning on line 11 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Build
Check warning on line 11 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Build
Check warning on line 11 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Analyze (csharp)
|
||
{ | ||
private static TypeAccessor typeAccessor { get; set; } | ||
|
||
static CardField() | ||
{ | ||
typeAccessor = TypeAccessor.Create(typeof(T)); | ||
} | ||
|
||
[CascadingParameter(Name="CardView")] | ||
private CardView<T> CardView { get; set; } | ||
|
||
[CascadingParameter(Name = "SearchTerm")] | ||
private string SearchTerm { get; set; } | ||
|
||
[Parameter] | ||
public T Context { get; set; } | ||
Check warning on line 27 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / build
Check warning on line 27 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / build
Check warning on line 27 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Build
Check warning on line 27 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Build
Check warning on line 27 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Analyze (csharp)
|
||
|
||
[Parameter] | ||
public string FieldName { get; set; } | ||
Check warning on line 30 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / build
Check warning on line 30 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / build
Check warning on line 30 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Build
Check warning on line 30 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Build
Check warning on line 30 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Analyze (csharp)
|
||
|
||
[Parameter] | ||
public bool AllowSort { get; set; } = true; | ||
Check warning on line 33 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / build
Check warning on line 33 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / build
Check warning on line 33 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Build
Check warning on line 33 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Build
Check warning on line 33 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Analyze (csharp)
|
||
|
||
[Parameter] | ||
public bool AllowSearch { get; set; } = true; | ||
Check warning on line 36 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / build
Check warning on line 36 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / build
Check warning on line 36 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Build
Check warning on line 36 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Build
Check warning on line 36 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Analyze (csharp)
|
||
|
||
protected override void OnInitialized() | ||
Check warning on line 38 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / build
Check warning on line 38 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / build
Check warning on line 38 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Build
Check warning on line 38 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Build
Check warning on line 38 in COMET.Web.Common/Components/CardView/CardField.cs GitHub Actions / Analyze (csharp)
|
||
{ | ||
base.OnInitialized(); | ||
this.CardView.RegisterCardField(this); | ||
} | ||
|
||
protected override void BuildRenderTree(RenderTreeBuilder builder) | ||
{ | ||
base.BuildRenderTree(builder); | ||
var value = typeAccessor[this.Context, this.FieldName].ToString(); | ||
|
||
if (this.AllowSearch && !string.IsNullOrWhiteSpace(value) && !string.IsNullOrWhiteSpace(this.SearchTerm)) | ||
{ | ||
var separatorPattern = $"({this.SearchTerm})"; | ||
var result = Regex.Split(value, separatorPattern, RegexOptions.IgnoreCase); | ||
var elementCounter = 0; | ||
|
||
foreach (var element in result) | ||
{ | ||
if (string.Equals(element, this.SearchTerm, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
builder.OpenElement(elementCounter, "span"); | ||
elementCounter++; | ||
builder.AddAttribute(elementCounter, "class", "search-mark"); | ||
elementCounter++; | ||
builder.AddContent(elementCounter, element); | ||
elementCounter++; | ||
builder.CloseElement(); | ||
} | ||
else | ||
{ | ||
builder.AddContent(elementCounter, element); | ||
elementCounter++; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
builder.AddContent(0, value); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,3 @@ | |
.card.selected { | ||
border: 2px solid var(--bs-primary, var(--primary)); | ||
} | ||
|