Skip to content

Commit

Permalink
Merge pull request #219 from appfolio/addBlockPanelOnToggle
Browse files Browse the repository at this point in the history
gt - Add onToggle to BlockPanel
  • Loading branch information
gthomas-appfolio authored May 18, 2017
2 parents 7ba2b52 + 2579cc8 commit 8af991d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/components/BlockPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ class BlockPanel extends Component {
className: React.PropTypes.string,
expandable: React.PropTypes.bool,
onEdit: React.PropTypes.func,
onToggle: React.PropTypes.func,
title: React.PropTypes.string.isRequired
};

static defaultProps = {
className: '',
open: true,
expandable: false
expandable: false,
onToggle: () => {}
};

constructor(props) {
Expand All @@ -26,10 +28,15 @@ class BlockPanel extends Component {
};
}

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

render() {
const { children, className, controls, expandable, title, onEdit, ...props } = this.props;
// eslint-disable-next-line no-unused-vars
const { children, className, controls, expandable, title, onEdit, onToggle, ...props } = this.props;
const { open } = this.state;

return (
Expand Down
11 changes: 10 additions & 1 deletion stories/BlockPanel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { storiesOf } from '@kadira/storybook';
import { action, storiesOf } from '@kadira/storybook';

import { BlockPanel, Button, Icon, HelpBubble } from '../src';
import { boolean, text } from '@kadira/storybook-addon-knobs';
Expand All @@ -24,6 +24,15 @@ storiesOf('BlockPanel', module)
Now you don't.
</BlockPanel>
))
.addWithInfo('onToggle', () => (
<BlockPanel
title={text('title', 'Click me you fool')}
onToggle={action('onToggle')}
expandable={boolean('expandable', true)}
>
Now you don't.
</BlockPanel>
))
.addWithInfo('components for title and controls', () => (
<BlockPanel
title={
Expand Down
14 changes: 14 additions & 0 deletions test/components/BlockPanel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ describe('<BlockPanel />', () => {
component.find(Icon).simulate('click');
assert.equal(component.find('#hi').length, 1, 'inner block should be visible');
});

it('should call onToggle when clicked', () => {
const onToggle = sinon.spy();

const component = mount(
<BlockPanel title="Open" expandable onToggle={onToggle}>
<h1 id="hi">Hello World!</h1>
</BlockPanel>
);
component.find(CardTitle).simulate('click');
assert.equal(onToggle.calledWith(false), true);
component.find(CardTitle).simulate('click');
assert.equal(onToggle.calledWith(true), true);
});
});

context('contains headerComponent', () => {
Expand Down

0 comments on commit 8af991d

Please sign in to comment.