material icon indicating copy to clipboard operation
material copied to clipboard

Have option to set initial focus to Cancel in $mdDialog.confirm()

Open rtrix opened this issue 11 years ago • 11 comments

Currently, the dialog $mdDialog.confirm() starts off with the ok action button focused. This is potentially very dangerous if what you're confirming is a destructive action like deletion. Could we get and option to set the initial focus to the cancel button instead?

rtrix avatar Jun 17 '15 22:06 rtrix

+1, or just not have either of them focused

amritk avatar Jun 26 '15 19:06 amritk

See md-autoFocus feature.

ThomasBurleson avatar Sep 04 '15 11:09 ThomasBurleson

@ThomasBurleson, where?

baelter avatar Oct 23 '15 16:10 baelter

@ThomasBurleson Apparently its not documented https://github.com/angular/material/issues/4737

amritk avatar Oct 23 '15 18:10 amritk

and, if I do not want to focus on any button?

patapron avatar Aug 05 '16 09:08 patapron

+1: please re-open, AFAIK there is no straight forward way to do this with $mdDialog.confirm() (html is on angular side so not possible to just add md-autofocus to cancel button)

fbriand avatar Aug 22 '16 11:08 fbriand

+1: I agree, I also can't think of any way to set md-autofocus on $mdDialog.confirm()

gilad905 avatar Sep 09 '16 07:09 gilad905

@gilad905 Let's try this code

var confirm = $mdDialog.confirm({onComplete: function afterShowAnimation() {
                        var $dialog = angular.element(document.querySelector('md-dialog'));
                        var $actionsSection = $dialog.find('md-dialog-actions');
                        var $cancelButton = $actionsSection.children()[0];
                        var $confirmButton = $actionsSection.children()[1];
                        angular.element($confirmButton).removeClass('md-focused');
                        angular.element($cancelButton).addClass('md-focused');
                        $cancelButton.focus();
            }})
.title('Delete book')
.textContent('Do you want to delete this book?')
.ariaLabel('Delete book')
.targetEvent(null)
.ok('Yes')
.cancel('No');

quocphu avatar Sep 09 '16 16:09 quocphu

Thanks, @quocphu, this does work. Although isn't direct DOM manipulation from a controller kind of a bad practice? or is this not considered to be made from a controller, because afterShowAnimation runs within the mdDialog directive?

gilad905 avatar Sep 12 '16 11:09 gilad905

Thanks, @quocphu, its working perfect

rajutanniru44 avatar Apr 15 '17 09:04 rajutanniru44

@quocphu thank you for the tip. You can also use $document[0].activeElement.blur(); in the onComplete callback.

farantesrodrigues avatar Jul 19 '18 13:07 farantesrodrigues