Skip to content

Commit 7ff8bbf

Browse files
author
Fabian Nickel
committed
add highlight class
1 parent e67a16a commit 7ff8bbf

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/components/MuiTreeView/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const tree = [
2626
defaultExpanded
2727
onLeafClick={node => alert(JSON.stringify(node))}
2828
highlightId="example-id"
29+
// highlightClass="highlightedClass"
2930
tree={tree}
3031
/>
3132
```

src/components/MuiTreeView/index.jsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ class MuiTreeView extends Component {
8585
expansionPanelDetailsProps: object,
8686
/** Properties applied to the ListItem element. */
8787
listItemProps: object,
88-
/** Id of a leaf which will be highlighted by adding the class */
89-
highlightId: oneOfType[(string, number)],
88+
/** Id of a leaf which will be highlighted by adding the class highlightItem
89+
* or a style given by the hightlightStyle prop.
90+
*/
91+
highlightId: oneOfType([string, number]),
92+
/* styling of the highlighted item */
93+
highlightClass: string,
9094
};
9195

9296
static defaultProps = {
@@ -95,6 +99,7 @@ class MuiTreeView extends Component {
9599
expansionPanelSummaryProps: null,
96100
expansionPanelDetailsProps: null,
97101
listItemProps: null,
102+
highlightClass: null,
98103
};
99104

100105
createFilteredTree = memoize(
@@ -123,11 +128,22 @@ class MuiTreeView extends Component {
123128
expansionPanelDetailsProps,
124129
listItemProps,
125130
highlightId,
131+
highlightClass,
126132
...props
127133
} = this.props;
128134
const value = this.getNodeValue(node);
129135
const id = this.getNodeId(node);
130136
const isLeaf = this.isLeaf(node);
137+
let additionalClasses = {};
138+
139+
if (highlightId === node.id) {
140+
if (highlightClass) {
141+
additionalClasses = { root: highlightClass };
142+
} else {
143+
additionalClasses = { root: classes.highlightItem };
144+
}
145+
}
146+
131147
const textIndent = isLeaf
132148
? depth * unit + unit + (parent ? unit : 0)
133149
: unit * depth + unit;
@@ -146,7 +162,7 @@ class MuiTreeView extends Component {
146162
value={value}
147163
onClick={() => this.handleLeafClick({ value, parent, id })}
148164
button
149-
classes={highlightId === id ? { root: classes.highlightItem } : {}}
165+
classes={additionalClasses}
150166
{...listItemProps}>
151167
<div className={classes.text}>{value}</div>
152168
</ListItem>

0 commit comments

Comments
 (0)