react-accordion-with-header
Version:
React accordion component with flexbox header
103 lines (85 loc) • 4.3 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { getHorizontalAlignment, getVerticalAlignment } from './utils';
var AccordionHeader = function (_PureComponent) {
_inherits(AccordionHeader, _PureComponent);
function AccordionHeader() {
var _temp, _this, _ret;
_classCallCheck(this, AccordionHeader);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _PureComponent.call.apply(_PureComponent, [this].concat(args))), _this), _this.renderChildren = function () {
if (!_this.props.template && !_this.props.children && !_this.props.title) {
throw new Error('AccordionHeader must have a title or template or at least one child!');
}
if (_this.props.title) {
return React.createElement(
'div',
null,
React.createElement(
'h1',
null,
_this.props.title
)
);
}
if (_this.props.template) {
return _this.props.template;
}
return _this.props.children;
}, _temp), _possibleConstructorReturn(_this, _ret);
}
AccordionHeader.prototype.render = function render() {
var _styles;
var _props = this.props,
style = _props.style,
titleColor = _props.titleColor,
verticalAlignment = _props.verticalAlignment,
horizontalAlignment = _props.horizontalAlignment,
className = _props.className,
isExpanded = _props.isExpanded,
onClickHeader = _props.onClickHeader;
var styles = (_styles = {
cursor: 'pointer',
color: titleColor || 'black',
display: '-webkit-flex'
}, _styles['display'] = 'flex', _styles.flexDirection = 'row', _styles.alignItems = getVerticalAlignment(verticalAlignment), _styles.justifyContent = getHorizontalAlignment(horizontalAlignment), _styles);
return React.createElement(
'div',
{
className: classNames(className, { 'header-is-expanded': isExpanded }),
onClick: onClickHeader,
style: _extends({}, style, styles)
},
this.renderChildren()
);
};
return AccordionHeader;
}(PureComponent);
export { AccordionHeader as default };
AccordionHeader.propTypes = process.env.NODE_ENV !== "production" ? {
className: PropTypes.string,
style: PropTypes.object,
verticalAlignment: PropTypes.oneOf(['top', 'center', 'bottom']),
horizontalAlignment: PropTypes.oneOf(['spaceAround', 'spaceBetween', 'spaceEvenly', 'stretch', 'centerSpaceBetween', 'centerSpaceAround', 'center', 'left', 'right']),
title: PropTypes.string,
titleColor: PropTypes.string,
template: PropTypes.element,
onClickHeader: PropTypes.func
} : {};
AccordionHeader.defaultProps = {
horizontalAlignment: 'centerSpaceAround',
verticalAlignment: 'center',
titleColor: 'black',
style: {
padding: 10,
boxShadow: '0 0 0 1px rgba(63,63,68,.05), 1px 1px 3px 0 rgba(63,63,68,.15)',
borderRadius: 3
}
};