Skip to content

Commit

Permalink
🐛 fix(TextField): failed to set max and min values (#2274)
Browse files Browse the repository at this point in the history
  • Loading branch information
capdiem authored Dec 9, 2024
1 parent 317906d commit dc28423
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
12 changes: 10 additions & 2 deletions src/Masa.Blazor/Components/Input/MInput.Validatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,18 @@ public virtual TValue? Value
private EditContext? _prevEditContext;
private InternalValueChangeType _changeType;

protected virtual void UpdateInternalValue(TValue? value, InternalValueChangeType changeType)
protected virtual void UpdateInternalValue(TValue? value, InternalValueChangeType changeType, bool force = false)
{
_changeType = changeType;
InternalValue = value;

if (force)
{
OnInternalValueChange(value);
}
else
{
InternalValue = value;
}
}

protected virtual TValue? DefaultValue => default;
Expand Down
34 changes: 22 additions & 12 deletions src/Masa.Blazor/Components/TextField/MTextField.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,9 @@ private async Task HandleOnInputOrChangeEvent(ChangeEventArgs args, EventCallbac
}
}

protected override void UpdateInternalValue(TValue? value, InternalValueChangeType changeType)
protected override void UpdateInternalValue(TValue? value, InternalValueChangeType changeType, bool force = false)
{
base.UpdateInternalValue(value, changeType);
base.UpdateInternalValue(value, changeType, force);

if (IsNumeric(value, out var numeric))
{
Expand All @@ -568,12 +568,15 @@ private bool IsNumeric(TValue? value, out IConvertible numeric)

protected override TValue? ConvertAndSetValueByJSInterop(TValue? val)
{
if (Props.Precision.HasValue && IsNumeric(val, out var numeric))
if (IsNumeric(val, out var numeric))
{
var decimalValue = Convert.ToDecimal(numeric, CultureInfo.InvariantCulture);
var newValue = Math.Round(decimalValue, Props.Precision.Value);
_ = SetValueByJsInterop(newValue.ToString(Props.PrecisionFormat, CultureInfo.InvariantCulture));
return (TValue)Convert.ChangeType(newValue, typeof(TValue), CultureInfo.InvariantCulture);
if (Props.Precision.HasValue)
{
var newValue = Math.Round(decimalValue, Props.Precision.Value);
_ = SetValueByJsInterop(newValue.ToString(Props.PrecisionFormat, CultureInfo.InvariantCulture));
return (TValue)Convert.ChangeType(newValue, typeof(TValue), CultureInfo.InvariantCulture);
}
}

return base.ConvertAndSetValueByJSInterop(val);
Expand All @@ -598,8 +601,15 @@ private void UpdateValue(string? originValue, bool succeeded, TValue? convertedV
if (succeeded)
{
_badInput = false;
var validValue = GetValidValue(convertedValue);
UpdateInternalValue(validValue, InternalValueChangeType.Input);

// the value would be changed internally if it's out of range,
// so we need to update the change type to InternalOperation
var (validValue, changed) = GetValidValue(convertedValue);

UpdateInternalValue(
validValue,
changed ? InternalValueChangeType.InternalOperation : InternalValueChangeType.Input,
changed);
}
else
{
Expand All @@ -621,24 +631,24 @@ private void UpdateValue(string? originValue, bool succeeded, TValue? convertedV
}
}

private TValue GetValidValue(TValue val)
private (TValue value, bool changed) GetValidValue(TValue val)
{
if (IsNumeric(val, out var numeric))
{
var decimalValue = Convert.ToDecimal(numeric, CultureInfo.InvariantCulture);

if (Props.Min.HasValue && decimalValue < Props.Min)
{
return (TValue)Convert.ChangeType(Props.Min, typeof(TValue), CultureInfo.InvariantCulture);
return ((TValue)Convert.ChangeType(Props.Min, typeof(TValue), CultureInfo.InvariantCulture), true);
}

if (Props.Max.HasValue && decimalValue > Props.Max)
{
return (TValue)Convert.ChangeType(Props.Max, typeof(TValue), CultureInfo.InvariantCulture);
return ((TValue)Convert.ChangeType(Props.Max, typeof(TValue), CultureInfo.InvariantCulture), true);
}
}

return val;
return (val, false);
}

public async Task HandleOnKeyUpAsync(KeyboardEventArgs args)
Expand Down

0 comments on commit dc28423

Please sign in to comment.