-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.jsx
309 lines (284 loc) · 8.03 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import React from 'react';
import {
arrayOf,
bool,
shape,
number,
string,
func,
oneOfType,
object,
node,
} from 'prop-types';
import classNames from 'classnames';
import { prop } from 'ramda';
import memoize from 'fast-memoize';
import { makeStyles, useTheme, withStyles } from '@material-ui/core/styles';
import ListItem from '@material-ui/core/ListItem';
import ListItemText from '@material-ui/core/ListItemText';
import Typography from '@material-ui/core/Typography';
import MuiExpansionPanel from '@material-ui/core/ExpansionPanel';
import ExpansionPanelDetails from '@material-ui/core/ExpansionPanelDetails';
import ExpansionPanelSummary from '@material-ui/core/ExpansionPanelSummary';
import KeyboardArrowDown from '@material-ui/icons/KeyboardArrowDown';
const pickClassName = prop('className');
/** Prop-type for a recursive data structure */
const tree = {
// The node value.
value: string.isRequired,
/**
* A string representation of the location to link to.
* Only use this property on a leaf node.
* This value will be fed directly to the
* [Link](https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/Link.md)
* component of `react-router-dom`.
* */
href: string,
// Optional node ID. Useful for when the node value is not unique.
id: oneOfType([string, number]),
};
Object.assign(tree, {
nodes: arrayOf(oneOfType([shape(tree), string])),
});
const ExpansionPanel = withStyles({
root: {
'&:before': {
opacity: 0,
},
'&$expanded': {
margin: 0,
},
},
expanded: {},
})(MuiExpansionPanel);
const useStyles = makeStyles(theme => ({
panel: {
width: '100%',
paddingRight: 0,
paddingLeft: 0,
},
panelSummary: {
padding: 0,
paddingRight: theme.spacing(1),
marginLeft: theme.spacing(1),
},
panelDetails: {
padding: 0,
display: 'block',
},
text: {
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'noWrap',
maxWidth: '75vw',
},
listItemTextDense: {
margin: 0,
},
}));
/**
* Render a tree view.
*/
function MuiTreeView(props) {
const theme = useTheme();
const classes = useStyles();
const unit = theme.spacing(1);
const {
tree,
searchTerm,
softSearch,
caseSensitiveSearch,
onEmptySearch,
} = props;
const handleLeafClick = leaf => {
if (props.onLeafClick) {
props.onLeafClick(leaf);
}
};
const handleParentClick = parent => {
if (props.onParentClick) {
props.onParentClick(parent);
}
};
const isLeafNode = node => {
return typeof node === 'string' || !node.nodes || !node.nodes.length;
};
const getNodeValue = node => {
return typeof node === 'string' ? node : node.value;
};
const getNodeId = node => {
if (typeof node === 'object') {
return node.id;
}
};
const getNodeHref = node => {
if (typeof node === 'object') {
return node.href;
}
};
const filter = tree => {
return tree.filter(node => {
const value = getNodeValue(node);
const isLeaf = isLeafNode(node);
const searchRegExp = caseSensitiveSearch
? RegExp(searchTerm)
: RegExp(searchTerm, 'i');
if (searchRegExp.test(value)) {
return true;
}
if (isLeaf) {
return false;
}
const subtree = filter(node.nodes);
return Boolean(subtree.length);
});
};
const createFilteredTree = memoize(
(tree, searchTerm) => (searchTerm ? filter(tree) : tree),
{
serializer: ([tree, searchTerm, softSearch]) =>
`${JSON.stringify(tree)}-${searchTerm}-${softSearch}`,
}
);
const renderNode = ({ node, parent, depth = 0, haltSearch }) => {
const {
searchTerm,
softSearch,
onLeafClick: _,
onParentClick: __,
onEmptySearch: ___,
Link,
expansionPanelSummaryProps,
expansionPanelDetailsProps,
listItemProps,
caseSensitiveSearch,
...rest
} = props;
const value = getNodeValue(node);
const id = getNodeId(node);
const isLeaf = isLeafNode(node);
const href = isLeaf ? getNodeHref(node) : null;
const textIndent = isLeaf
? depth * unit + unit + (parent ? unit : 0)
: unit * depth + unit;
const searchRegExp = caseSensitiveSearch
? RegExp(searchTerm)
: RegExp(searchTerm, 'i');
const shouldHaltSearch =
softSearch && searchTerm ? searchRegExp.test(value) : false;
if (!haltSearch && isLeaf && searchTerm && !searchRegExp.test(value)) {
return null;
}
if (!Link && isLeaf && href) {
throw new Error(
'A Link prop is required when a leaf node has an href specified.'
);
}
if (isLeaf) {
return (
<ListItem
disableGutters
style={{ textIndent }}
key={typeof id !== 'undefined' ? id : value}
id={value}
value={value}
onClick={() => handleLeafClick({ ...node, value, parent, id })}
button
{...(href
? {
component: Link,
to: href,
}
: null)}
{...listItemProps}>
<ListItemText
className={classNames(classes.text, classes.listItemTextDense)}
primary={value}
/>
</ListItem>
);
}
return (
<ExpansionPanel
style={{ textIndent }}
key={node.id || node.value}
elevation={0}
{...rest}
className={classNames(classes.panel, pickClassName(rest))}>
<ExpansionPanelSummary
classes={{ root: classes.panelSummary }}
IconButtonProps={{ edge: 'start' }}
{...expansionPanelSummaryProps}
className={classNames(pickClassName(expansionPanelSummaryProps))}
expandIcon={<KeyboardArrowDown />}
onClick={() => handleParentClick({ ...node, value, parent, id })}>
<Typography className={classes.text}>{node.value}</Typography>
</ExpansionPanelSummary>
<ExpansionPanelDetails
{...expansionPanelDetailsProps}
classes={{ root: classes.panelDetails }}
className={classNames(pickClassName(expansionPanelDetailsProps))}>
{node.nodes.map(l =>
renderNode({
node: l,
parent: node,
depth: depth + 1,
haltSearch: shouldHaltSearch,
})
)}
</ExpansionPanelDetails>
</ExpansionPanel>
);
};
const graph = createFilteredTree(tree, searchTerm, softSearch);
if (!graph.length && onEmptySearch) {
return onEmptySearch;
}
return graph.map(node =>
renderNode({ node, parent: null, haltSearch: false })
);
}
MuiTreeView.propTypes = {
/** The data to render as a tree view */
tree: arrayOf(shape(tree)).isRequired,
/** Callback function fired when a tree leaf is clicked. */
onLeafClick: func,
/** Callback function fired when a tree node is clicked. */
onParentClick: func,
/** A search term to refine the tree */
searchTerm: string,
/**
* Given a `searchTerm`, a subtree will be shown if any parent node
* higher up in the tree matches the search term. Defaults to false.
* */
softSearch: bool,
/** Properties applied to the ExpansionPanelSummary element. */
expansionPanelSummaryProps: object,
/** Properties applied to the ExpansionPanelDetails element. */
expansionPanelDetailsProps: object,
/** Properties applied to the ListItem element. */
listItemProps: object,
/** If true, search is case sensitive. Defaults to false. */
caseSensitiveSearch: bool,
/** Node to render when searchTerm is provided but the search filter
* returns no result. */
onEmptySearch: node,
/**
* A React Router Link node to use. Required when a leaf node
* has an href value.
* */
Link: node,
};
MuiTreeView.defaultProps = {
searchTerm: null,
softSearch: false,
onLeafClick: null,
onParentClick: null,
expansionPanelSummaryProps: null,
expansionPanelDetailsProps: null,
listItemProps: null,
caseSensitiveSearch: false,
onEmptySearch: null,
Link: null,
};
export default MuiTreeView;