Skip to content
This repository was archived by the owner on May 14, 2020. It is now read-only.

Commit c81be49

Browse files
gnestoralexkuz
authored andcommitted
Calculate shouldExpandNode on JSONNestedNode.render (#61)
1 parent a3094fa commit c81be49

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/JSONNestedNode.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ function renderChildNodes(props, from, to) {
5858
return childNodes;
5959
}
6060

61+
function getStateFromProps(props) {
62+
// calculate individual node expansion if necessary
63+
const expanded = props.shouldExpandNode && !props.isCircular ?
64+
props.shouldExpandNode(props.keyPath, props.data, props.level) :
65+
false;
66+
return {
67+
expanded,
68+
createdChildNodes: false
69+
};
70+
}
71+
6172
export default class JSONNestedNode extends React.Component {
6273
static propTypes = {
6374
getItemString: PropTypes.func.isRequired,
@@ -88,14 +99,15 @@ export default class JSONNestedNode extends React.Component {
8899

89100
constructor(props) {
90101
super(props);
102+
this.state = getStateFromProps(props);
103+
}
91104

92-
// calculate individual node expansion if necessary
93-
const expanded = props.shouldExpandNode && !props.isCircular ?
94-
props.shouldExpandNode(props.keyPath, props.data, props.level) : false;
95-
this.state = {
96-
expanded,
97-
createdChildNodes: false
98-
};
105+
componentWillReceiveProps(nextProps) {
106+
if (['shouldExpandNode', 'isCircular', 'keyPath', 'data', 'level'].find(k =>
107+
nextProps[k] !== this.props[k])
108+
) {
109+
this.setState(getStateFromProps(nextProps));
110+
}
99111
}
100112

101113
shouldComponentUpdate = shouldPureComponentUpdate;
@@ -114,7 +126,7 @@ export default class JSONNestedNode extends React.Component {
114126
labelRenderer,
115127
expandable
116128
} = this.props;
117-
const expanded = this.state.expanded;
129+
const { expanded } = this.state;
118130
const renderedChildren = expanded || (hideRoot && this.props.level === 0) ?
119131
renderChildNodes({ ...this.props, level: this.props.level + 1 }) : null;
120132

0 commit comments

Comments
 (0)