Skip to content

Commit

Permalink
Merge pull request #12424 from primefaces/issue-12351
Browse files Browse the repository at this point in the history
Fixed #12351 - Tree with virtualScroll: Inconsistent focus
  • Loading branch information
cetincakiroglu authored Dec 27, 2022
2 parents b553156 + f6d7bca commit e514cd6
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions src/app/components/tree/tree.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
import { CommonModule } from '@angular/common';
import {
NgModule,
AfterContentInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChildren,
ElementRef,
EventEmitter,
forwardRef,
Inject,
Input,
AfterContentInit,
NgModule,
OnChanges,
OnDestroy,
Output,
EventEmitter,
OnInit,
OnChanges,
ContentChildren,
Optional,
Output,
QueryList,
TemplateRef,
Inject,
ElementRef,
forwardRef,
ChangeDetectionStrategy,
SimpleChanges,
ViewEncapsulation,
ViewChild
TemplateRef,
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { Optional } from '@angular/core';
import { CommonModule } from '@angular/common';
import { PrimeNGConfig, TranslationKeys, TreeNode } from 'primeng/api';
import { SharedModule } from 'primeng/api';
import { PrimeTemplate } from 'primeng/api';
import { TreeDragDropService } from 'primeng/api';
import { Subscription } from 'rxjs';
import { BlockableUI } from 'primeng/api';
import { ObjectUtils } from 'primeng/utils';
import { BlockableUI, PrimeNGConfig, PrimeTemplate, SharedModule, TranslationKeys, TreeDragDropService, TreeNode } from 'primeng/api';
import { DomHandler } from 'primeng/dom';
import { RippleModule } from 'primeng/ripple';
import { Scroller, ScrollerModule, ScrollerOptions } from 'primeng/scroller';
import { ObjectUtils } from 'primeng/utils';
import { Subscription } from 'rxjs';

@Component({
selector: 'p-treeNode',
Expand Down Expand Up @@ -67,6 +64,7 @@ import { Scroller, ScrollerModule, ScrollerOptions } from 'primeng/scroller';
[attr.aria-expanded]="this.node.expanded"
[attr.aria-selected]="isSelected()"
[attr.aria-label]="node.label"
[attr.data-id]="node.data"
>
<button type="button" [attr.aria-label]="tree.togglerAriaLabel" class="p-tree-toggler p-link" (click)="toggle($event)" pRipple tabindex="-1">
<span class="p-tree-toggler-icon pi pi-fw" [ngClass]="{ 'pi-chevron-right': !node.expanded, 'pi-chevron-down': node.expanded }"></span>
Expand Down Expand Up @@ -181,6 +179,8 @@ export class UITreeNode implements OnInit {

tree: Tree;

timeout: any;

constructor(@Inject(forwardRef(() => Tree)) tree) {
this.tree = tree as Tree;
}
Expand Down Expand Up @@ -222,6 +222,7 @@ export class UITreeNode implements OnInit {
this.node.expanded = true;
if (this.tree.virtualScroll) {
this.tree.updateSerializedValue();
this.focusVirtualNode();
}
this.tree.onNodeExpand.emit({ originalEvent: event, node: this.node });
}
Expand All @@ -230,6 +231,7 @@ export class UITreeNode implements OnInit {
this.node.expanded = false;
if (this.tree.virtualScroll) {
this.tree.updateSerializedValue();
this.focusVirtualNode();
}
this.tree.onNodeCollapse.emit({ originalEvent: event, node: this.node });
}
Expand Down Expand Up @@ -467,7 +469,6 @@ export class UITreeNode implements OnInit {
}
}
}

event.preventDefault();
break;

Expand All @@ -490,7 +491,6 @@ export class UITreeNode implements OnInit {
if (!this.node.expanded && !this.tree.isNodeLeaf(this.node)) {
this.expand(event);
}

event.preventDefault();
break;

Expand Down Expand Up @@ -552,6 +552,13 @@ export class UITreeNode implements OnInit {
if (this.tree.droppableNodes) element.children[1].children[0].focus();
else element.children[0].children[0].focus();
}

focusVirtualNode() {
this.timeout = setTimeout(() => {
let node = DomHandler.findSingle(document.body, `[data-id="${this.node.data}"]`);
DomHandler.focus(node);
}, 1);
}
}

@Component({
Expand Down Expand Up @@ -580,6 +587,7 @@ export class UITreeNode implements OnInit {
#scroller
*ngIf="virtualScroll"
[items]="serializedValue"
[tabindex]="-1"
styleClass="p-tree-wrapper"
[style]="{ height: scrollHeight }"
[itemSize]="virtualScrollItemSize || _virtualNodeHeight"
Expand All @@ -592,6 +600,7 @@ export class UITreeNode implements OnInit {
<ng-template pTemplate="content" let-items let-scrollerOptions="options">
<ul *ngIf="items" class="p-tree-container" [ngClass]="scrollerOptions.contentStyleClass" [style]="scrollerOptions.contentStyle" role="tree" [attr.aria-label]="ariaLabel" [attr.aria-labelledby]="ariaLabelledBy">
<p-treeNode
#treeNode
*ngFor="let rowNode of items; let firstChild = first; let lastChild = last; let index = index; trackBy: trackBy"
[level]="rowNode.level"
[rowNode]="rowNode"
Expand Down Expand Up @@ -796,7 +805,7 @@ export class Tree implements OnInit, AfterContentInit, OnChanges, OnDestroy, Blo

public dragStopSubscription: Subscription;

constructor(public el: ElementRef, @Optional() public dragDropService: TreeDragDropService, public config: PrimeNGConfig) {}
constructor(public el: ElementRef, @Optional() public dragDropService: TreeDragDropService, public config: PrimeNGConfig, private cd: ChangeDetectorRef) {}

ngOnInit() {
if (this.droppableNodes) {
Expand Down

0 comments on commit e514cd6

Please sign in to comment.