Skip to content

Commit

Permalink
🐛 fix(TextField): OnChange doesn't work (#1440)
Browse files Browse the repository at this point in the history
* 🐛 fix(TextField): OnChange doesn't work

* format

* 🐛 fix: nullable
  • Loading branch information
capdiem authored Jul 24, 2023
1 parent a7f8407 commit b1cb837
Showing 1 changed file with 52 additions and 26 deletions.
78 changes: 52 additions & 26 deletions src/Masa.Blazor/Components/TextField/MTextField.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BlazorComponent.Web;
using System.Runtime.CompilerServices;
using BlazorComponent.Web;
using Microsoft.AspNetCore.Components.Web;

namespace Masa.Blazor
Expand Down Expand Up @@ -135,11 +136,18 @@ public virtual bool Outlined
public int DebounceInterval { get; set; }

/// <summary>
/// Update the bound value on blur instead of on input.
/// Update the bound value on change event instead of on input.
/// </summary>
[Parameter]
[Obsolete("Use UpdateOnChange instead.")]
public bool UpdateOnBlur { get; set; }

/// <summary>
/// Update the bound value on change event instead of on input.
/// </summary>
[Parameter]
public bool UpdateOnChange { get; set; }

private static readonly string[] s_dirtyTypes = { "color", "file", "time", "date", "datetime-local", "week", "month" };

private bool _badInput;
Expand Down Expand Up @@ -651,14 +659,6 @@ public override async Task HandleOnClickAsync(ExMouseEventArgs args)
await InputElement.FocusAsync();
}

public override async Task HandleOnChangeAsync(ChangeEventArgs args)
{
if (UpdateOnBlur)
{
await UpdateValue(args.Value?.ToString(), OnChange);
}
}

public virtual async Task HandleOnBlurAsync(FocusEventArgs args)
{
IsFocused = false;
Expand All @@ -676,35 +676,61 @@ public virtual async Task HandleOnBlurAsync(FocusEventArgs args)
}
}

public override async Task HandleOnInputAsync(ChangeEventArgs args)
public override Task HandleOnInputAsync(ChangeEventArgs args)
{
return OnValueChangedAsync(args, OnInput);
}

public override Task HandleOnChangeAsync(ChangeEventArgs args)
{
if (UpdateOnBlur)
return OnValueChangedAsync(args, OnChange);
}

private async Task OnValueChangedAsync(ChangeEventArgs args, EventCallback<TValue> cb,
[CallerArgumentExpression("cb")] string cbName = "")
{
var originValue = args.Value?.ToString();

var succeed = TryConvertTo<TValue>(originValue, out var result);

if (succeed && cb.HasDelegate)
{
return;
await cb.InvokeAsync(result);
}

await UpdateValue(args.Value?.ToString(), OnInput);
var updateOnChange = UpdateOnBlur || UpdateOnChange;

StateHasChanged();
// todo: args.validity.badInput
if ((cbName == nameof(OnInput) && !updateOnChange) || (cbName == nameof(OnChange) && updateOnChange))
{
UpdateValue(originValue, succeed, result);

StateHasChanged();
}
}

private async Task UpdateValue(string? value, EventCallback<TValue> callback)
private static bool TryConvertTo<T>(string? value, out T? result)
{
var success = BindConverter.TryConvertTo<TValue>(value, CultureInfo.InvariantCulture, out var val);
var succeeded = BindConverter.TryConvertTo<T>(value, CultureInfo.InvariantCulture, out var val);

if (succeeded)
{
result = val;
return true;
}

result = default;
return false;
}

if (success)
private void UpdateValue(string? originValue, bool succeeded, TValue? convertedValue)
{
if (succeeded)
{
_badInput = false;

ValueChangedInternally = true;

InternalValue = val;

if (callback.HasDelegate)
{
await callback.InvokeAsync(val);
}
InternalValue = convertedValue;
}
else
{
Expand All @@ -715,7 +741,7 @@ private async Task UpdateValue(string? value, EventCallback<TValue> callback)
if (Type.ToLower() == "number")
{
// reset the value of input element if failed to convert
if (!string.IsNullOrEmpty(value))
if (!string.IsNullOrEmpty(originValue))
{
_ = SetValueByJsInterop("");
}
Expand Down

0 comments on commit b1cb837

Please sign in to comment.