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

Add ITreeNode.nodeData property #2132

Merged
merged 11 commits into from
Mar 7, 2018
Merged
Changes from 3 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
13 changes: 10 additions & 3 deletions packages/core/src/components/tree/treeNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { safeInvoke } from "../../common/utils";
import { Collapse } from "../collapse/collapse";
import { Icon, IconName } from "../icon/icon";

export interface ITreeNode extends IProps {
export interface ITreeNode<T = {}> extends IProps {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we say T = undefined? more accurate for the default case. if you want to use <T> then you have to define it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When changing the generic constrain to <T = undefined>, each call to safeInvoke() won't complied:
Argument of type 'this' is not assignable to parameter of type 'TreeNode'.

I don't see the problem with the <T = {}> constrain, the user can still use the tree without declaring on any type.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was my original intent too, but per @adidahiya requested 16 days ago:
"you can set a default for the generic type parameter so that users who don't need it won't have to specify T.
use nodeData: T where the interface is defined as ITreeNode<T = {}>, similar to queryList:
blueprint/packages/select/src/components/query-list/queryList.tsx (Line 15 in ff76c4a)"

since both of you are reviewers and contributors, I suggest you guys touch bases and sync your request.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess {} is fine, since React does the same thing and state doesn't actually default to anything (you gotta initialize it yourself)

/**
* Child tree nodes of this node.
*/
Expand Down Expand Up @@ -56,9 +56,16 @@ export interface ITreeNode extends IProps {
* A secondary label/component that is displayed at the right side of the node.
*/
secondaryLabel?: string | JSX.Element;

/**
* An optional custom user object to associate with the node.
* This property can then be used in the `onClick`, `onContextMenu` and `onDoubleClick`
* event handlers for doing custom logic per node.
*/
nodeData?: T;
}

export interface ITreeNodeProps extends ITreeNode {
export interface ITreeNodeProps<T = {}> extends ITreeNode<T> {
children?: React.ReactNode;
contentRef?: (node: TreeNode, element: HTMLDivElement | null) => void;
depth: number;
Expand All @@ -71,7 +78,7 @@ export interface ITreeNodeProps extends ITreeNode {
path: number[];
}

export class TreeNode extends React.Component<ITreeNodeProps, {}> {
export class TreeNode<T = {}> extends React.Component<ITreeNodeProps<T>, {}> {
public render() {
const { children, className, hasCaret, icon, isExpanded, isSelected, label } = this.props;

Expand Down