Skip to content
Closed
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
52 changes: 0 additions & 52 deletions src/scripts/Tree.js

This file was deleted.

68 changes: 68 additions & 0 deletions src/scripts/Tree.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React, {
Component,
Children,
cloneElement,
HTMLAttributes,
} from 'react';
import classnames from 'classnames';

export type TreeProps = {
className?: string;
label?: string;
toggleOnNodeClick?: boolean;
onNodeClick?: (...args: any[]) => any;
onNodeToggle?: (...args: any[]) => any;
onNodeLabelClick?: (...args: any[]) => any;
};

export class Tree extends Component<
TreeProps & HTMLAttributes<HTMLDivElement>,
{}
> {
constructor(
props: Readonly<TreeProps & React.HTMLAttributes<HTMLDivElement>>
) {
super(props);
this.renderTreeNode = this.renderTreeNode.bind(this);
}

renderTreeNode(tnode: any) {
const {
onNodeClick,
onNodeToggle,
onNodeLabelClick,
toggleOnNodeClick,
} = this.props;
return cloneElement(tnode, {
level: 1,
onNodeClick,
onNodeToggle,
onNodeLabelClick,
toggleOnNodeClick,
});
}

render() {
const {
className,
label,
children,
/* eslint-disable @typescript-eslint/no-unused-vars */
onNodeClick,
onNodeToggle,
onNodeLabelClick,
toggleOnNodeClick,
/* eslint-enable @typescript-eslint/no-unused-vars */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://github.com/mashmatrix/react-lightning-design-system/pull/315/files#diff-b8e1116dcaaf4336c2da8bf811daa6c1L32
Before converting to TypeScript, propTypes were used in cleanProps() to avoid passing unnecessary props (like onNodeClick) to <div> element.
I couldn't replace it with TypeScript's types, so this is a workaround.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I don't do this, the following runtime warnings occur:

Warning: Unknown event handler property `onNodeClick`. It will be ignored.
Warning: Unknown event handler property `onNodeToggle`. It will be ignored.
Warning: Unknown event handler property `onNodeLabelClick`. It will be ignored.
Warning: React does not recognize the `toggleOnNodeClick` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `toggleonnodeclick` instead. If you accidentally passed it from a parent component, remove it from the DOM element.

...props
} = this.props;
const treeClassNames = classnames(className, 'slds-tree-container');
return (
<div className={treeClassNames} role='application' {...props}>
{label ? <h4 className='slds-text-heading--label'>{label}</h4> : null}
<ul className='slds-tree' role='tree'>
{Children.map(children, this.renderTreeNode)}
</ul>
</div>
);
}
}
3 changes: 1 addition & 2 deletions src/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import Picklist, { PicklistItem } from './Picklist';
import DateInput from './DateInput';
import Lookup from './Lookup';
import FieldSet from './FieldSet';
import Tree from './Tree';
import TreeNode from './TreeNode';
import Spinner from './Spinner';
import Grid, { Row, Col } from './Grid';
Expand Down Expand Up @@ -75,7 +74,6 @@ export {
DateInput,
Lookup,
FieldSet,
Tree,
TreeNode,
Spinner,
Grid,
Expand Down Expand Up @@ -116,4 +114,5 @@ export * from './Form';
export * from './FormElement';
export * from './Text';
export * from './Textarea';
export * from './Tree';
export * from './ComponentSettings';
44 changes: 22 additions & 22 deletions stories/TreeStories.js → stories/TreeStories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,57 @@ storiesOf('Tree', module).add(
onNodeClick={action('nodeClick')}
onNodeToggle={action('nodeToggle')}
onNodeLabelClick={action('nodeLabelClick')}
toggleOnNodeClick={boolean('toggleOnNodeClick')}
toggleOnNodeClick={boolean('toggleOnNodeClick', false)}
>
<TreeNode
label={text('label item #1', 'Item #1')}
opened={boolean('opened item #1', true)}
selected={boolean('selected item #1', true)}
loading={boolean('loading item #1')}
leaf={boolean('leaf item #1')}
loading={boolean('loading item #1', false)}
leaf={boolean('leaf item #1', false)}
>
<TreeNode
label={text('label item #1-1', 'Item #1-1')}
opened={boolean('opened item #1-1')}
selected={boolean('selected item #1-1')}
loading={boolean('loading item #1-1')}
opened={boolean('opened item #1-1', false)}
selected={boolean('selected item #1-1', false)}
loading={boolean('loading item #1-1', false)}
leaf={boolean('leaf item #1-1', true)}
/>
<TreeNode
label={text('label item #1-2', 'Item #1-2')}
opened={boolean('opened item #1-2')}
selected={boolean('selected item #1-2')}
loading={boolean('loading item #1-2')}
leaf={boolean('leaf item #1-2')}
opened={boolean('opened item #1-2', false)}
selected={boolean('selected item #1-2', false)}
loading={boolean('loading item #1-2', false)}
leaf={boolean('leaf item #1-2', false)}
>
<TreeNode
label={text('label item #1-2-1', 'Item #1-2-1')}
opened={boolean('opened item #1-2-1')}
selected={boolean('selected item #1-2-1')}
loading={boolean('loading item #1-2-1')}
opened={boolean('opened item #1-2-1', false)}
selected={boolean('selected item #1-2-1', false)}
loading={boolean('loading item #1-2-1', false)}
leaf={boolean('leaf item #1-2-1', true)}
/>
<TreeNode
label={text('label item #1-2-2', 'Item #1-2-2')}
opened={boolean('opened item #1-2-2')}
selected={boolean('selected item #1-2-2')}
loading={boolean('loading item #1-2-2')}
opened={boolean('opened item #1-2-2', false)}
selected={boolean('selected item #1-2-2', false)}
loading={boolean('loading item #1-2-2', false)}
leaf={boolean('leaf item #1-2-2', true)}
/>
</TreeNode>
<TreeNode
label={text('label item #1-3', 'Item #1-3')}
opened={boolean('opened item #1-3')}
selected={boolean('selected item #1-3')}
loading={boolean('loading item #1-3')}
opened={boolean('opened item #1-3', false)}
selected={boolean('selected item #1-3', false)}
loading={boolean('loading item #1-3', false)}
leaf={boolean('leaf item #1-3', true)}
/>
</TreeNode>
<TreeNode
label={text('label item #2', 'Item #2')}
opened={boolean('opened item #2')}
selected={boolean('selected item #2')}
loading={boolean('loading item #2')}
opened={boolean('opened item #2', false)}
selected={boolean('selected item #2', false)}
loading={boolean('loading item #2', false)}
leaf={boolean('leaf item #2', true)}
/>
</Tree>
Expand Down
1 change: 1 addition & 0 deletions test/storyshots/__snapshots__/storyshots.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45815,6 +45815,7 @@ exports[`Storyshots Tree Controlled with knobs 1`] = `
</div>
</li>
<li
aria-expanded={false}
aria-level={2}
role="treeitem"
>
Expand Down