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

DynamicFormsInstanceService & support for additional form field components #911

Merged
merged 10 commits into from
Jan 14, 2019

Conversation

Karamuto
Copy link
Collaborator

@Karamuto Karamuto commented Jan 4, 2019

As this was requested now a few times, I sat down and created a service which allows to retrieve the ComponentRef of a form model object.

This will hopefully provide a way to solve some of the issues like #899 , #852, #806

How to use:

First the service needs to be provided, this can be done either by using the 'forRoot()' on the CoreModule, or by simply providing it in a module or at component level. This also means for people not using 'forRoot()' this be a breaking change as they has to provide it additionally.

Next you inject the service in any component and/or service the name is as stated : 'DynamicFormsInstanceService'

After the view has inialized ( ngAfterViewInit ), one is able to call 'getFormControlInstance(modelId: string, index?: number)'. This will return a ComponentRef to the DynamicFormControl with the given id ( and index for form array elements ).

One thing which can cause issues here is if the same id is used multiple times ( except for FormArrays ), but this can solved by seperating elements with the same id into different components/modules and providing the service additionally for this components/modules.

Edit: Also added support for an 'isFormFieldControl' property in 'additional'. This allows for to mark a model as form field control and is useful when adding own material form field controls, but don't override existing ones.

@coveralls
Copy link

coveralls commented Jan 4, 2019

Coverage Status

Coverage decreased (-0.006%) to 94.902% when pulling 3958fe9 on Karamuto:development into 147edf2 on udos86:development.

@Karamuto Karamuto requested a review from udos86 January 8, 2019 07:37
@Karamuto Karamuto changed the title DynamicFormsInstanceService DynamicFormsInstanceService & support for additional form field components Jan 10, 2019
@Karamuto Karamuto merged commit 722a4df into udos86:development Jan 14, 2019
@regstar
Copy link
Contributor

regstar commented Feb 14, 2019

Hello @Karamuto

Thx for your merge. I had tested this new service and it works, but i am unable to set the focus to the element ref i get back from the service.

E.g.

this.dynamicFormInstanceService.getFormControlInstance("my_id_dynamic_form").location.nativeElement

I will get the complete control with all parent divs.

How do you use this service to set up the focus on the control?

Thx for your help

@Karamuto
Copy link
Collaborator Author

Hello @regstar ,

its actually a little weird in you case ( ng-bootstrap? ) you could place a class on the form control you want to focus ( or maybe use the tagname if sufficient ) and make something like this:

const htmlEl: HTMLElement = this.dynamicFormInstanceService.getFormControlInstance("my_id_dynamic_form").location.nativeElement;
// or by tagname
const matches = htmlEl.getElementsByClassName('form-control-class');

That last will return a node list, which should contain you form control, then you can use the focus() method to let the browser fucos on this control.

I hope it helps

@regstar
Copy link
Contributor

regstar commented Jul 3, 2019

Hello @Karamuto

On which version I can find this service? I could not find this service on the version 7.2+. Cannot find this on dev branch, too.

Can get me some information about this? Thanks!

@Karamuto
Copy link
Collaborator Author

Karamuto commented Jul 3, 2019

Hello @regstar ,

as it seems the service was renamed in newer version to DynamicFormComponentService.

@regstar
Copy link
Contributor

regstar commented Jul 3, 2019

Ah okay thanks. I did not pay attention to that. I will test this service. Thanks for your help on this.

@regstar
Copy link
Contributor

regstar commented Jul 4, 2019

Okay found it now. But I need to upgrade to ng-dynamic-forms 8+. Is this compatible with angular 7?

@CAlex-Wilson
Copy link

CAlex-Wilson commented Nov 14, 2019

@Karamuto I recently stumbled upon this while trying to set focus manually. I have this:

form.formGroup.reset();
const typeInput: HTMLElement = this._dynamicFormComponentService.getFormControlRef("Type").location.nativeElement;
console.log(typeNameInput);
// This does grab the correct control.
typeInput.focus()

Even though it has done its job and grabbed me the HTMLElement, focus() does not work. Am I missing a step? My form is located inside of a Material Dialog, if that matters.

@Karamuto
Copy link
Collaborator Author

Hello @CAlex-Wilson,

could you provide what kind of Form Control you were trying to focus?

Maybe even a little stackblitz would be really helpful.

Greetings

@CAlex-Wilson
Copy link

Greetings @Karamuto,

Thanks for the fast response! I am using a DynamicInputModel and I will begin setting up a stackblitz now.

@CAlex-Wilson
Copy link

@Karamuto,

Here is the StackBlitz: Blitz

@Karamuto
Copy link
Collaborator Author

Hi @CAlex-Wilson,

sadly some things seem to have changed since I made this feature.

If you want to focues this input, you need to do it like I changed in the stackblitz. We need to use the instance and focus the matInput in there.

@CAlex-Wilson
Copy link

Hi @Karamuto,

I don't see any differences on your version of the stackblitz. Am I missing it?

@cufeo
Copy link

cufeo commented Jun 18, 2020

Hi @Karamuto can you please provide an example of how to do it properly ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants