Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gt - Add onToggle to BlockPanel #219

Merged
merged 1 commit into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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