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

🐛 fix(Input): reset input and focus status when reset validation #2133

Merged
merged 2 commits into from
Sep 6, 2024
Merged
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
39 changes: 22 additions & 17 deletions src/Masa.Blazor/Components/Input/MInput.Validatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public virtual TValue? Value
set => SetValue(value);
}

private bool _hasInput;
private bool _hasFocused;
private bool _isResetting;
private bool _forceStatus;
private bool _internalValueChangingFromOnValueChanged;
private CancellationTokenSource? _cancellationTokenSource;
Expand All @@ -66,10 +69,6 @@ protected void UpdateInternalValue(TValue? value, InternalValueChangeType change

public FieldIdentifier? ValueIdentifier => ValueExpression is null ? null : FieldIdentifier.Create(ValueExpression);

protected bool HasInput { get; set; }

protected bool HasFocused { get; set; }

public virtual ElementReference InputElement { get; set; }

protected virtual TValue? InternalValue
Expand Down Expand Up @@ -167,10 +166,10 @@ public virtual bool ShouldValidate

if (ValidateOn == ValidateOn.Blur)
{
return HasFocused;
return _hasFocused;
}

return HasInput || HasFocused;
return _hasInput || _hasFocused;
}
}

Expand Down Expand Up @@ -236,8 +235,6 @@ public virtual Task HandleOnChangeAsync(ChangeEventArgs args)

protected virtual bool DisableSetValueByJsInterop => false;

protected virtual bool ValidateOnlyInFocusedState => true;

protected virtual bool WatchValueChangeImmediately => true;

protected virtual async Task SetValueByJsInterop(string? val)
Expand Down Expand Up @@ -273,7 +270,7 @@ private async void IsFocusedChangeCallback(bool val)
{
if (!val && !IsDisabled)
{
HasFocused = true;
_hasFocused = true;

if (ValidateOn == ValidateOn.Blur)
{
Expand Down Expand Up @@ -340,7 +337,7 @@ protected virtual void OnInternalValueChange(TValue? val)
{
// If it's the first time we're setting input,
// mark it with hasInput
HasInput = true;
_hasInput = true;

if (ValidateOn == ValidateOn.Input)
{
Expand Down Expand Up @@ -396,6 +393,13 @@ protected virtual Task OnIsFocusedChange(bool val)

protected void InternalValidate()
{
if (_isResetting)
{
_isResetting = false;
_hasInput = false;
_hasFocused = false;
}

if (EditContext == null)
{
var previousErrorBucket = ErrorBucket;
Expand Down Expand Up @@ -464,8 +468,8 @@ protected bool Validate(TValue? val)

if (force)
{
HasInput = true;
HasFocused = true;
_hasInput = true;
_hasFocused = true;
}

if (InternalRules.Any())
Expand All @@ -483,10 +487,9 @@ protected bool Validate(TValue? val)

public void Reset()
{
ErrorBucket.Clear();

HasInput = false;
HasFocused = false;
_hasInput = false;
_hasFocused = false;
_isResetting = true;

if (ValueIdentifier.HasValue)
{
Expand All @@ -504,7 +507,9 @@ public void Reset()

public void ResetValidation()
{
ErrorBucket.Clear();
_hasInput = false;
_hasFocused = false;
_isResetting = true;
}

protected void HandleOnValidationStateChanged(object? sender, ValidationStateChangedEventArgs e)
Expand Down
1 change: 0 additions & 1 deletion src/Masa.Blazor/Components/Select/MSelect.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,6 @@ public override async Task HandleOnClickAsync(ExMouseEventArgs args)
if (!IsFocused)
{
IsFocused = true;
HasFocused = true;

if (OnFocus.HasDelegate)
{
Expand Down
2 changes: 0 additions & 2 deletions src/Masa.Blazor/Components/Slider/MSliderBase.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,6 @@

public bool ShowThumbLabelContainer => IsFocused || IsActive || ThumbLabel == "always";

protected override bool ValidateOnlyInFocusedState => false;

protected virtual Task SetInternalValueAsync(double internalValue)
{
var val = RoundValue(Math.Min(Math.Max(internalValue, Min), Max));
Expand All @@ -220,7 +218,7 @@
return Task.CompletedTask;
}

protected override void OnValueChanged(TValue val)

Check warning on line 221 in src/Masa.Blazor/Components/Slider/MSliderBase.razor.cs

View workflow job for this annotation

GitHub Actions / pr

Nullability of type of parameter 'val' doesn't match overridden member (possibly because of nullability attributes).
{
//Value may not between min and max
//If that so,we should invoke ValueChanged
Expand Down Expand Up @@ -402,7 +400,7 @@
return IsFocused;
}

private bool IndependentTheme =>

Check warning on line 403 in src/Masa.Blazor/Components/Slider/MSliderBase.razor.cs

View workflow job for this annotation

GitHub Actions / pr

'MSliderBase<TValue, TNumeric>.IndependentTheme' hides inherited member 'MInput<TValue>.IndependentTheme'. Use the new keyword if hiding was intended.
(IsDirtyParameter(nameof(Dark)) && Dark) || (IsDirtyParameter(nameof(Light)) && Light);

#if NET8_0_OR_GREATER
Expand Down
2 changes: 1 addition & 1 deletion src/Masa.Blazor/Core/MasaComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ForwardRef RefBack
private bool _elementReferenceChanged;

[JsonIgnore]
public ILogger? Logger => LoggerFactory.CreateLogger(GetType());
public ILogger Logger => LoggerFactory.CreateLogger(GetType());

#region Build class and style

Expand Down
Loading