wix-style-react
Version:
wix-style-react
93 lines • 3.98 kB
JavaScript
import React from 'react';
import PropTypes from 'prop-types';
import SelectableAccordionItem from './Item';
import { st, classes } from './SelectableAccordion.st.css';
import { VERTICAL_PADDING } from './constants';
/** SelectableAccordion */
class SelectableAccordion extends React.PureComponent {
constructor() {
super(...arguments);
this.state = {
openIndices: this._populateInitiallyOpenIndices(),
};
this._onItemChanged = (idx, open) => {
const { type, onSelectionChanged } = this.props;
let openIndices;
if (open) {
if (type === 'radio') {
openIndices = [idx];
}
else {
openIndices = [...this.state.openIndices, idx];
}
}
else {
openIndices = [...this.state.openIndices].filter(openIndex => idx !== openIndex);
}
this.setState({ openIndices }, () => onSelectionChanged && onSelectionChanged(openIndices));
};
}
static getDerivedStateFromProps({ items }, state) {
const controlled = items.some(item => item.open);
return controlled
? {
openIndices: items
.map((item, index) => (item.open ? index : null))
.filter(index => index !== null),
}
: state;
}
_populateInitiallyOpenIndices() {
const { items } = this.props;
const controlled = items.some(item => item.open);
return items
.map((item, index) => item[controlled ? 'open' : 'initiallyOpen'] ? index : null)
.filter(index => index !== null);
}
render() {
const { dataHook, className, items, verticalPadding, type } = this.props;
const { openIndices } = this.state;
return (React.createElement("div", { className: st(classes.root, className), "data-hook": dataHook }, items.map((item, idx) => (React.createElement(SelectableAccordionItem, { key: idx, idx: idx, type: type, verticalPadding: verticalPadding, onChange: (e, index, open) => {
this._onItemChanged(index, open);
item.onClick && item.onClick(e, { open });
}, ...item, open: openIndices.includes(idx) })))));
}
}
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
* - `onClick`: A callback which is invoked when item's selection is changed
* */
items: PropTypes.arrayOf(PropTypes.shape({
title: PropTypes.node,
subtitle: PropTypes.node,
content: PropTypes.node,
initiallyOpen: PropTypes.bool,
open: PropTypes.bool,
disabled: PropTypes.bool,
onClick: PropTypes.func,
})),
/** 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,
};
SelectableAccordion.defaultProps = {
type: 'radio',
items: [],
verticalPadding: VERTICAL_PADDING.small,
};
SelectableAccordion.displayName = 'SelectableAccordion';
export default SelectableAccordion;
//# sourceMappingURL=SelectableAccordion.js.map