-
Notifications
You must be signed in to change notification settings - Fork 161
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(combo): update touched property and apply invalid color on blur when required - master #14922
fix(combo): update touched property and apply invalid color on blur when required - master #14922
Conversation
Thank you for the recommendation! I tested it, and your suggestion works as expected. As in the reference PR, I will proceed by updating ng-touched both on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer this approach and like the separation of concern as on the one side - _onTouchedCallback is not coupled with the validateComboState() AND on the other handling onFocus ensures the component should be more stable and less prone to bugs as this is handled as soon as the user interacts with the component. This may seem slightly redundant, yet should be more stable.
public onFocus(): void { | ||
this._onTouchedCallback(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Zneeky @IvayloG @kacheshmarova @Lipata
I guess I'll be the bearer of bad news again, since this cannot be right :) The control cannot be in touched state on first focus, that will cause initially invalid fields to turn red on focus, which is not intended at all.
See https://angular.dev/api/forms/ControlValueAccessor#registerOnTouched
your class calls it when the control should be considered blurred or "touched".
Or just the simplest of examples with the built-in ngModel
and simple input where the touched state remains false until first blur
:
https://stackblitz.com/edit/d5xmg9?file=src%2Fmain.ts
PS: Oh wow, just realized that fix is based on a really old change in the Select and I do remember we had the ValueAccessor implemented wrong.. Hey it's even still in there 😁 That's wrong too lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The actual issue from what I can tell is touched isn't fired on blur when clicking outside - which is fair enough, and that's likely in the close handling - there's a distinction between closing with selection/KB, which restores the focus back to the input (thus focus as a whole has not yet left the component) and outside click which does mean the component is 'blurred' and should use the touched callback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See pretty similar handling in the Date Picker for example that does just that and the else case can be replicated for this fix:
igniteui-angular/projects/igniteui-angular/src/lib/date-picker/date-picker.component.ts
Lines 927 to 933 in d5df92a
// do not focus the input if clicking outside in dropdown mode | |
if (this.getEditElement() && !(args.event && this.isDropdown)) { | |
this.inputDirective.focus(); | |
} else { | |
this._onTouchedCallback(); | |
this.updateValidity(); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@damyanpetev @Lipata
Adding some more details:
The control cannot be in a touched state on the first focus, that will cause initially invalid fields to turn red on focus, which is not intended at all.
I was thinking the same, yet after the currently implemented fix the red color is updated on the outside click as it should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly, because the error color is applied either if the underlying form control changes state (but it's invalid to begin with) or the check we evaluate on blur. Perhaps wasn't the best example, I suppose a better one would be if you have error message based on model.error.required && model.touched
it should be visible before that :)
On second thought, I believe this will also mess with the updateOn: 'blur'
on form controls since that relies on the touched callback to initiate the value update. I think we had a bunch of issues for that some time ago that showed us we weren't implementing the value accessor right and I thought we hunted those down across all components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh more fun, the combo had the same fix as the select... except we reverted that one quick enough lol 😁
See #8123
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@damyanpetev @IvayloG
I've created a new PR to address the issues that this PR introduced, and I'd appreciate your feedback on the proposed solution.
In the new PR I've made a change to the handleClosing
event:
if (!e.event) {
this.comboInput?.nativeElement.focus();
} else if (this.comboInput?.nativeElement !== this.document.activeElement) {
this._onTouchedCallback();
this.updateValidity();
}
I've added an else if condition to check if the currently active element is not this.comboInput
. Initially, I expected this to work with a simple else, but the touched and validation were still somehow being updated unnecessarily. This extra check for the active element seems to prevent those unnecessary updates effectively.
I am sticking to what's written in the Angular's docs => When the user blurs the form control element, the control is marked as "touched"
.
Let me know if you have any suggestions or concerns about this approach!
Closes #14900
Additional information (check all that apply):
Checklist:
feature/README.MD
updates for the feature docsREADME.MD
CHANGELOG.MD
updates for newly added functionalityng update
migrations for the breaking changes (migrations guidelines)