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

[HxInputDate] [HxInputDateRange] Changing value causes two calls of EditContext.OnFieldChanged #787

Open
jirikanda opened this issue Apr 11, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@jirikanda
Copy link
Contributor

jirikanda commented Apr 11, 2024

Reprosteps

  • Handle EditContext.OnFieldChanged event
  • Click on a date in the calendar.
  • EditContext.OnFieldChanged event handler is called twice

Why?

(both HxInputDate<TValue> and also HxInputDateInternal<TValue> inherit from InputBase<TValue>)

First call

  • HxInputDateInternal<TValue>.HandleValueChanged is called.
  • HxInputDateInternal<TValue>.HandleValueChanged sets the CurrentValueAsString.
  • CurrentValueAsString setter sets InputBase<TValuegt;.CurrentValue.
  • InputBase<TValue>.CurrentValue calls ValueChanged (important to understand the the second call)
  • The InputBase<TValue>.CurrentValue setter calls EditContext.NotifyFieldChanged for the first time.

Second call

  • HxInputDate<Value> handles ValueChanged event
  • It sets the CurrentValue property (see BuildRenderInputCore method, sequence number 101).
  • The InputBase<TValue>.CurrentValue setter calls (again) EditContext.NotifyFieldChanged.

Posible solutions (not sure right now)

  • only HxInputDate<TValue> should derive from InputBase<TValue>
  • OR HxInputDate<TValue> should not Set CurrentValue, but it should fire ValueChanged event

(I guess this occurs also in HxInputDateRange while it uses the same code structure.)

@hakenr
Copy link
Member

hakenr commented Apr 11, 2024

only HxInputDate should derive from InputBase

My preference (same in other similar components).

@hakenr hakenr added the bug Something isn't working label Apr 11, 2024
@jirikanda
Copy link
Contributor Author

jirikanda commented Apr 12, 2024

@hakenr, I am not familiar with the current code structure, there for I am not able to fix the issue (until someone explains me the thoughts behind).
ie.

	private void HandleValueChanged(string newInputValue)
	{
#if NET8_0_OR_GREATER
		CurrentValueAsString = newInputValue;
#else
		// HandleValueChanged is used instead of TryParseValueFromString
		// When TryParseValueFromString is used (pre net8), invalid input is replaced by previous value.		
		bool parsingFailed;
		_validationMessageStore.Clear(FieldIdentifier);

		if (HxInputDate<DateTime>.TryParseDateTimeOffsetFromString(newInputValue, null, out var date))
		{
			parsingFailed = false;
			CurrentValue = GetValueFromDateTimeOffset(date);
		}
		else
		{
			parsingFailed = true;
			_validationMessageStore.Add(FieldIdentifier, ParsingErrorMessageEffective);
		}

		// We can skip the validation notification if we were previously valid and still are
		if (parsingFailed || _previousParsingAttemptFailed)
		{
			EditContext.NotifyValidationStateChanged();
			_previousParsingAttemptFailed = parsingFailed;
		}
#endif
	}

Why are the so different NET8 vs pre-NET8?

jirikanda added a commit that referenced this issue Apr 12, 2024
@hakenr
Copy link
Member

hakenr commented Apr 17, 2024

@jirikanda Before .NET 8, the SetUpdatesAttributeName() method was missing, and some of our components, such as HxInputDate, compensated it by using our own logic. In .NET 8, the new InputBase implementation incorporates SetUpdatesAttributeName() along with _incomingValueBeforeParsing and other stuff from my pull request, making our custom logic obsolete. Now, the InputBase already addresses the issue we were compensating for, simplifying the process to just CurrentValueAsString = newInputValue;.

dotnet/aspnetcore#46434

@hakenr
Copy link
Member

hakenr commented Apr 17, 2024

We should be able to drop all these pre-net8 "branches" in November 2024:
https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core

@hakenr hakenr changed the title [HxInputDate] Changing value causes two calls of EditContext.OnFieldChanged [HxInputDate] [HxInputDateRange] Changing value causes two calls of EditContext.OnFieldChanged Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants