@retailmenot/anchor
Version:
A React UI Library by RetailMeNot
44 lines • 2.58 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
// REACT
import * as React from 'react';
// VENDOR
import classNames from 'classnames';
import styled from '@xstyled/styled-components';
// Currently no styles but following the design pattern of anchor components where top-level components
// are styled components.
const StyledCollapseGroup = styled('div') ``;
StyledCollapseGroup.displayName = 'StyledCollapseGroup';
export const CollapseGroup = (_a) => {
var { accordion = false, openIndex = 0, children, className } = _a, props = __rest(_a, ["accordion", "openIndex", "children", "className"]);
const [currentOpenIndex, setCurrentOpenIndex] = React.useState(openIndex);
return (React.createElement(StyledCollapseGroup, Object.assign({ className: classNames('anchor-collapse-group', className) }, props), children &&
children.map((child, index) => React.cloneElement(child, Object.assign({
// TODO: Even if not explicitly set, this forces all child Collapse components
// to closed because `undefined` is falsy. Additionally, this is overwritten
// if `isOpen` is explicitly set, because `...props` is spread below.
isOpen: accordion
? index === currentOpenIndex
: undefined,
// If accordion, use setCurrentOpenIndex as the event handler to control
// accordion functionality of the Collapse child. If the currentOpenIndex
// is the same as the iterator (i) that means the Collapse element is
// already open, in which case currentOpenIndex gets set to 1 which ends
// up returning false for all checks allowing an opened Collapse element
// to be closed.
onClick: accordion
? () => setCurrentOpenIndex(index === currentOpenIndex ? -1 : index)
: undefined,
// No Collapse element should have a bottom border except for the last one.
hasBottomBorder: index === children.length - 1, key: `collapse-child-${index}` }, props)))));
};
//# sourceMappingURL=CollapseGroup.component.js.map