Skip to content

Commit fd8518c

Browse files
author
Fabian Nickel
committed
add a highlight for a given path
1 parent 576eb6e commit fd8518c

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/components/MuiTreeView/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const tree = [
2525
<MuiTreeView
2626
defaultExpanded
2727
onLeafClick={(node, parent, fullPath) => alert(`${node} clicked, full path: ${JSON.stringify(fullPath)}`)}
28+
highlightItem={['Parent B', 'Parent C', 'Child D']}
2829
tree={tree}
2930
/>
3031
```

src/components/MuiTreeView/index.jsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ const styles = theme => ({
5252
maxWidth: '75vw',
5353
},
5454
expandIcon: {},
55+
highlightItem: {
56+
backgroundColor: 'rgb(220, 227, 239)',
57+
},
5558
});
5659

5760
/**
@@ -71,6 +74,8 @@ class MuiTreeView extends Component {
7174
expansionPanelDetailsProps: object,
7275
/** Properties applied to the ListItem element. */
7376
listItemProps: object,
77+
/** Value path for some leaf to highlight */
78+
highlightItem: arrayOf(string),
7479
};
7580

7681
static defaultProps = {
@@ -79,6 +84,7 @@ class MuiTreeView extends Component {
7984
expansionPanelSummaryProps: null,
8085
expansionPanelDetailsProps: null,
8186
listItemProps: null,
87+
highlightItem: null,
8288
};
8389

8490
createFilteredTree = memoize(
@@ -106,6 +112,7 @@ class MuiTreeView extends Component {
106112
expansionPanelSummaryProps,
107113
expansionPanelDetailsProps,
108114
listItemProps,
115+
highlightItem,
109116
...props
110117
} = this.props;
111118
const value = this.getNodeValue(node);
@@ -120,6 +127,14 @@ class MuiTreeView extends Component {
120127
}
121128

122129
if (isLeaf) {
130+
let isHighlighted = false;
131+
132+
if (highlightItem) {
133+
if (JSON.stringify(currentPath) === JSON.stringify(highlightItem)) {
134+
isHighlighted = true;
135+
}
136+
}
137+
123138
return (
124139
<ListItem
125140
disableGutters
@@ -129,6 +144,7 @@ class MuiTreeView extends Component {
129144
value={value}
130145
onClick={() => this.handleLeafClick(value, parent, currentPath)}
131146
button
147+
classes={isHighlighted ? { root: classes.highlightItem } : {}}
132148
{...listItemProps}>
133149
<div className={classes.text}>{value}</div>
134150
</ListItem>

src/index.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ const tree = [
3030

3131
class App extends Component {
3232
/* eslint-disable-next-line no-alert */
33-
handleLeafClick = (node, parent, fullPath) => alert(`${node} clicked, full path: ${JSON.stringify(fullPath)}`);
33+
handleLeafClick = (node, parent, fullPath) =>
34+
/* eslint-disable-next-line no-alert */
35+
alert(`${node} clicked, full path: ${JSON.stringify(fullPath)}`);
3436

3537
render() {
3638
return (

0 commit comments

Comments
 (0)