Skip to content

Commit

Permalink
Add "collapsible" HOC and field config "collapse"
Browse files Browse the repository at this point in the history
  • Loading branch information
Benaiah committed Jul 5, 2017
1 parent 144cabe commit e515b39
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/components/Widgets/ControlHOC.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component, PropTypes } from 'react';
import ImmutablePropTypes from "react-immutable-proptypes";
import repeatable from "./RepeatableHOC";
import ImmutablePropTypes from 'react-immutable-proptypes';
import { applyHOCs } from './WidgetHOCs';

const truthy = () => ({ error: false });

Expand Down Expand Up @@ -86,9 +86,7 @@ class ControlHOC extends Component {
};

render() {
const ControlComponent = this.props.field.get('repeat', false)
? repeatable(this.props.controlComponent)
: this.props.controlComponent;
const ControlComponent = applyHOCs(this.props.controlComponent);

return (<ControlComponent
{...this.props}
Expand Down
20 changes: 20 additions & 0 deletions src/components/Widgets/WidgetHOCs/collapsible.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@import "../../UI/theme";

.toggleButton {
cursor: pointer;
display: flex;
align-items: center;
height: 28px;
border: none;
border-radius: var(--borderRadius) var(--borderRadius) 0 0;
background: rgba(0,0,0,0.1);
}

.summary {
border: 2px solid rgba(0,0,0,0.1);
border-top-width: 28px;
border-radius: var(--borderRadius);
border-top-left-radius: 0;
margin-bottom: 20px;
padding: 20px;
}
40 changes: 40 additions & 0 deletions src/components/Widgets/WidgetHOCs/collapsible.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { Component } from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes';
import FontIcon from 'react-toolbox/lib/font_icon';

const collapsible = WrappedComponent =>
class extends Component {
static propTypes = {
field: ImmutablePropTypes.map.isRequired,
};

constructor(props) {
super(props);
this.state = { collapsed: this.props.field.getIn(['collapse', 'startCollapsed'], false) };
}

handleToggle = () => {
this.setState(Object.assign({}, this.state, { collapsed: !this.state.collapsed }));
}

getSummary() {
return "SUMMARY";
}

render() {
if (this.props.field.get('collapse')) {
return (<div>
<button onClick={this.handleToggle}>
<FontIcon value={this.state.collapsed ? 'expand_more' : 'expand_less'} />
</button>
{this.state.collapsed
? <div>SUMMARY</div>
: <WrappedComponent {...this.props} />}
</div>);
}

return <WrappedComponent {...this.props} />;
}
};

export default collapsible;

0 comments on commit e515b39

Please sign in to comment.