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 - Update BlockPanel dropdown #435

Merged
merged 1 commit into from
Jul 30, 2018
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
38 changes: 29 additions & 9 deletions src/components/BlockPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Card from './Card';
import CardBody from './CardBody';
import CardHeader from './CardHeader';
import CardTitle from './CardTitle';
import Collapse from './Collapse';
import Icon from './Icon';

class BlockPanel extends React.Component {
Expand Down Expand Up @@ -34,25 +35,37 @@ class BlockPanel extends React.Component {
super(props);

this.state = {
open: props.open
open: props.open,
closed: !props.open
};
}

toggle = () => {
const open = !this.state.open;
this.setState({ open }, () => this.props.onToggle(open));
const newState = open ?
{ open, closed: false } :
{ open };
this.setState(newState, () => this.props.onToggle(open));
};

onClosed = () => {
this.setState({ closed: true });
};

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

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

// TODO simplify - these styles should be default Card, CardHeader styles in theme, not util classes
const headerClassNames = classnames(
Expand Down Expand Up @@ -107,11 +120,18 @@ class BlockPanel extends React.Component {
)}
</div>
</CardHeader>
{children && (!expandable || open || hideOnToggle) ?
<CardBody hidden={expandable && !open && hideOnToggle}>
{children}
</CardBody>
: null}
{children && (
<Collapse
isOpen={children && (!expandable || open)}
onExited={() => this.onClosed()}
>
{(hideOnToggle || !closed) ?
<CardBody>
{children}
</CardBody> :
null}
</Collapse>
)}
</Card>
);
}
Expand Down
20 changes: 10 additions & 10 deletions test/components/BlockPanel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import assert from 'assert';
import { mount, shallow } from 'enzyme';
import sinon from 'sinon';
import { Button, CardBody, CardTitle, Icon, BlockPanel } from '../../src';
import { Button, CardTitle, Collapse, Icon, BlockPanel } from '../../src';

describe('<BlockPanel />', () => {
it('should be empty with no children', () => {
Expand Down Expand Up @@ -40,6 +40,7 @@ describe('<BlockPanel />', () => {
);

assert.equal(component.find('#hi').length, 0);
assert.equal(component.find(Collapse).prop('isOpen'), false, 'inner block should be hidden');
});

it('should be open when true passed as prop', () => {
Expand All @@ -60,12 +61,12 @@ describe('<BlockPanel />', () => {
);

assert.equal(component.find('#hi').length, 1, 'inner block should be visible');
assert.equal(component.find(CardBody).prop('hidden'), false, 'inner block should not be hidden');
assert.equal(component.find(Collapse).prop('isOpen'), true, 'inner block should not be hidden');
component.find(CardTitle).simulate('click');
assert.equal(component.find('#hi').length, 0, 'inner block should not be visible');
assert.equal(component.find(Collapse).prop('isOpen'), false, 'inner block should be hidden');
component.find(Icon).simulate('click');
assert.equal(component.find('#hi').length, 1, 'inner block should be visible');
assert.equal(component.find(CardBody).prop('hidden'), false, 'inner block should not be hidden');
assert.equal(component.find(Collapse).prop('isOpen'), true, 'inner block should not be hidden');
});

it('should honor state of open if state passed as prop', () => {
Expand All @@ -76,10 +77,10 @@ describe('<BlockPanel />', () => {
);

assert.equal(component.find('#hi').length, 1, 'inner block should be visible');

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

assert.equal(component.find('#hi').length, 0);
assert.equal(component.find(Collapse).prop('isOpen'), false, 'inner block should be hidden');
});

it('should show and hide when hideOnToggle is true', () => {
Expand All @@ -90,13 +91,13 @@ describe('<BlockPanel />', () => {
);

assert.equal(component.find('#hi').length, 1, 'inner block should be visible');
assert.equal(component.find(CardBody).prop('hidden'), false, 'inner block should not be hidden');
assert.equal(component.find(Collapse).prop('isOpen'), true, 'inner block should not be hidden');
component.find(CardTitle).simulate('click');
assert.equal(component.find('#hi').length, 1, 'inner block should be present');
assert.equal(component.find(CardBody).prop('hidden'), true, 'inner block should be hidden');
assert.equal(component.find(Collapse).prop('isOpen'), false, 'inner block should be hidden');
component.find(Icon).simulate('click');
assert.equal(component.find('#hi').length, 1, 'inner block should be visible');
assert.equal(component.find(CardBody).prop('hidden'), false, 'inner block should not be hidden');
assert.equal(component.find(Collapse).prop('isOpen'), true, 'inner block should not be hidden');
});

it('should call onToggle when clicked', () => {
Expand Down Expand Up @@ -170,6 +171,5 @@ describe('<BlockPanel />', () => {
assert.equal(component.find('#title').first().text(), 'WE ARE THE CHAMPIONS');
assert.equal(component.find('#action').first().text(), 'Go!');
});

});
});