Skip to content

Commit

Permalink
Merge pull request #541 from appfolio/controlledExpandableOpen
Browse files Browse the repository at this point in the history
gt - Add support for controlled open prop
  • Loading branch information
gthomas-appfolio authored May 16, 2019
2 parents 736afb5 + b4df23b commit 38496ef
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/components/ExpandableSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ class ExpandableSection extends React.Component {

toggle = () => this.setState({ open: !this.state.open });

componentWillReceiveProps(nextProps) {
if (nextProps.open !== this.props.open) {
if (nextProps.open) {
this.setState({ open: true });
} else {
this.setState({ open: false });
}
}
}

render() {
return (
<section className={this.props.className}>
Expand Down
9 changes: 6 additions & 3 deletions stories/ExpandableSection.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import React from 'react';
import { storiesOf } from '@storybook/react';
import { text } from '@storybook/addon-knobs';
import { boolean, text } from '@storybook/addon-knobs';

import { ExpandableSection } from '../src';

storiesOf('ExpandableSection', module)
.addWithInfo('Default', () => (
<ExpandableSection title={text('title', 'Click to expand me')}>
<ExpandableSection
title={text('title', 'Click to expand me')}
open={boolean('open', ExpandableSection.defaultProps.open)}
>
<h2>BOO!</h2>
</ExpandableSection>
))
.addWithInfo('Open', () => (
<ExpandableSection
title={text('title', 'Expanded by default')}
open
open={boolean('open', true)}
>
<h2>BOO!</h2>
</ExpandableSection>
Expand Down
12 changes: 12 additions & 0 deletions test/components/ExpandableSection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@ describe('<ExpandableSection />', () => {
component.find('header').simulate('click');
assert.equal(component.find(Collapse).prop('isOpen'), true, 'inner block should be visible');
});

it('should be open when prop changed', () => {
const component = mount(
<ExpandableSection title="Open">
<h1>Hello World!</h1>
</ExpandableSection>
);

assert.equal(component.find(Collapse).prop('isOpen'), false, 'inner block should be hidden');
component.setProps({ open: true });
assert.equal(component.find(Collapse).prop('isOpen'), true, 'inner block should be visible');
});
});

0 comments on commit 38496ef

Please sign in to comment.