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

fixed #7575 and #7561 Tab navigation broken in dialog in 7.1.1 #7576

Merged
merged 1 commit into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions src/app/components/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export class Dialog implements OnDestroy {

@Input() maximizable: boolean;

@Input() focusTrap: boolean = true;

@Input() transitionOptions: string = '150ms cubic-bezier(0, 0, 0.2, 1)';

@Input() closeIcon: string = 'pi pi-times';
Expand Down Expand Up @@ -408,30 +410,48 @@ export class Dialog implements OnDestroy {
}
}

getFocusableElements() {
let focusableElements = DomHandler.find(this.container,`button:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]),
[href][clientHeight][clientWidth]:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]),
input:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]), select:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]),
textarea:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]), [tabIndex]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]),
[contenteditable]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])`
);

let visibleFocusableElements = [];
for(let focusableElement of focusableElements) {
if(getComputedStyle(focusableElement).display != "none" && getComputedStyle(focusableElement).visibility != "hidden")
visibleFocusableElements.push(focusableElement);
}
return visibleFocusableElements;
}

onKeydown(event: KeyboardEvent) {
if(event.which === 9) {
event.preventDefault();

let focusableElements = DomHandler.find(this.container,'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])');

if (focusableElements && focusableElements.length > 0) {
if (!document.activeElement) {
focusableElements[0].focus();
}
else {
let focusedIndex = focusableElements.indexOf(document.activeElement);

if (event.shiftKey) {
if (focusedIndex == -1 || focusedIndex === 0)
focusableElements[focusableElements.length - 1].focus();
else
focusableElements[focusedIndex - 1].focus();
if(this.focusTrap) {
if(event.which === 9) {
event.preventDefault();

let focusableElements = this.getFocusableElements();

if (focusableElements && focusableElements.length > 0) {
if (!document.activeElement) {
focusableElements[0].focus();
}
else {
if (focusedIndex == -1 || focusedIndex === (focusableElements.length - 1))
focusableElements[0].focus();
else
focusableElements[focusedIndex + 1].focus();
let focusedIndex = focusableElements.indexOf(document.activeElement);

if (event.shiftKey) {
if (focusedIndex == -1 || focusedIndex === 0)
focusableElements[focusableElements.length - 1].focus();
else
focusableElements[focusedIndex - 1].focus();
}
else {
if (focusedIndex == -1 || focusedIndex === (focusableElements.length - 1))
focusableElements[0].focus();
else
focusableElements[focusedIndex + 1].focus();
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/app/showcase/components/dialog/dialogdemo.html
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ <h3>Properties</h3>
<td>true</td>
<td>When enabled, first button receives focus on show.</td>
</tr>
<tr>
<td>focusTrap</td>
<td>boolean</td>
<td>true</td>
<td>When enabled, can only focus on elements inside the dialog.</td>
</tr>
<tr>
<td>maximizable</td>
<td>boolean</td>
Expand Down