Skip to content

fix(examples): fix form-field-custom-control #15147

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

Merged
merged 1 commit into from
Feb 22, 2019

Conversation

swseverance
Copy link
Contributor

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Feb 11, 2019
@swseverance
Copy link
Contributor Author

@crisbeto would you mind taking a look? Thank you

_handleBlur(event: FocusEvent): void {
const inputs = Array.from(this.elRef.nativeElement.querySelectorAll('input'));

if (!inputs.some(input => event.relatedTarget === input)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this guaranteed to always be true? The only inputs that have the listener are the ones inside the elRef from what I can tell.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@crisbeto there are scenarios in which this evaluates to false. For example, if the user clicks an entirely different form control (which receives focus) then the blur event's relatedTarget will not be equal to one of the three input elements defined in MyTelInput's template.

My rationale is that if the user is changing focus between the three input elements there's no need to mark the form control as touched. However, if they focus on any element other than one of the three inputs it is appropriate to mark the form control as touched.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, although I remember relatedTarget being flaky on some browsers. This would be a bit cleaner if you used the CDK focus monitor with subtree monitoring.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@crisbeto thanks. The code is cleaner when relying on the CDK focus monitor.

* Update `MyTelInput` to implement `ControlValueAccessor` interface
* Resolves angular#14810
@swseverance swseverance force-pushed the custom-form-field-control branch from d0bcf79 to d47c68e Compare February 18, 2019 22:54
Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

this.parts = fb.group({
area: '',
exchange: '',
subscriber: '',
});

fm.monitor(elRef, true).subscribe(origin => {
if (this.focused && !origin) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think that we can safely assume that focused will always be true when the origin == null, because you can't blur an element that doesn't have focus.

@crisbeto crisbeto added pr: lgtm action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release labels Feb 19, 2019
@jelbourn jelbourn added the merge: fix commit message When the PR is merged, rewrites/fixups of the commit messages are needed label Feb 19, 2019
Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jelbourn jelbourn merged commit 83135e1 into angular:master Feb 22, 2019
@SebastianEShelby
Copy link

I tried adding validators.required to the 'parts' form group and while it works for the form control, the form component that is using this form control won't recognize the validation. Can someone implement the validation please?

@swseverance
Copy link
Contributor Author

@Alex-Samari did you happen to try this by modifying the StackBlitz on https://material.angular.io/guide/creating-a-custom-form-field-control? It appears that the changes I made are not yet deployed to StackBlitz.

@SebastianEShelby
Copy link

@swseverance No, I'm working on my own project and followed the changes you made in the gitHub repo. The value accessor works and I'm able to change the value of the custom form control but when I add validators to the form group, I can't access those validators in the parent component that is using the custom form control

@swseverance
Copy link
Contributor Author

@Alex-Samari Is there a branch I can look at? You can just email me directly if you'd like. My email is on my profile.

@SebastianEShelby
Copy link

@swseverance I couldn't find your email but here's a repository I made of what I have:
https://github.com/Alex-Samari/Angular-custom-form-field

I'm using the custom-tel-control inside my form component

@swseverance
Copy link
Contributor Author

@Alex-Samari I didn't implement validation as part of MyTelInput because it seemed like it was beyond the scope of what the tutorial was intended to cover. However, I created a stack blitz with a custom validator that hopefully achieves what you're looking for.

https://stackblitz.com/edit/angular-byencm?file=app%2Fform-field-custom-control-example.ts

my email is [email protected]

@SebastianEShelby
Copy link

@swseverance Thank you! Validation is working perfectly fine now 👍

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement merge: fix commit message When the PR is merged, rewrites/fixups of the commit messages are needed target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Creating a custom form field control documentation example not working
5 participants