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

Calculate shouldExpandNode on JSONNestedNode.render #61

Merged
merged 1 commit into from
Jan 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/JSONNestedNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ function renderChildNodes(props, from, to) {
return childNodes;
}

function getStateFromProps(props) {
// calculate individual node expansion if necessary
const expanded = props.shouldExpandNode && !props.isCircular ?
props.shouldExpandNode(props.keyPath, props.data, props.level) :
false;
return {
expanded,
createdChildNodes: false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for? Seems like it's not used anywhere.

};
}

export default class JSONNestedNode extends React.Component {
static propTypes = {
getItemString: PropTypes.func.isRequired,
Expand Down Expand Up @@ -88,14 +99,15 @@ export default class JSONNestedNode extends React.Component {

constructor(props) {
super(props);
this.state = getStateFromProps(props);
}

// calculate individual node expansion if necessary
const expanded = props.shouldExpandNode && !props.isCircular ?
props.shouldExpandNode(props.keyPath, props.data, props.level) : false;
this.state = {
expanded,
createdChildNodes: false
};
componentWillReceiveProps(nextProps) {
if (['shouldExpandNode', 'isCircular', 'keyPath', 'data', 'level'].find(k =>
nextProps[k] !== this.props[k])
) {
this.setState(getStateFromProps(nextProps));
}
}

shouldComponentUpdate = shouldPureComponentUpdate;
Expand All @@ -114,7 +126,7 @@ export default class JSONNestedNode extends React.Component {
labelRenderer,
expandable
} = this.props;
const expanded = this.state.expanded;
const { expanded } = this.state;
const renderedChildren = expanded || (hideRoot && this.props.level === 0) ?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to move that to componentWillReceiveProps, like this:

const getStateFromProps = props => ({
  ...  // calculate `expanded`
});

...

constructor(props) {
  super(props);
  this.state = getStateFromProps(props);
}

componentWillReceiveProps(nextProps) {
  if (['shouldExpandNode', 'isCircular', 'keyPath', 'data', 'level'].find(k => nextProps[k] !== this.props[k]) {
    this.setState(getStateFromProps(nextProps));
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, better to keep it DRY. I tested and this is working 👍

renderChildNodes({ ...this.props, level: this.props.level + 1 }) : null;

Expand Down