Skip to content

Commit

Permalink
Set default tab first time an accordion has components
Browse files Browse the repository at this point in the history
  • Loading branch information
erikasv committed Aug 10, 2022
1 parent 92b499a commit 2aedf9f
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/pages/search/Accordion.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,31 @@ class Accordion extends React.Component {
componentDidMount() {
const { componentsArray } = this.props;
if (componentsArray.length > 0) {
let defaultTab = componentsArray.find(
(item) => !item.label.collapsed,
);
if (defaultTab) {
defaultTab = defaultTab.label.id;
} else {
defaultTab = null;
}
this.setState({ expanded: defaultTab });
this.setDefaultTab();
}
}

componentDidUpdate(prevProps) {
const { componentsArray: prevArray } = prevProps;
const { componentsArray } = this.props;
if (prevArray.length === 0 && componentsArray.length > 0) {
this.setDefaultTab();
}
}

setDefaultTab = () => {
const { componentsArray } = this.props;
let defaultTab = componentsArray.find(
(item) => !item.label.collapsed,
);
if (defaultTab) {
defaultTab = defaultTab.label.id;
} else {
defaultTab = null;
}
this.setState({ expanded: defaultTab });
}

render() {
const {
componentsArray,
Expand Down

0 comments on commit 2aedf9f

Please sign in to comment.