• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ethanselzer / react-lazy-tree / 82

pending completion
82

Pull #4

circleci

GitHub
Allow react ^17.0.0 as peer dependencies
Pull Request #4: Allow react ^17.0.0 as peer dependencies

97 of 130 branches covered (74.62%)

143 of 143 relevant lines covered (100.0%)

512.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.22
/src/ReactLazyTree.js
1
import React from 'react';
1✔
2
import PropTypes from 'prop-types';
1✔
3
import omit from 'lodash.omit';
1✔
4
import noop from 'lodash.noop';
1✔
5
import TreeNode from './TreeNode';
1✔
6
import { findFirstNode } from './search';
1✔
7

1!
8
class ReactLazyTree extends React.Component {
9
    /* istanbul ignore next */
17!
10
    constructor(props) {
106✔
11
        super(props);
12

13
        const { childrenPropertyName, data, mapInitialActiveNode } = props;
14
        let activePath = '';
106✔
15
        if (typeof mapInitialActiveNode === 'function') {
106✔
16
            activePath = findFirstNode(
18✔
17
                this.normalizeData(data),
18
                mapInitialActiveNode,
19
                childrenPropertyName
20
            );
21
        }
22

23
        this.state = {
106✔
24
            activePath
25
        };
26

27
        this.onActiveNodeChanged = this.onActiveNodeChanged.bind(this);
106✔
28

29
        this.isTouchEnv = false;
106✔
30
    }
31

32
    onActiveNodeChanged(e, node, depth, index, path) {
36✔
33
        const { activePath } = this.state;
15✔
34
        const { interactiveStartDepth } = this.props;
35

106!
36
        if (this.isOnActivePath(path, activePath, depth)) {
36✔
37
            path = this.slicePathAtDepth(path, depth);
11!
38
        }
39

6!
40
        if (depth >= interactiveStartDepth && interactiveStartDepth !== -1) {
36✔
41
            this.setState({
34✔
42
                activePath: path
43
            });
44
        }
45

46
        this.props.onActiveNodeChanged({ e, node, depth, index, activePath: path });
36✔
47
    }
48

106!
49
    isOnActivePath(path, activePath, depth) {
50
        const ap = this.slicePathAtDepth(activePath, depth + 1);
36✔
51
        const p = this.slicePathAtDepth(path, depth + 1);
36✔
52
        return ap === p;
36✔
53
    }
54

55
    slicePathAtDepth(path, depth) {
106✔
56
        return path.split(',').slice(0, depth).join();
83✔
57
    }
58

59
    normalizeData(data) {
60
        if (!Array.isArray(data)) {
160✔
61
            return [data];
148✔
62
        }
63

64
        return data;
12✔
65
    }
66

67
    render() {
68
        const omitions = ['isInteractive', 'mapInitialActiveNode', 'data'];
142✔
69

70
        return (
142✔
71
            <TreeNode {...omit(this.props, omitions)} {...{
72
                activePath: this.state.activePath,
73
                node: this.normalizeData(this.props.data),
74
                onActiveNodeChanged: this.onActiveNodeChanged
75
            }} />
76
        );
36✔
77
    }
78
}
79

80
ReactLazyTree.propTypes = {
1✔
81
    childrenPropertyName: PropTypes.string,
82
    data: PropTypes.oneOfType([PropTypes.object, PropTypes.array]).isRequired,
83
    interactiveStartDepth: PropTypes.number,
84
    mapInitialActiveNode: PropTypes.func,
85
    mapListClassName: PropTypes.func,
86
    mapListItemClassName: PropTypes.func,
87
    mapNodeContent: PropTypes.func.isRequired,
88
    onActiveNodeChanged: PropTypes.func,
89
    shouldLazyRender: PropTypes.bool,
90
    shouldShowAllNodes: PropTypes.bool,
91
    verticalAnimationConfig: PropTypes.shape({
92
        durationInMs: PropTypes.number,
93
        timing: PropTypes.string
94
    })
95
};
96

97
ReactLazyTree.defaultProps = {
1✔
98
    childrenPropertyName: 'children',
99
    interactiveStartDepth: 0,
100
    onActiveNodeChanged: noop
101
};
102

103
export default ReactLazyTree;
160✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc