@instructure/ui-tree-browser 8.24.2 → 8.24.3-snapshot.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +27 -0
- package/es/TreeBrowser/TreeCollection/index.js +59 -29
- package/es/TreeBrowser/TreeCollection/props.js +2 -1
- package/es/TreeBrowser/index.js +2 -1
- package/lib/TreeBrowser/TreeCollection/index.js +59 -29
- package/lib/TreeBrowser/TreeCollection/props.js +2 -1
- package/lib/TreeBrowser/index.js +2 -1
- package/package.json +16 -15
- package/src/TreeBrowser/README.md +62 -1
- package/src/TreeBrowser/TreeCollection/index.tsx +47 -9
- package/src/TreeBrowser/TreeCollection/props.ts +2 -1
- package/src/TreeBrowser/index.tsx +2 -1
- package/src/TreeBrowser/props.ts +10 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/TreeBrowser/TreeCollection/index.d.ts +4 -2
- package/types/TreeBrowser/TreeCollection/index.d.ts.map +1 -1
- package/types/TreeBrowser/TreeCollection/props.d.ts.map +1 -1
- package/types/TreeBrowser/index.d.ts.map +1 -1
- package/types/TreeBrowser/props.d.ts +10 -1
- package/types/TreeBrowser/props.d.ts.map +1 -1
package/LICENSE.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: The MIT License (MIT)
|
|
3
|
+
category: Getting Started
|
|
4
|
+
order: 9
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# The MIT License (MIT)
|
|
8
|
+
|
|
9
|
+
Copyright (c) 2015 Instructure, Inc.
|
|
10
|
+
|
|
11
|
+
**Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
13
|
+
in the Software without restriction, including without limitation the rights
|
|
14
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
15
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
16
|
+
furnished to do so, subject to the following conditions.**
|
|
17
|
+
|
|
18
|
+
The above copyright notice and this permission notice shall be included in all
|
|
19
|
+
copies or substantial portions of the Software.
|
|
20
|
+
|
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
22
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
23
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
24
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
25
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
26
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
27
|
+
SOFTWARE.
|
|
@@ -133,27 +133,56 @@ let TreeCollection = (_dec = withStyle(generateStyles, generateComponentTheme),
|
|
|
133
133
|
return this.collectionsCount + this.itemsCount + (this.props.renderBeforeItems ? 1 : 0) + (this.props.renderAfterItems ? 1 : 0);
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
|
|
136
|
+
sortChildren() {
|
|
137
137
|
const _this$props6 = this.props,
|
|
138
138
|
collections = _this$props6.collections,
|
|
139
139
|
items = _this$props6.items,
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
140
|
+
compareFunc = _this$props6.compareFunc;
|
|
141
|
+
if (!compareFunc) return [];
|
|
142
|
+
const collections_ = collections ? collections.map(collection => {
|
|
143
|
+
return { ...collection,
|
|
144
|
+
type: 'collection'
|
|
145
|
+
};
|
|
146
|
+
}) : [];
|
|
147
|
+
const items_ = items ? items.map(item => {
|
|
148
|
+
return { ...item,
|
|
149
|
+
type: 'item'
|
|
150
|
+
};
|
|
151
|
+
}) : [];
|
|
152
|
+
const renderQueue = collections_.concat(items_);
|
|
153
|
+
renderQueue.sort(compareFunc);
|
|
154
|
+
return renderQueue;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
renderChildren() {
|
|
158
|
+
const _this$props7 = this.props,
|
|
159
|
+
collections = _this$props7.collections,
|
|
160
|
+
items = _this$props7.items,
|
|
161
|
+
id = _this$props7.id,
|
|
162
|
+
renderBeforeItems = _this$props7.renderBeforeItems,
|
|
163
|
+
renderAfterItems = _this$props7.renderAfterItems,
|
|
164
|
+
compareFunc = _this$props7.compareFunc;
|
|
143
165
|
let position = 1;
|
|
144
|
-
|
|
166
|
+
const sortedChildren = this.sortChildren();
|
|
167
|
+
return jsx(React.Fragment, null, renderBeforeItems && this.renderCollectionChildren(id, renderBeforeItems, position++, 'before'), !compareFunc && collections.map(collection => {
|
|
145
168
|
return this.renderCollectionNode(collection, position++);
|
|
146
|
-
}), items.map(item => {
|
|
169
|
+
}), !compareFunc && items.map(item => {
|
|
147
170
|
return this.renderItemNode(item, position++);
|
|
171
|
+
}), compareFunc && sortedChildren.map(child => {
|
|
172
|
+
if (child.type === 'collection') {
|
|
173
|
+
return this.renderCollectionNode(child, position++);
|
|
174
|
+
} else {
|
|
175
|
+
return this.renderItemNode(child, position++);
|
|
176
|
+
}
|
|
148
177
|
}), renderAfterItems && this.renderCollectionChildren(id, renderAfterItems, position++, 'after'));
|
|
149
178
|
}
|
|
150
179
|
|
|
151
180
|
renderCollectionChildren(collectionId, child, position, keyword) {
|
|
152
|
-
const _this$
|
|
153
|
-
selection = _this$
|
|
154
|
-
onKeyDown = _this$
|
|
155
|
-
getItemProps = _this$
|
|
156
|
-
styles = _this$
|
|
181
|
+
const _this$props8 = this.props,
|
|
182
|
+
selection = _this$props8.selection,
|
|
183
|
+
onKeyDown = _this$props8.onKeyDown,
|
|
184
|
+
getItemProps = _this$props8.getItemProps,
|
|
185
|
+
styles = _this$props8.styles;
|
|
157
186
|
const key = `${collectionId}_${keyword}`;
|
|
158
187
|
const ariaSelected = {};
|
|
159
188
|
|
|
@@ -218,17 +247,18 @@ let TreeCollection = (_dec = withStyle(generateStyles, generateComponentTheme),
|
|
|
218
247
|
renderBeforeItems: collection.renderBeforeItems,
|
|
219
248
|
renderAfterItems: collection.renderAfterItems,
|
|
220
249
|
isCollectionFlattened: false // only the root needs to be flattened
|
|
221
|
-
|
|
250
|
+
,
|
|
251
|
+
compareFunc: collection.compareFunc
|
|
222
252
|
}));
|
|
223
253
|
}
|
|
224
254
|
|
|
225
255
|
renderItemNode(item, position) {
|
|
226
|
-
const _this$
|
|
227
|
-
selection = _this$
|
|
228
|
-
onItemClick = _this$
|
|
229
|
-
onKeyDown = _this$
|
|
230
|
-
getItemProps = _this$
|
|
231
|
-
styles = _this$
|
|
256
|
+
const _this$props9 = this.props,
|
|
257
|
+
selection = _this$props9.selection,
|
|
258
|
+
onItemClick = _this$props9.onItemClick,
|
|
259
|
+
onKeyDown = _this$props9.onKeyDown,
|
|
260
|
+
getItemProps = _this$props9.getItemProps,
|
|
261
|
+
styles = _this$props9.styles;
|
|
232
262
|
const ariaSelected = {};
|
|
233
263
|
|
|
234
264
|
if (selection) {
|
|
@@ -278,17 +308,17 @@ let TreeCollection = (_dec = withStyle(generateStyles, generateComponentTheme),
|
|
|
278
308
|
}
|
|
279
309
|
|
|
280
310
|
render() {
|
|
281
|
-
const _this$
|
|
282
|
-
id = _this$
|
|
283
|
-
name = _this$
|
|
284
|
-
expanded = _this$
|
|
285
|
-
collectionIcon = _this$
|
|
286
|
-
collectionIconExpanded = _this$
|
|
287
|
-
isCollectionFlattened = _this$
|
|
288
|
-
getCollectionProps = _this$
|
|
289
|
-
level = _this$
|
|
290
|
-
position = _this$
|
|
291
|
-
styles = _this$
|
|
311
|
+
const _this$props10 = this.props,
|
|
312
|
+
id = _this$props10.id,
|
|
313
|
+
name = _this$props10.name,
|
|
314
|
+
expanded = _this$props10.expanded,
|
|
315
|
+
collectionIcon = _this$props10.collectionIcon,
|
|
316
|
+
collectionIconExpanded = _this$props10.collectionIconExpanded,
|
|
317
|
+
isCollectionFlattened = _this$props10.isCollectionFlattened,
|
|
318
|
+
getCollectionProps = _this$props10.getCollectionProps,
|
|
319
|
+
level = _this$props10.level,
|
|
320
|
+
position = _this$props10.position,
|
|
321
|
+
styles = _this$props10.styles;
|
|
292
322
|
const collectionProps = getCollectionProps({ ...this.getCommonButtonProps(),
|
|
293
323
|
expanded: expanded,
|
|
294
324
|
collectionIcon: collectionIcon,
|
|
@@ -49,7 +49,8 @@ const propTypes = {
|
|
|
49
49
|
renderAfterItems: Children.oneOf([TreeNode]),
|
|
50
50
|
containerRef: PropTypes.func,
|
|
51
51
|
isCollectionFlattened: PropTypes.bool,
|
|
52
|
-
renderContent: PropTypes.func
|
|
52
|
+
renderContent: PropTypes.func,
|
|
53
|
+
compareFunc: PropTypes.func
|
|
53
54
|
};
|
|
54
55
|
const allowedProps = ['id', 'name', 'descriptor', 'items', 'collections', 'expanded', 'selection', 'size', 'variant', 'collectionIcon', 'collectionIconExpanded', 'itemIcon', 'getItemProps', 'getCollectionProps', 'onItemClick', 'onCollectionClick', 'onKeyDown', 'numChildren', 'level', 'position', 'renderBeforeItems', 'renderAfterItems', 'containerRef', 'isCollectionFlattened', 'renderContent'];
|
|
55
56
|
export { propTypes, allowedProps };
|
package/es/TreeBrowser/index.js
CHANGED
|
@@ -296,7 +296,8 @@ let TreeBrowser = (_dec = withStyle(generateStyles, generateComponentTheme), _de
|
|
|
296
296
|
renderBeforeItems: collection.renderBeforeItems,
|
|
297
297
|
renderAfterItems: collection.renderAfterItems,
|
|
298
298
|
expanded: this.getExpandedIndex(this.expanded, collection.id) >= 0,
|
|
299
|
-
isCollectionFlattened: this.getIsFlattened(collection)
|
|
299
|
+
isCollectionFlattened: this.getIsFlattened(collection),
|
|
300
|
+
compareFunc: collection.compareFunc
|
|
300
301
|
};
|
|
301
302
|
}
|
|
302
303
|
|
|
@@ -123,27 +123,56 @@ let TreeCollection = (_dec = (0, _emotion.withStyle)(_styles.default, _theme.def
|
|
|
123
123
|
return this.collectionsCount + this.itemsCount + (this.props.renderBeforeItems ? 1 : 0) + (this.props.renderAfterItems ? 1 : 0);
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
-
|
|
126
|
+
sortChildren() {
|
|
127
127
|
const _this$props6 = this.props,
|
|
128
128
|
collections = _this$props6.collections,
|
|
129
129
|
items = _this$props6.items,
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
130
|
+
compareFunc = _this$props6.compareFunc;
|
|
131
|
+
if (!compareFunc) return [];
|
|
132
|
+
const collections_ = collections ? collections.map(collection => {
|
|
133
|
+
return { ...collection,
|
|
134
|
+
type: 'collection'
|
|
135
|
+
};
|
|
136
|
+
}) : [];
|
|
137
|
+
const items_ = items ? items.map(item => {
|
|
138
|
+
return { ...item,
|
|
139
|
+
type: 'item'
|
|
140
|
+
};
|
|
141
|
+
}) : [];
|
|
142
|
+
const renderQueue = collections_.concat(items_);
|
|
143
|
+
renderQueue.sort(compareFunc);
|
|
144
|
+
return renderQueue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
renderChildren() {
|
|
148
|
+
const _this$props7 = this.props,
|
|
149
|
+
collections = _this$props7.collections,
|
|
150
|
+
items = _this$props7.items,
|
|
151
|
+
id = _this$props7.id,
|
|
152
|
+
renderBeforeItems = _this$props7.renderBeforeItems,
|
|
153
|
+
renderAfterItems = _this$props7.renderAfterItems,
|
|
154
|
+
compareFunc = _this$props7.compareFunc;
|
|
133
155
|
let position = 1;
|
|
134
|
-
|
|
156
|
+
const sortedChildren = this.sortChildren();
|
|
157
|
+
return (0, _emotion.jsx)(_react.default.Fragment, null, renderBeforeItems && this.renderCollectionChildren(id, renderBeforeItems, position++, 'before'), !compareFunc && collections.map(collection => {
|
|
135
158
|
return this.renderCollectionNode(collection, position++);
|
|
136
|
-
}), items.map(item => {
|
|
159
|
+
}), !compareFunc && items.map(item => {
|
|
137
160
|
return this.renderItemNode(item, position++);
|
|
161
|
+
}), compareFunc && sortedChildren.map(child => {
|
|
162
|
+
if (child.type === 'collection') {
|
|
163
|
+
return this.renderCollectionNode(child, position++);
|
|
164
|
+
} else {
|
|
165
|
+
return this.renderItemNode(child, position++);
|
|
166
|
+
}
|
|
138
167
|
}), renderAfterItems && this.renderCollectionChildren(id, renderAfterItems, position++, 'after'));
|
|
139
168
|
}
|
|
140
169
|
|
|
141
170
|
renderCollectionChildren(collectionId, child, position, keyword) {
|
|
142
|
-
const _this$
|
|
143
|
-
selection = _this$
|
|
144
|
-
onKeyDown = _this$
|
|
145
|
-
getItemProps = _this$
|
|
146
|
-
styles = _this$
|
|
171
|
+
const _this$props8 = this.props,
|
|
172
|
+
selection = _this$props8.selection,
|
|
173
|
+
onKeyDown = _this$props8.onKeyDown,
|
|
174
|
+
getItemProps = _this$props8.getItemProps,
|
|
175
|
+
styles = _this$props8.styles;
|
|
147
176
|
const key = `${collectionId}_${keyword}`;
|
|
148
177
|
const ariaSelected = {};
|
|
149
178
|
|
|
@@ -208,17 +237,18 @@ let TreeCollection = (_dec = (0, _emotion.withStyle)(_styles.default, _theme.def
|
|
|
208
237
|
renderBeforeItems: collection.renderBeforeItems,
|
|
209
238
|
renderAfterItems: collection.renderAfterItems,
|
|
210
239
|
isCollectionFlattened: false // only the root needs to be flattened
|
|
211
|
-
|
|
240
|
+
,
|
|
241
|
+
compareFunc: collection.compareFunc
|
|
212
242
|
}));
|
|
213
243
|
}
|
|
214
244
|
|
|
215
245
|
renderItemNode(item, position) {
|
|
216
|
-
const _this$
|
|
217
|
-
selection = _this$
|
|
218
|
-
onItemClick = _this$
|
|
219
|
-
onKeyDown = _this$
|
|
220
|
-
getItemProps = _this$
|
|
221
|
-
styles = _this$
|
|
246
|
+
const _this$props9 = this.props,
|
|
247
|
+
selection = _this$props9.selection,
|
|
248
|
+
onItemClick = _this$props9.onItemClick,
|
|
249
|
+
onKeyDown = _this$props9.onKeyDown,
|
|
250
|
+
getItemProps = _this$props9.getItemProps,
|
|
251
|
+
styles = _this$props9.styles;
|
|
222
252
|
const ariaSelected = {};
|
|
223
253
|
|
|
224
254
|
if (selection) {
|
|
@@ -268,17 +298,17 @@ let TreeCollection = (_dec = (0, _emotion.withStyle)(_styles.default, _theme.def
|
|
|
268
298
|
}
|
|
269
299
|
|
|
270
300
|
render() {
|
|
271
|
-
const _this$
|
|
272
|
-
id = _this$
|
|
273
|
-
name = _this$
|
|
274
|
-
expanded = _this$
|
|
275
|
-
collectionIcon = _this$
|
|
276
|
-
collectionIconExpanded = _this$
|
|
277
|
-
isCollectionFlattened = _this$
|
|
278
|
-
getCollectionProps = _this$
|
|
279
|
-
level = _this$
|
|
280
|
-
position = _this$
|
|
281
|
-
styles = _this$
|
|
301
|
+
const _this$props10 = this.props,
|
|
302
|
+
id = _this$props10.id,
|
|
303
|
+
name = _this$props10.name,
|
|
304
|
+
expanded = _this$props10.expanded,
|
|
305
|
+
collectionIcon = _this$props10.collectionIcon,
|
|
306
|
+
collectionIconExpanded = _this$props10.collectionIconExpanded,
|
|
307
|
+
isCollectionFlattened = _this$props10.isCollectionFlattened,
|
|
308
|
+
getCollectionProps = _this$props10.getCollectionProps,
|
|
309
|
+
level = _this$props10.level,
|
|
310
|
+
position = _this$props10.position,
|
|
311
|
+
styles = _this$props10.styles;
|
|
282
312
|
const collectionProps = getCollectionProps({ ...this.getCommonButtonProps(),
|
|
283
313
|
expanded: expanded,
|
|
284
314
|
collectionIcon: collectionIcon,
|
|
@@ -61,7 +61,8 @@ const propTypes = {
|
|
|
61
61
|
renderAfterItems: _Children.Children.oneOf([_TreeNode.TreeNode]),
|
|
62
62
|
containerRef: _propTypes.default.func,
|
|
63
63
|
isCollectionFlattened: _propTypes.default.bool,
|
|
64
|
-
renderContent: _propTypes.default.func
|
|
64
|
+
renderContent: _propTypes.default.func,
|
|
65
|
+
compareFunc: _propTypes.default.func
|
|
65
66
|
};
|
|
66
67
|
exports.propTypes = propTypes;
|
|
67
68
|
const allowedProps = ['id', 'name', 'descriptor', 'items', 'collections', 'expanded', 'selection', 'size', 'variant', 'collectionIcon', 'collectionIconExpanded', 'itemIcon', 'getItemProps', 'getCollectionProps', 'onItemClick', 'onCollectionClick', 'onKeyDown', 'numChildren', 'level', 'position', 'renderBeforeItems', 'renderAfterItems', 'containerRef', 'isCollectionFlattened', 'renderContent'];
|
package/lib/TreeBrowser/index.js
CHANGED
|
@@ -295,7 +295,8 @@ let TreeBrowser = (_dec = (0, _emotion.withStyle)(_styles.default, _theme.defaul
|
|
|
295
295
|
renderBeforeItems: collection.renderBeforeItems,
|
|
296
296
|
renderAfterItems: collection.renderAfterItems,
|
|
297
297
|
expanded: this.getExpandedIndex(this.expanded, collection.id) >= 0,
|
|
298
|
-
isCollectionFlattened: this.getIsFlattened(collection)
|
|
298
|
+
isCollectionFlattened: this.getIsFlattened(collection),
|
|
299
|
+
compareFunc: collection.compareFunc
|
|
299
300
|
};
|
|
300
301
|
}
|
|
301
302
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@instructure/ui-tree-browser",
|
|
3
|
-
"version": "8.24.
|
|
3
|
+
"version": "8.24.3-snapshot.6+53afffb68",
|
|
4
4
|
"description": "A component for displaying a hierarchical view of information",
|
|
5
5
|
"author": "Instructure, Inc. Engineering and Product Design",
|
|
6
6
|
"module": "./es/index.js",
|
|
@@ -23,22 +23,22 @@
|
|
|
23
23
|
},
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@instructure/ui-babel-preset": "8.24.
|
|
27
|
-
"@instructure/ui-color-utils": "8.24.
|
|
28
|
-
"@instructure/ui-test-locator": "8.24.
|
|
29
|
-
"@instructure/ui-test-utils": "8.24.
|
|
30
|
-
"@instructure/ui-themes": "8.24.
|
|
26
|
+
"@instructure/ui-babel-preset": "8.24.3-snapshot.6+53afffb68",
|
|
27
|
+
"@instructure/ui-color-utils": "8.24.3-snapshot.6+53afffb68",
|
|
28
|
+
"@instructure/ui-test-locator": "8.24.3-snapshot.6+53afffb68",
|
|
29
|
+
"@instructure/ui-test-utils": "8.24.3-snapshot.6+53afffb68",
|
|
30
|
+
"@instructure/ui-themes": "8.24.3-snapshot.6+53afffb68"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@babel/runtime": "^7.13.10",
|
|
34
|
-
"@instructure/emotion": "8.24.
|
|
35
|
-
"@instructure/shared-types": "8.24.
|
|
36
|
-
"@instructure/ui-icons": "8.24.
|
|
37
|
-
"@instructure/ui-img": "8.24.
|
|
38
|
-
"@instructure/ui-prop-types": "8.24.
|
|
39
|
-
"@instructure/ui-react-utils": "8.24.
|
|
40
|
-
"@instructure/ui-testable": "8.24.
|
|
41
|
-
"@instructure/ui-utils": "8.24.
|
|
34
|
+
"@instructure/emotion": "8.24.3-snapshot.6+53afffb68",
|
|
35
|
+
"@instructure/shared-types": "8.24.3-snapshot.6+53afffb68",
|
|
36
|
+
"@instructure/ui-icons": "8.24.3-snapshot.6+53afffb68",
|
|
37
|
+
"@instructure/ui-img": "8.24.3-snapshot.6+53afffb68",
|
|
38
|
+
"@instructure/ui-prop-types": "8.24.3-snapshot.6+53afffb68",
|
|
39
|
+
"@instructure/ui-react-utils": "8.24.3-snapshot.6+53afffb68",
|
|
40
|
+
"@instructure/ui-testable": "8.24.3-snapshot.6+53afffb68",
|
|
41
|
+
"@instructure/ui-utils": "8.24.3-snapshot.6+53afffb68",
|
|
42
42
|
"keycode": "^2",
|
|
43
43
|
"prop-types": "^15"
|
|
44
44
|
},
|
|
@@ -48,5 +48,6 @@
|
|
|
48
48
|
"publishConfig": {
|
|
49
49
|
"access": "public"
|
|
50
50
|
},
|
|
51
|
-
"sideEffects": false
|
|
51
|
+
"sideEffects": false,
|
|
52
|
+
"gitHead": "53afffb68cdee592b6d98380f2b7559659c2caee"
|
|
52
53
|
}
|
|
@@ -381,7 +381,7 @@ class Example extends React.Component {
|
|
|
381
381
|
render(<Example/>)
|
|
382
382
|
```
|
|
383
383
|
|
|
384
|
-
### Change the order of appearance of items and
|
|
384
|
+
### Change the order of appearance of items and collections
|
|
385
385
|
|
|
386
386
|
By default, the order of collections and items depend on the order of `collections` and `items` array. We can override it by providing a `sortOrder` comparison function.
|
|
387
387
|
|
|
@@ -455,6 +455,67 @@ render(<Example/>)
|
|
|
455
455
|
|
|
456
456
|
```
|
|
457
457
|
|
|
458
|
+
There is another way to sort the children of one collection. By adding the `compareFunc` as the comparison function to the collection's properties. This will be effective only within the collection's scope. For more convenience, we support a prop called `type` to specify whether the collection's children is either an item or a subcollection (this is only make sense in `compareFunc`)
|
|
459
|
+
|
|
460
|
+
```js
|
|
461
|
+
---
|
|
462
|
+
example: true
|
|
463
|
+
render: false
|
|
464
|
+
---
|
|
465
|
+
class Example extends React.Component {
|
|
466
|
+
constructor (props) {
|
|
467
|
+
super(props)
|
|
468
|
+
this.state = {
|
|
469
|
+
size: 'medium'
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
render () {
|
|
473
|
+
return (
|
|
474
|
+
<TreeBrowser
|
|
475
|
+
size={this.state.size}
|
|
476
|
+
collections={{
|
|
477
|
+
1: {
|
|
478
|
+
id: 1,
|
|
479
|
+
name: "Assignments",
|
|
480
|
+
collections: [3,2],
|
|
481
|
+
items: [3],
|
|
482
|
+
descriptor: "Class Assignments",
|
|
483
|
+
// Sort the direct children of "Assignment" by their name in alphabetical order
|
|
484
|
+
compareFunc: (a,b)=>{return a.name.localeCompare(b.name)}
|
|
485
|
+
},
|
|
486
|
+
2: { id: 2, name: "English Assignments", collections: [4], items: [] },
|
|
487
|
+
3: { id: 3, name: "Math Assignments", collections: [5], items: [2,1],
|
|
488
|
+
// The items appear before subcollections
|
|
489
|
+
compareFunc: (a,b)=>{
|
|
490
|
+
if(a.type === "item" && b.type === "collection"){
|
|
491
|
+
return -1
|
|
492
|
+
}
|
|
493
|
+
if(a.type === "collection" && b.type === "item"){
|
|
494
|
+
return 1
|
|
495
|
+
}
|
|
496
|
+
return 0
|
|
497
|
+
}},
|
|
498
|
+
4: { id: 4, name: "Reading Assignments", collections: [], items: [4] },
|
|
499
|
+
5: { id: 5, name: "Advanced Math Assignments", items: [5]}
|
|
500
|
+
}}
|
|
501
|
+
items={{
|
|
502
|
+
1: { id: 1, name: "Addition Worksheet" },
|
|
503
|
+
2: { id: 2, name: "Subtraction Worksheet" },
|
|
504
|
+
3: { id: 3, name: "General Questions" },
|
|
505
|
+
4: { id: 4, name: "Vogon Poetry" },
|
|
506
|
+
5: { id: 5, name: "Bistromath", descriptor: "Explain the Bistromathic Drive" }
|
|
507
|
+
}}
|
|
508
|
+
defaultExpanded={[1, 3]}
|
|
509
|
+
rootId={1}
|
|
510
|
+
/>
|
|
511
|
+
)
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
render(<Example/>)
|
|
516
|
+
|
|
517
|
+
```
|
|
518
|
+
|
|
458
519
|
### showRootCollection
|
|
459
520
|
|
|
460
521
|
The `showRootCollection` prop sets whether the root collection (specified in `rootId` prop) is displayed or to begin with its immediate sub-collections and items instead. It defaults to `true`.
|
|
@@ -36,6 +36,7 @@ import { TreeButton } from '../TreeButton'
|
|
|
36
36
|
import generateStyles from './styles'
|
|
37
37
|
import generateComponentTheme from './theme'
|
|
38
38
|
import type { TreeBrowserCollectionProps, TreeCollectionState } from './props'
|
|
39
|
+
import type { CompareObject } from '../props'
|
|
39
40
|
import { allowedProps, propTypes } from './props'
|
|
40
41
|
import { CollectionItem, CollectionProps, CollectionData } from '../props'
|
|
41
42
|
|
|
@@ -142,12 +143,35 @@ class TreeCollection extends Component<
|
|
|
142
143
|
(this.props.renderAfterItems ? 1 : 0)
|
|
143
144
|
)
|
|
144
145
|
}
|
|
145
|
-
|
|
146
|
+
sortChildren() {
|
|
147
|
+
const { collections, items, compareFunc } = this.props
|
|
148
|
+
if (!compareFunc) return []
|
|
149
|
+
const collections_: CompareObject[] = collections
|
|
150
|
+
? collections.map((collection) => {
|
|
151
|
+
return { ...collection, type: 'collection' }
|
|
152
|
+
})
|
|
153
|
+
: []
|
|
154
|
+
const items_: CompareObject[] = items
|
|
155
|
+
? items.map((item) => {
|
|
156
|
+
return { ...item, type: 'item' }
|
|
157
|
+
})
|
|
158
|
+
: []
|
|
159
|
+
const renderQueue = collections_.concat(items_)
|
|
160
|
+
renderQueue.sort(compareFunc)
|
|
161
|
+
return renderQueue
|
|
162
|
+
}
|
|
146
163
|
renderChildren() {
|
|
147
|
-
const {
|
|
148
|
-
|
|
164
|
+
const {
|
|
165
|
+
collections,
|
|
166
|
+
items,
|
|
167
|
+
id,
|
|
168
|
+
renderBeforeItems,
|
|
169
|
+
renderAfterItems,
|
|
170
|
+
compareFunc
|
|
171
|
+
} = this.props
|
|
149
172
|
|
|
150
173
|
let position = 1
|
|
174
|
+
const sortedChildren = this.sortChildren()
|
|
151
175
|
return (
|
|
152
176
|
<>
|
|
153
177
|
{renderBeforeItems &&
|
|
@@ -157,12 +181,25 @@ class TreeCollection extends Component<
|
|
|
157
181
|
position++,
|
|
158
182
|
'before'
|
|
159
183
|
)}
|
|
160
|
-
{
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
184
|
+
{!compareFunc &&
|
|
185
|
+
collections!.map((collection) => {
|
|
186
|
+
return this.renderCollectionNode(collection, position++)
|
|
187
|
+
})}
|
|
188
|
+
{!compareFunc &&
|
|
189
|
+
items!.map((item) => {
|
|
190
|
+
return this.renderItemNode(item, position++)
|
|
191
|
+
})}
|
|
192
|
+
{compareFunc &&
|
|
193
|
+
sortedChildren.map((child) => {
|
|
194
|
+
if (child.type === 'collection') {
|
|
195
|
+
return this.renderCollectionNode(
|
|
196
|
+
child as CollectionProps,
|
|
197
|
+
position++
|
|
198
|
+
)
|
|
199
|
+
} else {
|
|
200
|
+
return this.renderItemNode(child as CollectionItem, position++)
|
|
201
|
+
}
|
|
202
|
+
})}
|
|
166
203
|
{renderAfterItems &&
|
|
167
204
|
this.renderCollectionChildren(
|
|
168
205
|
id,
|
|
@@ -249,6 +286,7 @@ class TreeCollection extends Component<
|
|
|
249
286
|
renderBeforeItems={collection.renderBeforeItems}
|
|
250
287
|
renderAfterItems={collection.renderAfterItems}
|
|
251
288
|
isCollectionFlattened={false} // only the root needs to be flattened
|
|
289
|
+
compareFunc={collection.compareFunc}
|
|
252
290
|
/>
|
|
253
291
|
)
|
|
254
292
|
}
|
|
@@ -84,7 +84,8 @@ const propTypes: PropValidators<PropKeys> = {
|
|
|
84
84
|
renderAfterItems: Children.oneOf([TreeNode]),
|
|
85
85
|
containerRef: PropTypes.func,
|
|
86
86
|
isCollectionFlattened: PropTypes.bool,
|
|
87
|
-
renderContent: PropTypes.func
|
|
87
|
+
renderContent: PropTypes.func,
|
|
88
|
+
compareFunc: PropTypes.func
|
|
88
89
|
}
|
|
89
90
|
|
|
90
91
|
const allowedProps: AllowedPropKeys = [
|
|
@@ -327,7 +327,8 @@ class TreeBrowser extends Component<TreeBrowserProps, TreeBrowserState> {
|
|
|
327
327
|
renderBeforeItems: collection.renderBeforeItems,
|
|
328
328
|
renderAfterItems: collection.renderAfterItems,
|
|
329
329
|
expanded: this.getExpandedIndex(this.expanded, collection.id) >= 0,
|
|
330
|
-
isCollectionFlattened: this.getIsFlattened(collection)
|
|
330
|
+
isCollectionFlattened: this.getIsFlattened(collection),
|
|
331
|
+
compareFunc: collection.compareFunc
|
|
331
332
|
}
|
|
332
333
|
}
|
|
333
334
|
|
package/src/TreeBrowser/props.ts
CHANGED
|
@@ -148,6 +148,7 @@ type CollectionBase = {
|
|
|
148
148
|
type Collection = CollectionBase & {
|
|
149
149
|
items?: number[]
|
|
150
150
|
collections?: (number | string)[]
|
|
151
|
+
compareFunc?: (a: CompareObject, b: CompareObject) => number
|
|
151
152
|
}
|
|
152
153
|
|
|
153
154
|
type CollectionItem = {
|
|
@@ -163,8 +164,14 @@ type CollectionProps = {
|
|
|
163
164
|
items?: CollectionItem[]
|
|
164
165
|
expanded?: boolean
|
|
165
166
|
isCollectionFlattened?: boolean
|
|
167
|
+
compareFunc?: (a: CompareObject, b: CompareObject) => number
|
|
166
168
|
} & CollectionBase
|
|
167
169
|
|
|
170
|
+
type CompareCollection = CollectionProps & { type: 'collection' }
|
|
171
|
+
type CompareItem = CollectionItem & { type: 'item' }
|
|
172
|
+
// this is a sum type, so CompareObject is one of CompareCollection OR CompareItem
|
|
173
|
+
type CompareObject = CompareItem | CompareCollection
|
|
174
|
+
|
|
168
175
|
type CollectionData = {
|
|
169
176
|
id?: number | string
|
|
170
177
|
expanded?: boolean
|
|
@@ -236,6 +243,9 @@ export type {
|
|
|
236
243
|
CollectionData,
|
|
237
244
|
Collection,
|
|
238
245
|
CollectionItem,
|
|
246
|
+
CompareCollection,
|
|
247
|
+
CompareItem,
|
|
248
|
+
CompareObject,
|
|
239
249
|
CollectionProps,
|
|
240
250
|
TreeBrowserBaseProps,
|
|
241
251
|
TreeBrowserCommonProps
|