Skip to content

Commit

Permalink
Fixed #15749 | Dialog: Focus to input element set before transition ends
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmetcetin01140 committed Nov 15, 2024
1 parent fbaf913 commit 5397623
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/primeng/src/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,18 +597,39 @@ export class Dialog extends BaseComponent implements OnInit, OnDestroy {
return this.header !== null ? uuid('pn_id_') + '_header' : null;
}

parseDurationToMilliseconds(durationString: string): number | undefined {
const transitionTimeRegex = /([\d\.]+)(ms|s)\b/g;
let totalMilliseconds = 0;
let match;
while ((match = transitionTimeRegex.exec(durationString)) !== null) {
const value = parseFloat(match[1]);
const unit = match[2];
if (unit === 'ms') {
totalMilliseconds += value;
} else if (unit === 's') {
totalMilliseconds += value * 1000;
}
}
if (totalMilliseconds === 0) {
return undefined;
}
return totalMilliseconds;
}

focus(focusParentElement = this.contentViewChild.nativeElement) {
const timeoutDuration = this.parseDurationToMilliseconds(this.transitionOptions);
let focusable = DomHandler.getFocusableElement(focusParentElement, '[autofocus]');

if (focusable) {
this.zone.runOutsideAngular(() => {
setTimeout(() => focusable.focus(), 5);
setTimeout(() => focusable.focus(), timeoutDuration || 5);
});
return;
}
const focusableElement = DomHandler.getFocusableElement(focusParentElement);
if (focusableElement) {
this.zone.runOutsideAngular(() => {
setTimeout(() => focusableElement.focus(), 5);
setTimeout(() => focusableElement.focus(), timeoutDuration || 5);
});
} else if (this.footerViewChild) {
// If the content section is empty try to focus on footer
Expand Down

0 comments on commit 5397623

Please sign in to comment.