11import { Component } from 'react' ;
2- import { arrayOf , shape , string , func , oneOfType , object } from 'prop-types' ;
2+ import { arrayOf , shape , number , string , func , oneOfType , object } from 'prop-types' ;
33import classNames from 'classnames' ;
44import { prop } from 'ramda' ;
55import memoize from 'fast-memoize' ;
@@ -14,6 +14,7 @@ const pickClassName = prop('className');
1414/** Prop-type for a recursive data structure */
1515const tree = {
1616 value : string . isRequired ,
17+ id : oneOfType [ string , number ] ,
1718} ;
1819
1920Object . assign ( tree , {
@@ -74,8 +75,8 @@ class MuiTreeView extends Component {
7475 expansionPanelDetailsProps : object ,
7576 /** Properties applied to the ListItem element. */
7677 listItemProps : object ,
77- /** Value path for some leaf to highlight */
78- highlightItem : arrayOf ( string ) ,
78+ /** Id of a leaf which will be highlighted by adding the class */
79+ highlightId : oneOfType [ string , number ] ,
7980 } ;
8081
8182 static defaultProps = {
@@ -84,7 +85,7 @@ class MuiTreeView extends Component {
8485 expansionPanelSummaryProps : null ,
8586 expansionPanelDetailsProps : null ,
8687 listItemProps : null ,
87- highlightItem : null ,
88+ highlightId : null ,
8889 } ;
8990
9091 createFilteredTree = memoize (
@@ -95,13 +96,13 @@ class MuiTreeView extends Component {
9596 }
9697 ) ;
9798
98- handleLeafClick = ( value , parent , path ) => {
99+ handleLeafClick = ( leaf ) => {
99100 if ( this . props . onLeafClick ) {
100- this . props . onLeafClick ( value , parent , path ) ;
101+ this . props . onLeafClick ( leaf ) ;
101102 }
102103 } ;
103104
104- renderNode = ( node , parent , depth = 0 , fullPath = [ ] ) => {
105+ renderNode = ( node , parent , depth = 0 ) => {
105106 const {
106107 theme : {
107108 spacing : { unit } ,
@@ -112,11 +113,11 @@ class MuiTreeView extends Component {
112113 expansionPanelSummaryProps,
113114 expansionPanelDetailsProps,
114115 listItemProps,
115- highlightItem ,
116+ highlightId ,
116117 ...props
117118 } = this . props ;
118119 const value = this . getNodeValue ( node ) ;
119- const currentPath = [ ... fullPath , value ] ;
120+ const id = this . getNodeId ( node ) ;
120121 const isLeaf = this . isLeaf ( node ) ;
121122 const textIndent = isLeaf
122123 ? depth * unit + unit + ( parent ? unit : 0 )
@@ -127,24 +128,16 @@ class MuiTreeView extends Component {
127128 }
128129
129130 if ( isLeaf ) {
130- let isHighlighted = false ;
131-
132- if ( highlightItem ) {
133- if ( JSON . stringify ( currentPath ) === JSON . stringify ( highlightItem ) ) {
134- isHighlighted = true ;
135- }
136- }
137-
138131 return (
139132 < ListItem
140133 disableGutters
141134 style = { { textIndent } }
142135 key = { value }
143136 id = { value }
144137 value = { value }
145- onClick = { ( ) => this . handleLeafClick ( value , parent , currentPath ) }
138+ onClick = { ( ) => this . handleLeafClick ( { value, parent, id } ) }
146139 button
147- classes = { isHighlighted ? { root : classes . highlightItem } : { } }
140+ classes = { highlightId === id ? { root : classes . highlightItem } : { } }
148141 { ...listItemProps } >
149142 < div className = { classes . text } > { value } </ div >
150143 </ ListItem >
@@ -179,7 +172,7 @@ class MuiTreeView extends Component {
179172 classes = { { root : classes . panelDetails } }
180173 className = { classNames ( pickClassName ( expansionPanelDetailsProps ) ) } >
181174 { node . nodes . map ( l =>
182- this . renderNode ( l , node , depth + 1 , currentPath )
175+ this . renderNode ( l , node , depth + 1 )
183176 ) }
184177 </ ExpansionPanelDetails >
185178 </ ExpansionPanel >
@@ -194,6 +187,12 @@ class MuiTreeView extends Component {
194187 return typeof node === 'string' ? node : node . value ;
195188 }
196189
190+ getNodeId ( node ) {
191+ if ( typeof node === 'object' ) {
192+ return node . id ;
193+ }
194+ }
195+
197196 filter ( tree ) {
198197 const { searchTerm } = this . props ;
199198
0 commit comments