Skip to content

refactor(tree): avoid improper query usage #16540

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

Merged
merged 1 commit into from
Jul 19, 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
36 changes: 17 additions & 19 deletions src/material/tree/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ import {
import {
AfterContentInit,
Attribute,
ContentChildren,
Directive,
ElementRef,
Input,
IterableDiffers,
OnDestroy,
QueryList,
} from '@angular/core';
import {
CanDisable,
Expand All @@ -32,16 +30,11 @@ import {
mixinDisabled,
mixinTabIndex,
} from '@angular/material/core';

import {MatTreeNodeOutlet} from './outlet';
import {coerceBooleanProperty} from '@angular/cdk/coercion';

const _MatTreeNodeMixinBase: HasTabIndexCtor & CanDisableCtor & typeof CdkTreeNode =
mixinTabIndex(mixinDisabled(CdkTreeNode));

const _MatNestedTreeNodeMixinBase:
HasTabIndexCtor & CanDisableCtor & typeof CdkNestedTreeNode =
mixinTabIndex(mixinDisabled(CdkNestedTreeNode));

/**
* Wrapper for the CdkTree node with Material design styles.
*/
Expand Down Expand Up @@ -95,31 +88,36 @@ export class MatTreeNodeDef<T> extends CdkTreeNodeDef<T> {
'[attr.role]': 'role',
'class': 'mat-nested-tree-node',
},
inputs: ['disabled', 'tabIndex'],
providers: [
{provide: CdkNestedTreeNode, useExisting: MatNestedTreeNode},
{provide: CdkTreeNode, useExisting: MatNestedTreeNode},
{provide: CDK_TREE_NODE_OUTLET_NODE, useExisting: MatNestedTreeNode}
]
})
export class MatNestedTreeNode<T> extends _MatNestedTreeNodeMixinBase<T> implements
AfterContentInit, CanDisable, HasTabIndex, OnDestroy {
export class MatNestedTreeNode<T> extends CdkNestedTreeNode<T> implements AfterContentInit,
OnDestroy {
@Input('matNestedTreeNode') node: T;

/** The children node placeholder. */
@ContentChildren(MatTreeNodeOutlet, {
// We need to use `descendants: true`, because Ivy will no longer match
// indirect descendants if it's left as false.
descendants: true
})
nodeOutlet: QueryList<MatTreeNodeOutlet>;
/** Whether the node is disabled. */
@Input()
get disabled() { return this._disabled; }
set disabled(value: any) { this._disabled = coerceBooleanProperty(value); }
private _disabled = false;

/** Tabindex for the node. */
@Input()
get tabIndex(): number { return this.disabled ? -1 : this._tabIndex; }
set tabIndex(value: number) {
// If the specified tabIndex value is null or undefined, fall back to the default value.
this._tabIndex = value != null ? value : 0;
}
private _tabIndex: number;

constructor(protected _elementRef: ElementRef<HTMLElement>,
protected _tree: CdkTree<T>,
protected _differs: IterableDiffers,
@Attribute('tabindex') tabIndex: string) {
super(_elementRef, _tree, _differs);

this.tabIndex = Number(tabIndex) || 0;
}

Expand Down
6 changes: 5 additions & 1 deletion src/material/tree/outlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import {
* inside the outlet.
*/
@Directive({
selector: '[matTreeNodeOutlet]'
selector: '[matTreeNodeOutlet]',
providers: [{
provide: CdkTreeNodeOutlet,
useExisting: MatTreeNodeOutlet
}]
})
export class MatTreeNodeOutlet implements CdkTreeNodeOutlet {
constructor(
Expand Down
5 changes: 3 additions & 2 deletions tools/public_api_guard/material/tree.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export declare class MatNestedTreeNode<T> extends _MatNestedTreeNodeMixinBase<T> implements AfterContentInit, CanDisable, HasTabIndex, OnDestroy {
export declare class MatNestedTreeNode<T> extends CdkNestedTreeNode<T> implements AfterContentInit, OnDestroy {
protected _differs: IterableDiffers;
protected _elementRef: ElementRef<HTMLElement>;
protected _tree: CdkTree<T>;
disabled: any;
node: T;
nodeOutlet: QueryList<MatTreeNodeOutlet>;
tabIndex: number;
constructor(_elementRef: ElementRef<HTMLElement>, _tree: CdkTree<T>, _differs: IterableDiffers, tabIndex: string);
ngAfterContentInit(): void;
ngOnDestroy(): void;
Expand Down