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

Conversation

gnestor
Copy link
Contributor

@gnestor gnestor commented Nov 10, 2016

Closes #60

@gnestor gnestor force-pushed the shouldExpandNode-onrender branch from e9fed4a to dbb61b8 Compare November 12, 2016 02:26
@gnestor
Copy link
Contributor Author

gnestor commented Nov 12, 2016

Here's a test component:

import React from 'react';
import JSONTree from 'react-json-tree';

export class JSONFilterableTree extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      filter: ''
    };
  }

  render() {
    let data = this.props.data;
    if (this.state.filter) {
      data = Object.keys(this.props.data).reduce((result, key) => {
        if (JSON.stringify(this.props.data[key]).includes(this.state.filter)) result[key] = this.props.data[key];
        return result;
      }, {});
    }
    return (
      <div
        style={{
          position: 'relative'
        }}
      >
        <input
          value={this.state.filter}
          onChange={(event) => {
            this.setState({filter: event.target.value});
          }}
          style={{
            position: 'absolute',
            top: 0,
            right: 0,
            width: '33%',
            maxWidth: 150,
            zIndex: 10,
            fontSize: 13,
            paddingHorizontal: 4,
            paddingVertical: 2
          }}
          type="text"
          placeholder="Filter..."
        />
        <JSONTree data={data} />
      </div>
    );
  }

}

expanded = this.props.shouldExpandNode && !this.props.isCircular ?
this.props.shouldExpandNode(this.props.keyPath, this.props.data, this.props.level) :
false;
}
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 👍

@gnestor gnestor force-pushed the shouldExpandNode-onrender branch from 924c6b5 to 8668522 Compare November 23, 2016 18:17
@gnestor
Copy link
Contributor Author

gnestor commented Dec 5, 2016

@alexkuz I made the changes you suggested. I think this is ready to merge 👍

@cephirothdy2j
Copy link

+1 on this; I'm waiting on the same fix. Will this be merged and released any time soon?

@alexkuz
Copy link
Owner

alexkuz commented Jan 5, 2017

Yes, I'll take a look soon, sorry for delay.

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.

@alexkuz
Copy link
Owner

alexkuz commented Jan 8, 2017

@gnestor Thank you! Everything is fine, except for that seemingly redundant state value.

@alexkuz
Copy link
Owner

alexkuz commented Jan 8, 2017

Oh, wait, it was there before. Nevermind then :)

@zalmoxisus
Copy link
Collaborator

zalmoxisus commented Feb 16, 2017

Could the breaking changes from this pr be moved under a flag (prop)?

As mentioned in #66, it breaks tree inspection for both Redux DevTools Inspector and Log Monitor:

wbhsmo0vt8

@gnestor
Copy link
Contributor Author

gnestor commented Feb 17, 2017

What if we changed the following:

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

to:

componentWillReceiveProps(nextProps) {
  if (getStateFromProps(nextProps) !== getStateFromProps(this.props)) {
    this.setState(getStateFromProps(nextProps));
  }
}

In its current form, componentWillReceiveProps is over-eager to recalculate expanded state, but this change should correct that (because it appears that the only value that is changing in the above screencap is data). @zalmoxisus Can you test whether this fixes the issue in redux-devtools-inspector? And whether it has any effect on performance?

@zalmoxisus
Copy link
Collaborator

zalmoxisus commented Feb 17, 2017

@gnestor it doesn't help, but if (false) helps 😂

You can clone redux-devtools-inspector and run yarn && yarn start to open the demo.

Not sure if solving that specific issue when an action is selected (as in my screenshot) would be enough. According to #66, users got used that, even when selecting a different action, the state tree remains expanded. If so, we could just add a prop to enable the introduced updates. @alexkuz what do you think? Do we need these updates in Inspector?

@alexkuz
Copy link
Owner

alexkuz commented Feb 19, 2017

@zalmoxisus try v0.10.3 (or RDI v0.11.2), it should work now.

As @gnestor proposed, now it just checks if state.expanded was changed.

@zalmoxisus
Copy link
Collaborator

@alexkuz yes, it works. Thanks a lot for the fix!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants