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.

120 lines (110 loc) 4.58 kB
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import * as React from "react"; import styled from "styled-components"; import Loading from "../Loading"; import CardWrapper from "./components/CardWrapper"; import { Provider as SectionProvider } from "./CardContext"; import defaultTheme from "../defaultTheme"; import getSpacingToken from "../common/getSpacingToken"; import Header from "./components/Header"; import { ELEMENT_OPTIONS } from "../Heading/consts"; export var StyledCard = styled.div.withConfig({ displayName: "Card__StyledCard", componentId: "ff0z2v-0" })(["width:100%;box-sizing:border-box;position:relative;font-family:", ";margin-bottom:", ";"], function (_ref) { var theme = _ref.theme; return theme.orbit.fontFamily; }, getSpacingToken); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledCard.defaultProps = { theme: defaultTheme }; var Card = function Card(_ref2) { var title = _ref2.title, _ref2$titleAs = _ref2.titleAs, titleAs = _ref2$titleAs === void 0 ? ELEMENT_OPTIONS.H2 : _ref2$titleAs, icon = _ref2.icon, actions = _ref2.actions, description = _ref2.description, children = _ref2.children, dataTest = _ref2.dataTest, onClose = _ref2.onClose, loading = _ref2.loading, header = _ref2.header, spaceAfter = _ref2.spaceAfter, dataA11ySection = _ref2.dataA11ySection; var _React$useState = React.useState([]), _React$useState2 = _slicedToArray(_React$useState, 2), expandedSections = _React$useState2[0], setExpandedSections = _React$useState2[1]; // handles array of expanded sections var addSection = React.useCallback(function (index) { setExpandedSections(function (prev) { return prev.indexOf(index) === -1 ? [].concat(_toConsumableArray(prev), [index]) : prev; }); }, []); var removeSection = React.useCallback(function (index) { setExpandedSections(function (prev) { return prev.indexOf(index) !== -1 ? prev.filter(function (val) { return 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 var renderSection = function 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, { bottomBorder: !children || expandedSections.some(function (val) { return val === 0; }) }, /*#__PURE__*/React.createElement(Header, { icon: icon, description: description, dataA11ySection: dataA11ySection, actions: actions, title: title, titleAs: titleAs, onClose: onClose, header: header })), children ? React.Children.map(children, function (item, key) { if (!item) return null; var topRoundedBorder = expandedSections.indexOf(key - 1) !== -1 || expandedSections.indexOf(key) !== -1; var 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 var index = Number(item.key) || key; return /*#__PURE__*/React.createElement(SectionProvider, { value: { addSection: addSection, removeSection: removeSection, roundedBorders: { top: topRoundedBorder, bottom: bottomRounderBorder }, index: index, noBorderTop: index === 0 && Boolean(title), isOpened: expandedSections.some(function (val) { return val === index; }) } }, loading ? /*#__PURE__*/React.createElement(CardWrapper, { noPadding: true }, /*#__PURE__*/React.createElement(Loading, { loading: loading, type: "boxLoader" }, renderSection(item, index))) : renderSection(item, index)); }) : null); }; export default Card; export { default as CardSection } from "./CardSection";