forked from MudBlazor/MudBlazor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MudNumericField.razor.cs
441 lines (390 loc) · 16.4 KB
/
MudNumericField.razor.cs
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright (c) MudBlazor 2022
// MudBlazor licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using MudBlazor.Services;
using MudBlazor.Utilities;
namespace MudBlazor
{
public partial class MudNumericField<T> : MudDebouncedInput<T>
{
private IKeyInterceptor _keyInterceptor;
private Comparer _comparer = new(CultureInfo.InvariantCulture);
public MudNumericField() : base()
{
Validation = new Func<T, Task<bool>>(ValidateInput);
#region parameters default depending on T
//sbyte
if (typeof(T) == typeof(sbyte) || typeof(T) == typeof(sbyte?))
{
_minDefault = (T)(object)sbyte.MinValue;
_maxDefault = (T)(object)sbyte.MaxValue;
_stepDefault = (T)(object)(sbyte)1;
}
// byte
else if (typeof(T) == typeof(byte) || typeof(T) == typeof(byte?))
{
_minDefault = (T)(object)byte.MinValue;
_maxDefault = (T)(object)byte.MaxValue;
_stepDefault = (T)(object)(byte)1;
}
// short
else if (typeof(T) == typeof(short) || typeof(T) == typeof(short?))
{
_minDefault = (T)(object)short.MinValue;
_maxDefault = (T)(object)short.MaxValue;
_stepDefault = (T)(object)(short)1;
}
// ushort
else if (typeof(T) == typeof(ushort) || typeof(T) == typeof(ushort?))
{
_minDefault = (T)(object)ushort.MinValue;
_maxDefault = (T)(object)ushort.MaxValue;
_stepDefault = (T)(object)(ushort)1;
}
// int
else if (typeof(T) == typeof(int) || typeof(T) == typeof(int?))
{
_minDefault = (T)(object)int.MinValue;
_maxDefault = (T)(object)int.MaxValue;
_stepDefault = (T)(object)1;
}
// uint
else if (typeof(T) == typeof(uint) || typeof(T) == typeof(uint?))
{
_minDefault = (T)(object)uint.MinValue;
_maxDefault = (T)(object)uint.MaxValue;
_stepDefault = (T)(object)1u;
}
// long
else if (typeof(T) == typeof(long) || typeof(T) == typeof(long?))
{
_minDefault = (T)(object)long.MinValue;
_maxDefault = (T)(object)long.MaxValue;
_stepDefault = (T)(object)1L;
}
// ulong
else if (typeof(T) == typeof(ulong) || typeof(T) == typeof(ulong?))
{
_minDefault = (T)(object)ulong.MinValue;
_maxDefault = (T)(object)ulong.MaxValue;
_stepDefault = (T)(object)1ul;
}
// float
else if (typeof(T) == typeof(float) || typeof(T) == typeof(float?))
{
_minDefault = (T)(object)float.MinValue;
_maxDefault = (T)(object)float.MaxValue;
_stepDefault = (T)(object)1.0f;
}
// double
else if (typeof(T) == typeof(double) || typeof(T) == typeof(double?))
{
_minDefault = (T)(object)double.MinValue;
_maxDefault = (T)(object)double.MaxValue;
_stepDefault = (T)(object)1.0;
}
// decimal
else if (typeof(T) == typeof(decimal) || typeof(T) == typeof(decimal?))
{
_minDefault = (T)(object)decimal.MinValue;
_maxDefault = (T)(object)decimal.MaxValue;
_stepDefault = (T)(object)1M;
}
#endregion parameters default depending on T
}
protected string Classname =>
new CssBuilder("mud-input-input-control mud-input-number-control " +
(HideSpinButtons ? "mud-input-nospin" : "mud-input-showspin"))
.AddClass(Class)
.Build();
[Inject] private IKeyInterceptorFactory _keyInterceptorFactory { get; set; }
private string _elementId = "numericField_" + Guid.NewGuid().ToString().Substring(0, 8);
private MudInput<string> _elementReference;
[ExcludeFromCodeCoverage]
public override ValueTask FocusAsync()
{
return _elementReference.FocusAsync();
}
[ExcludeFromCodeCoverage]
public override ValueTask BlurAsync()
{
return _elementReference.BlurAsync();
}
[ExcludeFromCodeCoverage]
public override ValueTask SelectAsync()
{
return _elementReference.SelectAsync();
}
[ExcludeFromCodeCoverage]
public override ValueTask SelectRangeAsync(int pos1, int pos2)
{
return _elementReference.SelectRangeAsync(pos1, pos2);
}
protected override Task SetValueAsync(T value, bool updateText = true)
{
bool valueChanged;
(value, valueChanged) = ConstrainBoundaries(value);
return base.SetValueAsync(value, valueChanged || updateText);
}
protected internal override async void OnBlurred(FocusEventArgs obj)
{
base.OnBlurred(obj);
await UpdateValuePropertyAsync(true); //Required to set the value after a blur before the debounce period has elapsed
await UpdateTextPropertyAsync(false); //Required to update the string formatting after a blur before the debouce period has elapsed
}
protected async Task<bool> ValidateInput(T value)
{
bool valueChanged;
(value, valueChanged) = ConstrainBoundaries(value);
if (valueChanged)
await SetValueAsync(value, true);
return true; //Don't show errors
}
/// <summary>
/// Show clear button.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.Behavior)]
public bool Clearable { get; set; } = false;
/// <summary>
/// Decrements or increments depending on factor
/// </summary>
/// <param name="factor">Multiplication factor (1 or -1) will be applied to the step</param>
private async Task Change(double factor = 1)
{
try
{
var nextValue = GetNextValue(factor);
// validate that the data type is a value type before we compare them
if (typeof(T).IsValueType)
{
if (factor > 0 && _comparer.Compare(nextValue, Value) < 0)
nextValue = Max;
else if (factor < 0 && (_comparer.Compare(nextValue, Value) > 0 || nextValue is null))
nextValue = Min;
}
await SetValueAsync(ConstrainBoundaries(nextValue).value);
_elementReference.SetText(Text).AndForget();
}
catch (OverflowException)
{
// if next value overflows the primitive type, lets set it to Min or Max depending if factor is positive or negative
await SetValueAsync(factor > 0 ? Max : Min, true);
}
}
private T GetNextValue(double factor)
{
if (typeof(T) == typeof(decimal) || typeof(T) == typeof(decimal?))
return (T)(object)Convert.ToDecimal(FromDecimal(Value) + FromDecimal(Step) * (decimal)factor);
if (typeof(T) == typeof(long) || typeof(T) == typeof(long?))
return (T)(object)Convert.ToInt64(FromInt64(Value) + FromInt64(Step) * factor);
if (typeof(T) == typeof(ulong) || typeof(T) == typeof(ulong?))
return (T)(object)Convert.ToUInt64(FromUInt64(Value) + FromUInt64(Step) * factor);
return Num.To<T>(Num.From(Value) + Num.From(Step) * factor);
}
/// <summary>
/// Adds a Step to the Value
/// </summary>
public Task Increment() => Change(factor: 1);
/// <summary>
/// Substracts a Step from the Value
/// </summary>
public Task Decrement() => Change(factor: -1);
/// <summary>
/// Checks if the value respects the boundaries set for this instance.
/// </summary>
/// <param name="value">Value to check.</param>
/// <returns>Returns a valid value and if it has been changed.</returns>
protected (T value, bool changed) ConstrainBoundaries(T value)
{
if (value == null)
return (default(T), false);
// validate that the data type is a value type before we compare them
if (typeof(T).IsValueType)
{
// check if value is bigger than defined MAX, if so take the defined MAX value instead
if (_comparer.Compare(value, Max) > 0)
return (Max, true);
// check if value is lower than defined MIN, if so take the defined MIN value instead
if (_comparer.Compare(value, Min) < 0)
return (Min, true);
};
return (value, false);
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_keyInterceptor = _keyInterceptorFactory.Create();
await _keyInterceptor.Connect(_elementId, new KeyInterceptorOptions()
{
//EnableLogging = true,
TargetClass = "mud-input-slot",
Keys = {
new KeyOptions { Key="ArrowUp", PreventDown = "key+none" }, // prevent scrolling page, instead increment
new KeyOptions { Key="ArrowDown", PreventDown = "key+none" }, // prevent scrolling page, instead decrement
new KeyOptions { Key="Dead", PreventDown = "key+any" }, // prevent dead keys like ^ ` ´ etc
new KeyOptions { Key="/^(?!"+(Pattern ?? "[0-9]").TrimEnd('*')+").$/", PreventDown = "key+none|key+shift|key+alt" }, // prevent input of all other characters except allowed, like [0-9.,-+]
},
});
}
await base.OnAfterRenderAsync(firstRender);
}
protected async Task HandleKeydown(KeyboardEventArgs obj)
{
if (Disabled || ReadOnly)
return;
switch (obj.Key)
{
case "ArrowUp":
await Increment();
break;
case "ArrowDown":
await Decrement();
break;
}
OnKeyDown.InvokeAsync(obj).AndForget();
}
protected Task HandleKeyUp(KeyboardEventArgs obj)
{
if (Disabled || ReadOnly)
return Task.CompletedTask;
OnKeyUp.InvokeAsync(obj).AndForget();
return Task.CompletedTask;
}
protected async Task OnMouseWheel(WheelEventArgs obj)
{
if (!obj.ShiftKey || Disabled || ReadOnly)
return;
if (obj.DeltaY < 0)
{
if (InvertMouseWheel == false)
await Increment();
else
await Decrement();
}
else if (obj.DeltaY > 0)
{
if (InvertMouseWheel == false)
await Decrement();
else
await Increment();
}
}
private bool _minHasValue = false;
/// <summary>
/// Reverts mouse wheel up and down events, if true.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.Behavior)]
public bool InvertMouseWheel { get; set; } = false;
private T _minDefault;
private T _min;
/// <summary>
/// The minimum value for the input.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.Validation)]
public T Min
{
get => _minHasValue ? _min : _minDefault;
set
{
_minHasValue = value != null;
_min = value;
}
}
private bool _maxHasValue = false;
private T _maxDefault;
private T _max;
/// <summary>
/// The maximum value for the input.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.Validation)]
public T Max
{
get => _maxHasValue ? _max : _maxDefault;
set
{
_maxHasValue = value != null;
_max = value;
}
}
private bool _stepHasValue = false;
private T _stepDefault;
private T _step;
/// <summary>
/// The increment added/subtracted by the spin buttons.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.Behavior)]
public T Step
{
get => _stepHasValue ? _step : _stepDefault;
set
{
_stepHasValue = value != null;
_step = value;
}
}
/// <summary>
/// Hides the spin buttons, the user can still change value with keyboard arrows and manual update.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.Appearance)]
public bool HideSpinButtons { get; set; }
/// <summary>
/// Hints at the type of data that might be entered by the user while editing the input.
/// Defaults to numeric
/// </summary>
[Parameter]
public override InputMode InputMode { get; set; } = InputMode.numeric;
/// <summary>
/// The pattern attribute, when specified, is a regular expression which the input's value must match in order for the value to pass constraint validation. It must be a valid JavaScript regular expression
/// Defaults to [0-9,.\-]
/// To get a numerical keyboard on safari, use the pattern. The default pattern should achieve numerical keyboard.
///
/// Note: this pattern is also used to prevent all input except numbers and allowed characters. So for instance to allow only numbers, no signs and no commas you might change it to to [0-9.]
/// </summary>
[Parameter]
public override string Pattern { get; set; } = @"[0-9,.\-]";
private string GetCounterText() => Counter == null ? string.Empty : (Counter == 0 ? (string.IsNullOrEmpty(Text) ? "0" : $"{Text.Length}") : ((string.IsNullOrEmpty(Text) ? "0" : $"{Text.Length}") + $" / {Counter}"));
private Task OnInputValueChanged(string text)
{
return SetTextAsync(text);
}
//avoids the format to use scientific notation for large or small number in floating points types, while covering all options
//https://stackoverflow.com/questions/1546113/double-to-string-conversion-without-scientific-notation
private const string TagFormat = "0.###################################################################################################################################################################################################################################################################################################################################################";
private string FormatParam(T value)
{
if (value is IFormattable f)
return f.ToString(TagFormat, CultureInfo.InvariantCulture.NumberFormat);
else
return null;
}
private decimal FromDecimal(T v)
=> Convert.ToDecimal((decimal?)(object)v);
private long FromInt64(T v)
=> Convert.ToInt64((long?)(object)v);
private ulong FromUInt64(T v)
=> Convert.ToUInt64((ulong?)(object)v);
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing == true)
{
_keyInterceptor?.Dispose();
}
}
}
}