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

fix(tree-view): add aria multiselectable attribute when needed #1563

Merged
merged 6 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion projects/angular/clarity.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4309,7 +4309,7 @@ export class ClrTooltipTrigger {
// @public (undocumented)
export class ClrTree<T> implements AfterContentInit, OnDestroy {
// Warning: (ae-forgotten-export) The symbol "TreeFocusManagerService" needs to be exported by the entry point index.d.ts
constructor(featuresService: TreeFeaturesService<T>, focusManagerService: TreeFocusManagerService<T>, { nativeElement }: ElementRef<HTMLElement>, renderer: Renderer2, ngZone: NgZone);
constructor(featuresService: TreeFeaturesService<T>, focusManagerService: TreeFocusManagerService<T>, renderer: Renderer2, el: ElementRef<HTMLElement>, ngZone: NgZone);
// (undocumented)
featuresService: TreeFeaturesService<T>;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion projects/angular/src/data/tree-view/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default function (): void {
});

it('adds the aria-multiselectable if tree is selectable and has children', function (this: Context) {
expect(this.clarityElement.getAttribute('aria-multiselectable')).toBe('false');
expect(this.clarityElement.getAttribute('aria-multiselectable')).toBeNull();
this.getClarityProvider(TreeFeaturesService).selectable = true;
this.hostComponent.hasChild = true;
this.detectChanges();
Expand Down
26 changes: 19 additions & 7 deletions projects/angular/src/data/tree-view/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,30 @@
host: {
tabindex: '0',
'[attr.role]': '"tree"',
'[attr.aria-multiselectable]': 'isMultiSelectable',
},
})
export class ClrTree<T> implements AfterContentInit, OnDestroy {
@ContentChildren(ClrTreeNode) private rootNodes: QueryList<ClrTreeNode<T>>;

private subscriptions: Subscription[] = [];
private _isMultiSelectable = false;

constructor(
public featuresService: TreeFeaturesService<T>,
private focusManagerService: TreeFocusManagerService<T>,
{ nativeElement }: ElementRef<HTMLElement>,
mivaylo marked this conversation as resolved.
Show resolved Hide resolved
renderer: Renderer2,
private renderer: Renderer2,
private el: ElementRef<HTMLElement>,
ngZone: NgZone
) {
const subscription = ngZone.runOutsideAngular(() =>
fromEvent(nativeElement, 'focusin').subscribe((event: FocusEvent) => {
if (event.target === nativeElement) {
fromEvent(this.el.nativeElement, 'focusin').subscribe((event: FocusEvent) => {

Check failure on line 54 in projects/angular/src/data/tree-view/tree.ts

View workflow job for this annotation

GitHub Actions / PR Build (Angular v15)

Don't use 'this' in constructors for parameter properties
kevinbuhmann marked this conversation as resolved.
Show resolved Hide resolved
if (event.target === this.el.nativeElement) {

Check failure on line 55 in projects/angular/src/data/tree-view/tree.ts

View workflow job for this annotation

GitHub Actions / PR Build (Angular v15)

Don't use 'this' in constructors for parameter properties
// After discussing with the team, I've made it so that when the tree receives focus, the first visible node will be focused.
// This will prevent from the page scrolling abruptly to the first selected node if it exist in a deeply nested tree.
focusManagerService.focusFirstVisibleNode();
// when the first child gets focus,
// tree should no longer have tabindex of 0.
renderer.removeAttribute(nativeElement, 'tabindex');
renderer.removeAttribute(this.el.nativeElement, 'tabindex');

Check failure on line 61 in projects/angular/src/data/tree-view/tree.ts

View workflow job for this annotation

GitHub Actions / PR Build (Angular v15)

Don't use 'this' in constructors for parameter properties
}
})
);
Expand All @@ -72,13 +72,15 @@
}

get isMultiSelectable() {
return this.featuresService.selectable && this.rootNodes.length > 0;
return this._isMultiSelectable;
}

ngAfterContentInit() {
this.setRootNodes();
this.subscriptions.push(
this.rootNodes.changes.subscribe(() => {
this.setMultiSelectable();

this.setRootNodes();
})
);
Expand All @@ -88,6 +90,16 @@
this.subscriptions.forEach(sub => sub.unsubscribe());
}

private setMultiSelectable() {
if (this.featuresService.selectable && this.rootNodes.length > 0) {
this._isMultiSelectable = true;
this.renderer.setAttribute(this.el.nativeElement, 'aria-multiselectable', 'true');
} else {
this._isMultiSelectable = false;
this.renderer.removeAttribute(this.el.nativeElement, 'aria-multiselectable');
}
}

private setRootNodes(): void {
// if node has no parent, it's a root node
// for recursive tree, this.rootNodes registers also nested children
Expand Down
Loading