Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from aspecto-io/fix/arrayGroup
Browse files Browse the repository at this point in the history
bugfix: incorrect ArrayGroup opened
  • Loading branch information
Yaniv Davidi authored Jul 19, 2021
2 parents 7e84217 + 9b881db commit 9a33391
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "searchable-react-json-view",
"description": "Interactive react component for displaying javascript arrays and JSON objects.",
"version": "0.0.7",
"version": "0.0.8",
"main": "dist/main.js",
"dependencies": {
"flux": "^3.1.3",
Expand Down
43 changes: 36 additions & 7 deletions src/js/components/ArrayGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,47 @@ export default class extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
expanded: []
expanded: {}
};
}

toggleCollapsed = (i) => {
const newExpanded = [];
for (const j in this.state.expanded) {
newExpanded.push(this.state.expanded[j]);
componentDidMount() {
this.updateExpandedGroups();
}

componentDidUpdate(prevProps) {
if (prevProps.highlightSearch !== this.props.highlightSearch) {
this.updateExpandedGroups();
}
}


updateExpandedGroups = () => {
const size = this.props.groupArraysAfterLength;
const groups = Math.ceil(this.props.src.length / size);
const expandedGroups = {};
for (let i in [...Array(groups)]) {
const groupSrc = this.props.src.slice(i * size, i * size + size)
const groupContainingSearch = this.props.highlightSearch && JSON.stringify(groupSrc).toLowerCase().includes(this.props.highlightSearch.toLowerCase());
if (groupContainingSearch) {
expandedGroups[i] = true;
}
}
newExpanded[i] = !newExpanded[i];

this.setState({
...this.state,
expanded: expandedGroups
});
}

toggleCollapsed = (i) => {
const groupState = !this.state.expanded[i];
this.setState({
expanded: newExpanded
...this.state,
expanded: {
...this.state.expanded,
[i]: groupState,
}
});
}

Expand Down

0 comments on commit 9a33391

Please sign in to comment.