From 397b4e48de664f780ba942ea956654a8d6adb24c Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 4 Aug 2024 11:51:42 +0200 Subject: [PATCH] Added a text slider component --- .../Components/Blocks/MudTextSlider.razor | 7 ++ .../Components/Blocks/MudTextSlider.razor.cs | 78 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 app/MindWork AI Studio/Components/Blocks/MudTextSlider.razor create mode 100644 app/MindWork AI Studio/Components/Blocks/MudTextSlider.razor.cs diff --git a/app/MindWork AI Studio/Components/Blocks/MudTextSlider.razor b/app/MindWork AI Studio/Components/Blocks/MudTextSlider.razor new file mode 100644 index 00000000..3c95b8dd --- /dev/null +++ b/app/MindWork AI Studio/Components/Blocks/MudTextSlider.razor @@ -0,0 +1,7 @@ +@typeparam T + + + + @this.Value @this.Unit + + \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/Blocks/MudTextSlider.razor.cs b/app/MindWork AI Studio/Components/Blocks/MudTextSlider.razor.cs new file mode 100644 index 00000000..c3e24111 --- /dev/null +++ b/app/MindWork AI Studio/Components/Blocks/MudTextSlider.razor.cs @@ -0,0 +1,78 @@ +using System.Numerics; + +using Microsoft.AspNetCore.Components; + +namespace AIStudio.Components.Blocks; + +public partial class MudTextSlider : ComponentBase where T : struct, INumber +{ + /// + /// The minimum value for the slider. + /// + [Parameter] + public T Min { get; set; } = T.Zero; + + /// + /// The maximum value for the slider. + /// + [Parameter] + public T Max { get; set; } = T.One; + + /// + /// The step size for the slider. + /// + [Parameter] + public T Step { get; set; } = T.One; + + /// + /// The unit to display next to the slider's value. + /// + [Parameter] + public string Unit { get; set; } = string.Empty; + + [Parameter] + public T Value { get; set; } + + [Parameter] + public EventCallback ValueChanged { get; set; } + + /// + /// The label to display above the slider. + /// + [Parameter] + public string Label { get; set; } = string.Empty; + + [Parameter] + public Func Disabled { get; set; } = () => false; + + #region Overrides of ComponentBase + + protected override async Task OnInitializedAsync() + { + await this.EnsureMinMax(); + await base.OnInitializedAsync(); + } + + protected override async Task OnParametersSetAsync() + { + await this.EnsureMinMax(); + await base.OnParametersSetAsync(); + } + + #endregion + + private async Task EnsureMinMax() + { + if (this.Value < this.Min) + await this.ValueUpdated(this.Min); + + else if(this.Value > this.Max) + await this.ValueUpdated(this.Max); + } + + private async Task ValueUpdated(T value) + { + this.Value = value; + await this.ValueChanged.InvokeAsync(this.Value); + } +} \ No newline at end of file