Fix Inconsistent behavior occurs when using @bind and @bind-value in input date type#67310
Open
NanthiniMahalingam wants to merge 3 commits into
Open
Fix Inconsistent behavior occurs when using @bind and @bind-value in input date type#67310NanthiniMahalingam wants to merge 3 commits into
NanthiniMahalingam wants to merge 3 commits into
Conversation
Open
3 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes inconsistent <input type="date"> editing behavior in Blazor when using @bind/@bind-value with date/time types by avoiding disruptive re-renders during segmented date entry and aligning formatted null output with what browsers report for empty date inputs.
Changes:
- Adjusts binder empty-string handling to avoid resetting date/time values to invalid defaults while users are editing.
- Updates
BindConverterformatting for nullable date/time types to return""(instead ofnull) so renderedvaluestays in sync with the browser’s empty value. - Adds regression tests covering non-nullable
DateTimeempty-string scenarios (and should be extended to cover async setter overloads).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Components/Components/src/EventCallbackFactoryBinderExtensions.cs | Updates binder behavior when the browser reports an empty string during change events. |
| src/Components/Components/src/BindConverter.cs | Formats nullable date/time values as empty string to keep rendered value aligned with browser state. |
| src/Components/Components/test/EventCallbackFactoryBinderExtensionsTest.cs | Adds regression tests for non-nullable DateTime empty string binder scenarios. |
Comment on lines
+482
to
+500
| [Fact] | ||
| public async Task CreateBinder_NonNullableDateTime_EmptyValue_PreservesEachBoundValue() | ||
| { | ||
| var value = new DateTime(2022, 2, 10); | ||
| var component = new EventCountingComponent(); | ||
| Action<DateTime> setter = (_) => value = _; | ||
|
|
||
| EventCallback<ChangeEventArgs> binder = EventCallback.Factory.CreateBinder(component, setter, value, "yyyy-MM-dd", CultureInfo.InvariantCulture); | ||
| await binder.InvokeAsync(new ChangeEventArgs() { Value = string.Empty, }); | ||
| Assert.Equal(1, component.Count); | ||
| var value1 = new DateTime(2023, 02, 09); | ||
| Action<DateTime> setter1 = (_) => value1 = _; | ||
|
|
||
| var binder1 = EventCallback.Factory.CreateBinder(component, setter1, value1, "yyyy-MM-dd", CultureInfo.InvariantCulture); | ||
| await binder1.InvokeAsync(new ChangeEventArgs() { Value = string.Empty, }); | ||
| // The setter must not have been called, so the previous valid value is preserved. | ||
| Assert.Equal(new DateTime(2023, 02, 9), value1); | ||
| Assert.Equal(2, component.Count); | ||
| } |
Comment on lines
1374
to
+1378
| else if (string.Empty.Equals(e.Value)) | ||
| { | ||
| setter(default!); | ||
| var typeInfo = typeof(T); | ||
| var isNullable = typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof(Nullable<>); | ||
| if (typeInfo == typeof(string) || isNullable) |
Comment on lines
1466
to
+1470
| else if (string.Empty.Equals(e.Value)) | ||
| { | ||
| setter(default!); | ||
| var typeInfo = typeof(T); | ||
| var isNullable = typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof(Nullable<>); | ||
| if (typeInfo == typeof(string) || isNullable) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug Description
Inconsistent behavior occurs when using @Bind and @bind-value in input date type
Description of code changes
Output
Before changes
Beforefix40660.mp4
After changes
Afterfix40660_1.mp4
Fixes #40660