Skip to content

Conversation

@9kubczas4
Copy link
Collaborator

No description provided.

@netlify
Copy link

netlify bot commented Feb 25, 2023

Deploy Preview for charming-faun-72b6b3 ready!

Name Link
🔨 Latest commit 64cb7dc
🔍 Latest deploy log https://app.netlify.com/sites/charming-faun-72b6b3/deploys/63fa576645a95b00076073e5
😎 Deploy Preview https://deploy-preview-19--charming-faun-72b6b3.netlify.app/snippets/disable-components-using-directive
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@santoshyadavdev
Copy link
Owner

Looks like it is missing DestroyedDirective

private readonly destroyed$ = inject(DestroyedDirective).destroyed$;

constructor(private readonly service: ExampleService,
@Optional() @Self() private readonly button: MatButton,
Copy link
Collaborator

@yharaskrik yharaskrik Feb 25, 2023

Choose a reason for hiding this comment

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

I think you should be able to make this more generic by doing something like

private readonly elementRef: ElementRef

as that will get the generic host element

then you could do something like

    this.service.shouldDisable$
    .pipe(takeUntil(this.destroyed$))
    .subscribe((shouldDisable) => {
      if (this.elementRef.hasOwnProperty('disabled')) {
        this.elementRef.disabled = shouldDisable;
      } 
    });

Then it would work for any host element that has a disabled property.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea, to make it more generic. Will adjust it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd give it a try in a sample app. I thiiiink it will work but not positive.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hi guys,
@santoshyadavdev - I've added the link to the tweet about the destroyed directive.
@yharaskrik - I remembered why I once implemented it this way. Using elementRef.nativeElement.disabled = true, it doesn't work for MatSelect.
For MatButton it works in most cases, i.e. it works when we trigger the change of the enabled/disabled status change in runtime, but it doesn't work when we want the button to be disabled on init. At first glance, it looks like it's some kind of problem with change detection on the material side.

I added elementRef to the code snippet so that we can disable other elements that will allow us to do so, e.g. input, html select, or button.

Copy link
Owner

Choose a reason for hiding this comment

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

Approved @yharaskrik let me know if it looks good now?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ya looks good to me

@santoshyadavdev santoshyadavdev merged commit cfba963 into santoshyadavdev:main Feb 27, 2023
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.

3 participants