-
Notifications
You must be signed in to change notification settings - Fork 115
Calculate shouldExpandNode on JSONNestedNode.render #61
Conversation
e9fed4a
to
dbb61b8
Compare
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) ? |
There was a problem hiding this comment.
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));
}
}
There was a problem hiding this comment.
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 👍
dbb61b8
to
924c6b5
Compare
924c6b5
to
8668522
Compare
@alexkuz I made the changes you suggested. I think this is ready to merge 👍 |
+1 on this; I'm waiting on the same fix. Will this be merged and released any time soon? |
Yes, I'll take a look soon, sorry for delay. |
false; | ||
return { | ||
expanded, | ||
createdChildNodes: false |
There was a problem hiding this comment.
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.
@gnestor Thank you! Everything is fine, except for that seemingly redundant state value. |
Oh, wait, it was there before. Nevermind then :) |
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: |
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, |
@gnestor it doesn't help, but You can clone 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? |
@zalmoxisus try As @gnestor proposed, now it just checks if |
@alexkuz yes, it works. Thanks a lot for the fix! |
Closes #60