Skip to content

Commit

Permalink
refactor(expandcollapse): Adjust panel content height when inner cont…
Browse files Browse the repository at this point in the history
…ent changes
  • Loading branch information
ryanoglesby08 committed Nov 6, 2017
1 parent 3c5f4c8 commit 5b2c369
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
47 changes: 45 additions & 2 deletions src/components/ExpandCollapse/ExpandCollapse.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
```
<ExpandCollapse open={["panel-1"]}>
<ExpandCollapse>
<ExpandCollapse.Panel id="panel-1" header="First panel title">
First panel
</ExpandCollapse.Panel>
<ExpandCollapse.Panel id="panel-2" header="Second panel title">
<ExpandCollapse.Panel id="panel-2" header="Second panel title" subtext="Some subtext">
Second panel
</ExpandCollapse.Panel>
</ExpandCollapse>
```

### Arbitrary header component

```
<ExpandCollapse>
<ExpandCollapse.Panel id="panel-10" header={<Text size="large">I'm large and misligned</Text>}>
First panel
</ExpandCollapse.Panel>
</ExpandCollapse>
```


### Adjusting height

```
initialState = {
error: '',
};
const trigger = () => {
if( state.error === '' ) {
setState({error: 'Oh no there was an error'});
}
else {
setState({error: ''});
}
};
<ExpandCollapse open={['panel-21']}>
<ExpandCollapse.Panel id="panel-20" header="First panel title">
First panel
</ExpandCollapse.Panel>
<ExpandCollapse.Panel id="panel-21" header="Second panel title">
<div>
<Paragraph>Second panel blah blah blah</Paragraph>
<Button onClick={trigger}>Trigger</Button>
<Input label="Testing" error={state.error} />
</div>
</ExpandCollapse.Panel>
</ExpandCollapse>
```
21 changes: 16 additions & 5 deletions src/components/ExpandCollapse/PanelWrapper/PanelWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class PanelWrapper extends React.Component {
this.state = {
open: props.open,
hover: false,
contentWrapperHeight: 0,
}
}

Expand All @@ -39,6 +40,20 @@ class PanelWrapper extends React.Component {
}
}

componentDidMount() {
this.adjustContentHeight()
}

componentDidUpdate() {
this.adjustContentHeight()
}

adjustContentHeight() {
if (this.contentWrapper.offsetHeight !== this.state.contentWrapperHeight) {
this.setState({ contentWrapperHeight: this.contentWrapper.offsetHeight })
}
}

mouseEnter = () => {
this.setState({ hover: true })
}
Expand Down Expand Up @@ -93,11 +108,7 @@ class PanelWrapper extends React.Component {
</Box>
</Clickable>

<Reveal
timeout={500}
in={this.state.open}
height={this.contentWrapper ? this.contentWrapper.offsetHeight : 0}
>
<Reveal timeout={500} in={this.state.open} height={this.state.contentWrapperHeight}>
{() => (
<div
ref={contentWrapper => {
Expand Down

0 comments on commit 5b2c369

Please sign in to comment.