UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.

132 lines (109 loc) 4.48 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "CardSection", { enumerable: true, get: function () { return _CardSection.default; } }); exports.default = exports.StyledCard = void 0; var React = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _Loading = _interopRequireDefault(require("../Loading")); var _CardWrapper = _interopRequireDefault(require("./components/CardWrapper")); var _CardContext = require("./CardContext"); var _defaultTheme = _interopRequireDefault(require("../defaultTheme")); var _getSpacingToken = _interopRequireDefault(require("../common/getSpacingToken")); var _Header = _interopRequireDefault(require("./components/Header")); var _consts = require("../Heading/consts"); var _CardSection = _interopRequireDefault(require("./CardSection")); const StyledCard = _styledComponents.default.div.withConfig({ displayName: "Card__StyledCard", componentId: "sc-ha7axl-0" })(["width:100%;box-sizing:border-box;position:relative;font-family:", ";margin-bottom:", ";"], ({ theme }) => theme.orbit.fontFamily, _getSpacingToken.default); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 exports.StyledCard = StyledCard; StyledCard.defaultProps = { theme: _defaultTheme.default }; const Card = ({ title, titleAs = _consts.ELEMENT_OPTIONS.H2, icon, actions, description, children, dataTest, onClose, loading, header, spaceAfter, dataA11ySection }) => { const [expandedSections, setExpandedSections] = React.useState([]); // handles array of expanded sections const addSection = React.useCallback(index => { setExpandedSections(prev => prev.indexOf(index) === -1 ? [...prev, index] : prev); }, []); const removeSection = React.useCallback(index => { setExpandedSections(prev => prev.indexOf(index) !== -1 ? prev.filter(val => val !== index) : prev); }, []); // Currently disable that code, becuase of IE 11, where it does not work // It will be fixed later, when we'll find solution // eslint-disable-next-line no-unused-vars const renderSection = (item, index) => { if ( /*#__PURE__*/React.isValidElement(item)) { // if (item.props.children && item.type.name !== "CardSection") { // return React.createElement(CardSection, { // ...item.props.children.props, // key: index, // }); return /*#__PURE__*/React.cloneElement(item); } return null; }; return /*#__PURE__*/React.createElement(StyledCard, { spaceAfter: spaceAfter, "data-test": dataTest }, (title || header) && !loading && /*#__PURE__*/React.createElement(_CardWrapper.default, { bottomBorder: !children || expandedSections.some(val => val === 0) }, /*#__PURE__*/React.createElement(_Header.default, { icon: icon, description: description, dataA11ySection: dataA11ySection, actions: actions, title: title, titleAs: titleAs, onClose: onClose, header: header })), children ? React.Children.map(children, (item, key) => { if (!item) return null; const topRoundedBorder = expandedSections.indexOf(key - 1) !== -1 || expandedSections.indexOf(key) !== -1; const bottomRounderBorder = expandedSections.indexOf(key + 1) !== -1 || expandedSections.indexOf(key) !== -1; // This is used for the case when user wants to map sections and change their order // related issue: #1005 const index = Number(item.key) || key; return /*#__PURE__*/React.createElement(_CardContext.Provider, { value: { addSection, removeSection, roundedBorders: { top: topRoundedBorder, bottom: bottomRounderBorder }, index, noBorderTop: index === 0 && Boolean(title), isOpened: expandedSections.some(val => val === index) } }, loading ? /*#__PURE__*/React.createElement(_CardWrapper.default, { noPadding: true }, /*#__PURE__*/React.createElement(_Loading.default, { loading: loading, type: "boxLoader" }, renderSection(item, index))) : renderSection(item, index)); }) : null); }; var _default = Card; exports.default = _default;