1+ @using ColorControl .Shared .Contracts
2+ @using ColorControl .Shared .EventDispatcher
3+ @using ColorControl .UI .Services ;
4+ @using ColorControl .Shared .Common ;
5+
6+ @* @rendermode Constants.RenderMode *@
7+
8+ @typeparam TValue where TValue : struct, IComparable, IFormattable, IConvertible, IComparable<TValue >, IEquatable<TValue >
9+
10+ @if (Id != null )
11+ {
12+ <div class =" mb-2" >
13+ <label class =" form-label @(ExtraLabelStyling ?? " " )" for =" @Id" >@Label : @GetValueDisplay()@Unit </label >
14+ <input class =" form-range" type =" range" step =" @Step" min =" @(Values?.Min() ?? Min)" max =" @(Values?.Max() ?? Max)" @bind =" Value" id =" @Id" @oninput =" e => RangeOnChange(e)" >
15+ @if (SubLabel != null )
16+ {
17+ <div id =" @(Id)_help" class =" form-text" >
18+ @SubLabel
19+ </div >
20+ }
21+ </div >
22+ }
23+
24+ @code {
25+
26+ [Parameter ]
27+ public TValue Value { get ; set ; }
28+
29+ [Parameter ]
30+ public string ? Label { get ; set ; }
31+
32+ [Parameter ]
33+ public string ? SubLabel { get ; set ; }
34+
35+ [Parameter ]
36+ public string ? Unit { get ; set ; }
37+
38+ [Parameter ]
39+ public string ? UnitString { get ; set ; }
40+
41+ [Parameter ]
42+ public string ? Id { get ; set ; }
43+
44+ [Parameter ]
45+ public TValue ? Min { get ; set ; }
46+
47+ [Parameter ]
48+ public TValue ? Max { get ; set ; }
49+
50+ [Parameter ]
51+ public TValue ? Step { get ; set ; }
52+
53+ [Parameter ]
54+ public List <TValue >? Values { get ; set ; }
55+
56+ [Parameter ]
57+ public EventCallback <TValue > ValueChanged { get ; set ; }
58+
59+ [Parameter ]
60+ public Func <TValue , string >? LabelStylingFunc { get ; set ; }
61+
62+ [Parameter ]
63+ public Func <TValue , string >? CustomLabelFunc { get ; set ; }
64+
65+ private string ? ExtraLabelStyling ;
66+
67+ protected override void OnParametersSet ()
68+ {
69+ ExtraLabelStyling = LabelStylingFunc ? .Invoke (Value );
70+ Min ??= (TValue )Convert .ChangeType (0 , typeof (TValue ));
71+ Max ??= (TValue )Convert .ChangeType (100 , typeof (TValue ));
72+ Step ??= (TValue )Convert .ChangeType (1 , typeof (TValue ));
73+ }
74+
75+ private string ? GetValueDisplay ()
76+ {
77+ if (CustomLabelFunc != null )
78+ {
79+ return CustomLabelFunc (Value );
80+ }
81+
82+ if (string .IsNullOrEmpty (UnitString ))
83+ {
84+ return Value .ToString ();
85+ }
86+
87+ if (Value is uint uintValue )
88+ {
89+ return uintValue .ToUnitString (UnitString );
90+ }
91+ if (Value is int intValue )
92+ {
93+ return intValue .ToSignedUnitString (UnitString );
94+ }
95+
96+ return Value .ToString ();
97+ }
98+
99+ private async Task RangeOnChange (ChangeEventArgs eventArgs )
100+ {
101+ if (typeof (TValue ) == typeof (int ))
102+ {
103+ Value = (TValue )Convert .ChangeType (int .Parse (eventArgs .Value ? .ToString () ?? " 0" ), typeof (TValue ));
104+
105+ }
106+ else if (typeof (TValue ) == typeof (uint ))
107+ {
108+ Value = (TValue )Convert .ChangeType (uint .Parse (eventArgs .Value ? .ToString () ?? " 0" ), typeof (TValue ));
109+ }
110+
111+ await ValueChanged .InvokeAsync (Value );
112+
113+ ExtraLabelStyling = LabelStylingFunc ? .Invoke (Value );
114+ }
115+
116+ }
0 commit comments