wix-style-react
Version:
176 lines (150 loc) • 6.79 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import React from 'react';
import PropTypes from 'prop-types';
import SelectableAccordionItem from './Item';
import { WixStyleReactContext } from '../WixStyleReactProvider/context';
import { st, classes } from './SelectableAccordion.st.css';
import { VERTICAL_PADDING } from './constants';
/** SelectableAccordion */
var SelectableAccordion = /*#__PURE__*/function (_React$PureComponent) {
_inherits(SelectableAccordion, _React$PureComponent);
var _super = _createSuper(SelectableAccordion);
function SelectableAccordion() {
var _this;
_classCallCheck(this, SelectableAccordion);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "state", {
openIndices: _this._populateInitiallyOpenIndices()
});
_defineProperty(_assertThisInitialized(_this), "_onItemChanged", function (idx, open) {
var _this$props = _this.props,
type = _this$props.type,
onSelectionChanged = _this$props.onSelectionChanged;
var openIndices;
if (open) {
if (type === 'radio') {
openIndices = [idx];
} else {
openIndices = [].concat(_toConsumableArray(_this.state.openIndices), [idx]);
}
} else {
openIndices = _toConsumableArray(_this.state.openIndices).filter(function (openIndex) {
return idx !== openIndex;
});
}
_this.setState({
openIndices: openIndices
}, function () {
return onSelectionChanged && onSelectionChanged(openIndices);
});
});
return _this;
}
_createClass(SelectableAccordion, [{
key: "_populateInitiallyOpenIndices",
value: function _populateInitiallyOpenIndices() {
var items = this.props.items;
var controlled = items.some(function (item) {
return item.open;
});
return items.map(function (item, index) {
return item[controlled ? 'open' : 'initiallyOpen'] ? index : null;
}).filter(function (index) {
return index !== null;
});
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props2 = this.props,
dataHook = _this$props2.dataHook,
className = _this$props2.className,
items = _this$props2.items,
verticalPadding = _this$props2.verticalPadding,
type = _this$props2.type;
var openIndices = this.state.openIndices;
return /*#__PURE__*/React.createElement(WixStyleReactContext.Consumer, null, function (_ref) {
var reducedSpacingAndImprovedLayout = _ref.reducedSpacingAndImprovedLayout;
var defaultVerticalPadding = reducedSpacingAndImprovedLayout ? VERTICAL_PADDING.small : VERTICAL_PADDING.medium;
return /*#__PURE__*/React.createElement("div", {
className: st(classes.root, className),
"data-hook": dataHook
}, items.map(function (item, idx) {
return /*#__PURE__*/React.createElement(SelectableAccordionItem, _extends({
key: idx,
idx: idx,
type: type,
verticalPadding: verticalPadding || defaultVerticalPadding,
onChange: _this2._onItemChanged
}, item, {
open: openIndices.includes(idx)
}));
}));
});
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(_ref2, state) {
var items = _ref2.items;
var controlled = items.some(function (item) {
return item.open;
});
return controlled ? {
openIndices: items.map(function (item, index) {
return item.open ? index : null;
}).filter(function (index) {
return index !== null;
})
} : state;
}
}]);
return SelectableAccordion;
}(React.PureComponent);
_defineProperty(SelectableAccordion, "propTypes", {
/** Applied as data-hook HTML attribute that can be used in the tests */
dataHook: PropTypes.string,
/** A css class to be applied to the component's root element */
className: PropTypes.string,
/** A type can be ether radio, checkbox, or toggle, which will effect the way an accordion item is selected */
type: PropTypes.oneOf(['radio', 'checkbox', 'toggle']),
/** An array of Accordion items:
* - `title`: A title of the item
* - `subtitle`: An optional second row of the header
* - `content`: A content of the item
* - `initiallyOpen`: Whether the item is initially open
* - `open`: Whether the item is open
* - `disabled`: Whether the item is disabled
* */
items: PropTypes.arrayOf(PropTypes.shape({
title: PropTypes.node,
subtitle: PropTypes.node,
content: PropTypes.node,
initiallyOpen: PropTypes.bool,
open: PropTypes.bool,
disabled: PropTypes.bool
})),
/** Extra space on top and bottom of selectable accordion item */
verticalPadding: PropTypes.oneOf(['medium', 'small', 'tiny']),
/** A callback which is invoked every time the selection is changed */
onSelectionChanged: PropTypes.func
});
_defineProperty(SelectableAccordion, "defaultProps", {
type: 'radio',
items: []
});
_defineProperty(SelectableAccordion, "displayName", 'SelectableAccordion');
export default SelectableAccordion;