DEV Community

Cover image for Wrapping Angular Material button in custom Angular component (part 2)

Wrapping Angular Material button in custom Angular component (part 2)

Dzhavat Ushev on October 26, 2021

In the first part of this post we ended up with a simple but repetitive solution to wrapping Angular Material button in a custom component. In this...
Collapse
 
benlune profile image
Benoît Plâtre

Hi Dzhavat,
Thanks a lot for this blog post and the idea behind it.
I think it's a good approach, but I wondere how you would manage some important properties for buttons, like disabled and type ('submit').
When we use ngComponentOutlet we can't use input. Maybe we would have to create other types of components, for submit it could be fine, but for disabled, which is supposed to be dynamic, it might be harder.

Collapse
 
dzhavat profile image
Dzhavat Ushev Playful Programming Angular

Hi Benoît,
Thanks for you comment.

I wondere how you would manage some important properties for buttons, like disabled and type ('submit').

We're using this approach on a button component at work. We recently had a use case where we had to change the type of one button to submit and another one to button. What we did was we introduced a new property. We changed the type property to accept a type (i.e. submit or button, button being the default) and we added a variant which could be primary, secondary or text.

As for the disabled attribute, the ngComponentOutlet has an injector input which can be used to inject values. How we use it is to provide a custom token, which is also injected in the internal components as well. This way it's possible to pass dynamic values. If you're interested in knowing more, I can point you to some examples. Hope some of this helps you :)

Collapse
 
benlune profile image
Benoît Plâtre

Hi Dzhavat,
Thanks a lot for your reply. Yes I learnt than we can use an injector in the ngComponentOutlet params but I didn't succeed making it work yet. If you have an example, it would be helpful :-)

Thread Thread
 
dzhavat profile image
Dzhavat Ushev Playful Programming Angular

Sure :)

In this code you can see how we create the injector which is later passed to ngComponentOutlet in the template. We provide disabledAttributeToken which is injected in all internal buttons to control the disabled state. The same way we inject the type attribute.

Thread Thread
 
benlune profile image
Benoît Plâtre

Hi Dzhavat,
Thanks a lot for this example ! It will help a lot, hopefully not only me :-)
Have a nice day !

Thread Thread
 
benlune profile image
Benoît Plâtre • Edited

Hi Dzhavat,
I could successfully implement my version on your button ;-)
I faced a problem regarding the disabled property. It works well on instanciated buttons, but, when listening to click event on the wrapper, you still get the event, even if the button is disabled.
I tried to preventDefault/stopPropagation the click event from the Host but it didn't work.
Finally I did this :

constructor(
    private injector: Injector,
    private el: ElementRef<HTMLElement>
  ) {}

  ngOnChanges(changes: SimpleChanges): void {
    const { nativeElement } = this.el;
    if (this.disabled) {
      nativeElement.style.pointerEvents = 'none';
    } else {
      nativeElement.style.pointerEvents = 'auto';
    }
  }
Enter fullscreen mode Exit fullscreen mode

I don't know if you faced that problem yet :-)

Collapse
 
beazer profile image
Dave

Learned some good stuff in these two posts - thanks!

Collapse
 
dzhavat profile image
Dzhavat Ushev Playful Programming Angular

Thanks Dave. I'm glad to hear that 😊
Other parts will be coming as well :)

Collapse
 
beazer profile image
Dave

Cool, I look forward to it. ✌🏻