@ryami333/react-accessible-accordion
Version:
Accessible Accordion component for React
139 lines (111 loc) • 5.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
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; }
var defaultProps = {
accordion: true,
onChange: function onChange() {},
className: 'accordion'
};
var propTypes = {
accordion: _propTypes2.default.bool,
children: _propTypes2.default.oneOfType([_propTypes2.default.arrayOf(_propTypes2.default.element), _propTypes2.default.object]).isRequired,
className: _propTypes2.default.string,
onChange: _propTypes2.default.func
};
var Accordion = function (_Component) {
_inherits(Accordion, _Component);
function Accordion(props) {
_classCallCheck(this, Accordion);
var _this = _possibleConstructorReturn(this, (Accordion.__proto__ || Object.getPrototypeOf(Accordion)).call(this, props));
var activeItems = _this.preExpandedItems();
_this.state = {
activeItems: activeItems,
accordion: true
};
_this.renderItems = _this.renderItems.bind(_this);
return _this;
}
_createClass(Accordion, [{
key: 'preExpandedItems',
value: function preExpandedItems() {
var _this2 = this;
var activeItems = [];
_react2.default.Children.map(this.props.children, function (item, index) {
if (item.props.expanded) {
if (_this2.props.accordion) {
if (activeItems.length === 0) activeItems.push(index);
} else {
activeItems.push(index);
}
}
});
return activeItems;
}
}, {
key: 'handleClick',
value: function handleClick(key) {
var activeItems = this.state.activeItems;
if (this.props.accordion) {
activeItems = activeItems[0] === key ? [] : [key];
} else {
activeItems = [].concat(_toConsumableArray(activeItems));
var index = activeItems.indexOf(key);
var isActive = index > -1;
if (isActive) {
// remove active state
activeItems.splice(index, 1);
} else {
activeItems.push(key);
}
}
this.setState({
activeItems: activeItems
});
this.props.onChange(this.props.accordion ? activeItems[0] : activeItems);
}
}, {
key: 'renderItems',
value: function renderItems() {
var _this3 = this;
var _props = this.props,
accordion = _props.accordion,
children = _props.children;
return _react2.default.Children.map(children, function (item, index) {
var key = index;
var expanded = _this3.state.activeItems.indexOf(key) !== -1 && !item.props.disabled;
return _react2.default.cloneElement(item, {
disabled: item.props.disabled,
accordion: accordion,
expanded: expanded,
key: 'accordion__item-' + key,
onClick: _this3.handleClick.bind(_this3, key)
});
});
}
}, {
key: 'render',
value: function render() {
var className = this.props.className;
return _react2.default.createElement(
'div',
{ className: className },
this.renderItems()
);
}
}]);
return Accordion;
}(_react.Component);
Accordion.propTypes = propTypes;
Accordion.defaultProps = defaultProps;
exports.default = Accordion;