Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MatNumericUpDownField Fixes #676

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ protected override EventCallback<KeyboardEventArgs> OnKeyDownEvent()

protected void Increase()
{
CurrentValue = SwitchT.Increase(CurrentValue, Step, Maximum);
CurrentValue = SwitchT.Increase(CurrentValue, Step);
CurrentValue = SwitchT.Clamp(CurrentValue, Minimum, Maximum);
}

protected void Decrease()
{
CurrentValue = SwitchT.Decrease(CurrentValue, Step, Minimum);
CurrentValue = SwitchT.Decrease(CurrentValue, Step);
CurrentValue = SwitchT.Clamp(CurrentValue, Minimum, Maximum);
}

protected override TValue CurrentValue
Expand Down Expand Up @@ -66,6 +68,8 @@ protected override bool InputTextReadOnly()
private readonly EventCallback<KeyboardEventArgs> OnKeyDownEvent2;
public BaseMatNumericUpDownFieldInternal()
{
OnFocusOutEvent.Event += OnFocusOutEvent_Event;

OnKeyDownEvent2 = EventCallback.Factory.Create<KeyboardEventArgs>(this, async (e) =>
{
await OnKeyDown.InvokeAsync(e);
Expand All @@ -78,13 +82,20 @@ public BaseMatNumericUpDownFieldInternal()
Decrease();
}
});
Maximum = SwitchT.GetMaximum();
Minimum = SwitchT.GetMinimum();

Maximum ??= SwitchT.GetMaximum();
Minimum ??= SwitchT.GetMinimum();

ClassMapper.Add("mat-numeric-up-down-field");
ClassMapper.Add("mat-text-field-with-actions-container");
}

private void OnFocusOutEvent_Event(object sender, FocusEventArgs e)
{
// Clamp to minimum and maximum on blur.
CurrentValue = SwitchT.Clamp(CurrentValue, Minimum, Maximum);
}

private readonly TValue ZeroValue = MatTypeConverter.ChangeType<TValue>(0);

protected override void OnParametersSet()
Expand All @@ -107,7 +118,10 @@ protected override void OnParametersSet()
}
else
{
Step = SwitchT.GetStep();
if (Step == null || Step.Equals(ZeroValue))
{
Step = SwitchT.GetStep();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/MatBlazor/Components/MatPaginator/BaseMatPaginator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public async Task NavigateToPage(MatPaginatorAction direction, int pageSize)
{
page = ((PageIndex * PageSize) / pageSize);
}
catch (OverflowException e)
catch (OverflowException)
{
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/MatBlazor/Core/MatBlazorSwitchT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ namespace MatBlazor
{
public abstract class MatBlazorSwitchT<T>
{
public abstract T Increase(T v, T step, T max);
public abstract T Decrease(T v, T step, T min);
public abstract T Clamp(T v, T min, T max);
public abstract T Increase(T v, T step);
public abstract T Decrease(T v, T step);
public abstract T Round(T v, int dp);

public abstract T GetMinimum();
Expand Down
10 changes: 8 additions & 2 deletions src/MatBlazor/Core/MatBlazorSwitchTBool.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.SymbolStore;
using System.Globalization;

namespace MatBlazor
Expand All @@ -15,12 +16,17 @@ public override bool ToBool(bool v)
return v;
}

public override bool Increase(bool v, bool step, bool max)
public override bool Clamp(bool v, bool min, bool max)
{
return v;
}

public override bool Increase(bool v, bool step)
{
return !v;
}

public override bool Decrease(bool v, bool step, bool min)
public override bool Decrease(bool v, bool step)
{
return !v;
}
Expand Down
9 changes: 7 additions & 2 deletions src/MatBlazor/Core/MatBlazorSwitchTBoolNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ public override bool ToBool(bool? v)
return v ?? false;
}

public override bool? Increase(bool? v, bool? step, bool? max)
public override bool? Clamp(bool? v, bool? min, bool? max)
{
return v;
}

public override bool? Increase(bool? v, bool? step)
{
return !(v ?? false);
}

public override bool? Decrease(bool? v, bool? step, bool? min)
public override bool? Decrease(bool? v, bool? step)
{
return !(v ?? false);
}
Expand Down
23 changes: 13 additions & 10 deletions src/MatBlazor/Core/MatBlazorSwitchTByte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,37 @@ namespace MatBlazor
{
public class MatBlazorSwitchTByte : MatBlazorSwitchT<byte>
{
public override byte Increase(byte v, byte step, byte max)
public override byte Clamp(byte v, byte min, byte max)
{
return v < min ? min : v > max ? max : v;
}

public override byte Increase(byte v, byte step)
{
checked
{
try
{
var v2 = (byte) (v + step);
return v2 <= max ? v2 : max;
return (byte) (v + step);
}
catch (OverflowException e)
catch (OverflowException)
{
return max;
return byte.MaxValue;
}
}
}

public override byte Decrease(byte v, byte step, byte min)
public override byte Decrease(byte v, byte step)
{
checked
{
try
{
var v2 = (byte)(v - step);
return v2 >= min ? v2 : min;
return (byte)(v - step);
}
catch (OverflowException e)
catch (OverflowException)
{
return min;
return byte.MinValue;
}
}
}
Expand Down
23 changes: 13 additions & 10 deletions src/MatBlazor/Core/MatBlazorSwitchTByteNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,37 @@ namespace MatBlazor
{
public class MatBlazorSwitchTByteNull : MatBlazorSwitchT<byte?>
{
public override byte? Increase(byte? v, byte? step, byte? max)
public override byte? Clamp(byte? v, byte? min, byte? max)
{
return v < min ? min : v > max ? max : v;
}

public override byte? Increase(byte? v, byte? step)
{
checked
{
try
{
var v2 = (v.HasValue || step.HasValue) ? (byte?) ((v ?? 0) + (step ?? 0)) : null;
return (max.HasValue && v2.HasValue) ? (v2.Value <= max.Value ? v2.Value : max.Value) : v2;
return (v.HasValue || step.HasValue) ? (byte?) ((v ?? 0) + (step ?? 0)) : null;
}
catch (OverflowException e)
catch (OverflowException)
{
return max;
return byte.MaxValue;
}
}
}

public override byte? Decrease(byte? v, byte? step, byte? min)
public override byte? Decrease(byte? v, byte? step)
{
checked
{
try
{
var v2 = (v.HasValue || step.HasValue) ? (byte?) ((v ?? 0) - (step ?? 0)) : null;
return (min.HasValue && v2.HasValue) ? (v2.Value >= min.Value ? v2.Value : min.Value) : v2;
return (v.HasValue || step.HasValue) ? (byte?) ((v ?? 0) - (step ?? 0)) : null;
}
catch (OverflowException e)
catch (OverflowException)
{
return min;
return byte.MinValue;
}
}
}
Expand Down
23 changes: 13 additions & 10 deletions src/MatBlazor/Core/MatBlazorSwitchTChar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,37 @@ namespace MatBlazor
{
public class MatBlazorSwitchTChar : MatBlazorSwitchT<char>
{
public override char Increase(char v, char step, char max)
public override char Clamp(char v, char min, char max)
{
return v < min ? min : v > max ? max : v;
}

public override char Increase(char v, char step)
{
checked
{
try
{
var v2 = (char) (v + step);
return v2 <= max ? v2 : max;
return (char) (v + step);
}
catch (OverflowException e)
catch (OverflowException)
{
return max;
return char.MaxValue;
}
}
}

public override char Decrease(char v, char step, char min)
public override char Decrease(char v, char step)
{
checked
{
try
{
var v2 = (char)(v - step);
return v2 >= min ? v2 : min;
return (char)(v - step);
}
catch (OverflowException e)
catch (OverflowException)
{
return min;
return char.MinValue;
}
}
}
Expand Down
23 changes: 13 additions & 10 deletions src/MatBlazor/Core/MatBlazorSwitchTCharNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,37 @@ namespace MatBlazor
{
public class MatBlazorSwitchTCharNull : MatBlazorSwitchT<char?>
{
public override char? Increase(char? v, char? step, char? max)
public override char? Clamp(char? v, char? min, char? max)
{
return v < min ? min : v > max ? max : v;
}

public override char? Increase(char? v, char? step)
{
checked
{
try
{
var v2 = (v.HasValue || step.HasValue) ? (char?) ((v ?? 0) + (step ?? 0)) : null;
return (max.HasValue && v2.HasValue) ? (v2.Value <= max.Value ? v2.Value : max.Value) : v2;
return (v.HasValue || step.HasValue) ? (char?) ((v ?? 0) + (step ?? 0)) : null;
}
catch (OverflowException e)
catch (OverflowException)
{
return max;
return char.MaxValue;
}
}
}

public override char? Decrease(char? v, char? step, char? min)
public override char? Decrease(char? v, char? step)
{
checked
{
try
{
var v2 = (v.HasValue || step.HasValue) ? (char?) ((v ?? 0) - (step ?? 0)) : null;
return (min.HasValue && v2.HasValue) ? (v2.Value >= min.Value ? v2.Value : min.Value) : v2;
return (v.HasValue || step.HasValue) ? (char?) ((v ?? 0) - (step ?? 0)) : null;
}
catch (OverflowException e)
catch (OverflowException)
{
return min;
return char.MinValue;
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/MatBlazor/Core/MatBlazorSwitchTCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ namespace MatBlazor
{
public class MatBlazorSwitchTCommon<T> : MatBlazorSwitchT<T>
{
public override T Increase(T v, T step, T max)
public override T Clamp(T v, T min, T max)
{
throw new NotImplementedException();
}

public override T Decrease(T v, T step, T min)
public override T Increase(T v, T step)
{
throw new NotImplementedException();
}

public override T Decrease(T v, T step)
{
throw new NotImplementedException();
}
Expand Down
9 changes: 7 additions & 2 deletions src/MatBlazor/Core/MatBlazorSwitchTDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ namespace MatBlazor
{
public class MatBlazorSwitchTDateTime : MatBlazorSwitchT<DateTime>
{
public override DateTime Increase(DateTime v, DateTime step, DateTime max)
public override DateTime Clamp(DateTime v, DateTime min, DateTime max)
{
throw new NotImplementedException();
}

public override DateTime Decrease(DateTime v, DateTime step, DateTime min)
public override DateTime Increase(DateTime v, DateTime step)
{
throw new NotImplementedException();
}

public override DateTime Decrease(DateTime v, DateTime step)
{
throw new NotImplementedException();
}
Expand Down
9 changes: 7 additions & 2 deletions src/MatBlazor/Core/MatBlazorSwitchTDateTimeNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ namespace MatBlazor
{
public class MatBlazorSwitchTDateTimeNull : MatBlazorSwitchT<DateTime?>
{
public override DateTime? Increase(DateTime? v, DateTime? step, DateTime? max)
public override DateTime? Clamp(DateTime? v, DateTime? min, DateTime? max)
{
throw new NotImplementedException();
}

public override DateTime? Decrease(DateTime? v, DateTime? step, DateTime? min)
public override DateTime? Increase(DateTime? v, DateTime? step)
{
throw new NotImplementedException();
}

public override DateTime? Decrease(DateTime? v, DateTime? step)
{
throw new NotImplementedException();
}
Expand Down
Loading