-
-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathBaseMatNumericUpDownFieldInternal.cs
More file actions
195 lines (172 loc) · 6.06 KB
/
BaseMatNumericUpDownFieldInternal.cs
File metadata and controls
195 lines (172 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace MatBlazor
{
/// <summary>
/// Material Design NumericUpDown for Blazor, text fields allow users to input, edit, and select text.
/// </summary>
/// <typeparam name="TValue">sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, decimal?</typeparam>
public class BaseMatNumericUpDownFieldInternal<TValue> : MatInputTextComponent<TValue>
{
private TValue minimum;
private TValue maximum;
protected override EventCallback<KeyboardEventArgs> OnKeyDownEvent()
{
return OnKeyDownEvent2;
}
protected void Increase()
{
CurrentValue = SwitchT.Increase(CurrentValue, Step, Max);
}
protected void Decrease()
{
CurrentValue = SwitchT.Decrease(CurrentValue, Step, Min);
}
protected override TValue CurrentValue
{
get => base.CurrentValue;
set => base.CurrentValue = SwitchT.Round(value, RoundingDecimalPlaces);
}
[Parameter]
public bool AllowInput { get; set; } = true;
[Parameter]
public TValue Min
{
get => minimum;
set
{
minimum = value;
}
}
[Parameter]
[Obsolete("Use parameter Min")]
public TValue Minimum
{
get => minimum;
set
{
minimum = value;
}
}
[Parameter]
public TValue Max
{
get => maximum;
set
{
maximum = value;
}
}
[Parameter]
[Obsolete("Use parameter Max")]
public TValue Maximum
{
get => maximum;
set
{
maximum = value;
}
}
[Parameter]
public int DecimalPlaces { get; set; } = 0;
private int RoundingDecimalPlaces => FieldType == MatNumericUpDownFieldType.Percent ? DecimalPlaces + 2 : DecimalPlaces;
[Parameter]
public TValue Step { get; set; }
[Parameter]
public MatNumericUpDownFieldType FieldType { get; set; }
public string PlusIcon { get; set; } = MatIconNames.Keyboard_arrow_down;
public string MinusIcon { get; set; } = MatIconNames.Keyboard_arrow_up;
protected override bool InputTextReadOnly()
{
return base.InputTextReadOnly() || !AllowInput;
}
private readonly EventCallback<KeyboardEventArgs> OnKeyDownEvent2;
public BaseMatNumericUpDownFieldInternal()
{
OnKeyDownEvent2 = EventCallback.Factory.Create<KeyboardEventArgs>(this, async (e) =>
{
await OnKeyDown.InvokeAsync(e);
if (e.Key == "ArrowUp")
{
Increase();
}
else if (e.Key == "ArrowDown")
{
Decrease();
}
});
Max = SwitchT.GetMaximum();
Min = SwitchT.GetMinimum();
ClassMapper.Add("mat-numeric-up-down-field");
ClassMapper.Add("mat-text-field-with-actions-container");
}
private readonly TValue ZeroValue = MatTypeConverter.ChangeType<TValue>(0);
protected override void OnParametersSet()
{
if (FieldType == MatNumericUpDownFieldType.Currency)
{
if (Step == null || Step.Equals(ZeroValue))
{
Step = SwitchT.GetStep();
}
Format = $"c{DecimalPlaces}";
}
else if (FieldType == MatNumericUpDownFieldType.Percent)
{
if (Step == null || Step.Equals(ZeroValue))
{
Step = MatTypeConverter.ChangeType<TValue>(0.01m);
}
Format = $"p{DecimalPlaces}";
}
else if (Step == null || Step.Equals(ZeroValue))
{
Step = SwitchT.GetStep();
}
}
protected async override Task OnFirstAfterRenderAsync()
{
await base.OnFirstAfterRenderAsync();
}
protected override bool TryParseValueFromString(string value, out TValue result, out string validationErrorMessage)
{
if (string.IsNullOrEmpty(value) == false)
{
if (value[^1] == '%')
{
value = value[0..^1];
}
}
var success = base.TryParseValueFromString(value, out result, out validationErrorMessage);
if (success == true && FieldType == MatNumericUpDownFieldType.Percent)
{
// The standard percent formatter assumes 1.0 is 100%, so we divide the input by 100 to make the input match the output
if (result != null)
{
if (result is decimal decimalResult)
{
result = MatTypeConverter.ChangeType<TValue>(decimalResult * 0.01m);
}
else if (result is float floatResult)
{
result = MatTypeConverter.ChangeType<TValue>(floatResult * 0.01f);
}
else if (result is double doubleResult)
{
result = MatTypeConverter.ChangeType<TValue>(doubleResult * 0.01d);
}
}
}
if (result != null) // Snap to Min/Max
{
var comparer = Comparer<TValue>.Default;
if (Max != null && comparer.Compare(result, Max) > 0) result = Max;
if (Min != null && comparer.Compare(result, Min) < 0) result = Min;
}
return success;
}
}
}