feat: tree sorting

This commit is contained in:
cha0s 2022-04-09 19:41:53 -05:00
parent 308c059c85
commit 7d6e9f9743
2 changed files with 36 additions and 22 deletions

View File

@ -31,4 +31,14 @@ Tree.propTypes = {
Tree.displayName = 'Tree';
Tree.fileSort = (l, r) => {
if (l.nodes && !r.nodes) {
return -1;
}
if (!l.nodes && r.nodes) {
return 1;
}
return l.value < r.value ? -1 : 1;
};
export default Tree;

View File

@ -14,31 +14,33 @@ const Node = (props) => {
isCollapsed,
nodes,
renderLabel,
sort,
styles,
value,
} = props;
const children = nodes
? nodes.map(
({
label,
nodes,
styles,
value,
}) => (
<Node
activate={activate}
key={value}
nodes={nodes}
indent={indent + 1}
isActive={isActive}
isCollapsed={isCollapsed}
label={label}
renderLabel={renderLabel}
styles={styles}
value={value}
/>
),
)
? (sort ? nodes.sort(sort) : nodes)
.map(
({
label,
nodes,
styles,
value,
}) => (
<Node
activate={activate}
key={value}
nodes={nodes}
indent={indent + 1}
isActive={isActive}
isCollapsed={isCollapsed}
label={label}
renderLabel={renderLabel}
styles={styles}
value={value}
/>
),
)
: null;
let item = renderLabel({...props, builtinStyles});
for (let i = 0; i < indent; ++i) {
@ -98,6 +100,7 @@ Node.defaultProps = {
{label}
</div>
),
sort: null,
styles: {},
};
@ -109,6 +112,7 @@ const propTypesTree = {
// Enough to fool lint.
nodes: PropTypes.arrayOf(PropTypes.shape({})),
renderLabel: PropTypes.func,
sort: PropTypes.func,
styles: PropTypes.shape({
active: PropTypes.string,
collapsed: PropTypes.string,
@ -120,7 +124,7 @@ const propTypesTree = {
value: PropTypes.string.isRequired,
};
propTypesTree.nodes = PropTypes.arrayOf(propTypesTree);
propTypesTree.nodes = PropTypes.arrayOf(PropTypes.shape(propTypesTree));
Node.propTypes = propTypesTree;