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.

117 lines (113 loc) 3.94 kB
import * as React from "react"; import styled, { css } 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"; import useTheme from "../hooks/useTheme"; import { spacingUtility } from "../utils/common"; export const StyledCard = styled.div.withConfig({ displayName: "Card__StyledCard", componentId: "sc-1qe4ovj-0" })(["", ";"], ({ margin, theme }) => css(["width:100%;box-sizing:border-box;position:relative;font-family:", ";", ""], theme.orbit.fontFamily, spacingUtility(margin))); StyledCard.defaultProps = { theme: defaultTheme }; const Card = ({ title, titleAs = ELEMENT_OPTIONS.H2, icon, actions, description, children, labelClose = "Close", dataTest, id, onClose, loading, margin, header, spaceAfter, dataA11ySection }) => { const [expandedSections, setExpandedSections] = React.useState([]); const theme = useTheme(); // 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 const renderSection = item => { 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 // TODO: remove spaceAfter in the next major version , { margin: spaceAfter ? { bottom: getSpacingToken({ spaceAfter, theme }) } : margin, "data-test": dataTest, id: id }, (title || header) && !loading && /*#__PURE__*/React.createElement(CardWrapper, { bottomBorder: !children || expandedSections.some(val => val === 0) }, /*#__PURE__*/React.createElement(Header, { icon: icon, description: description, dataA11ySection: dataA11ySection, actions: actions, title: title, labelClose: labelClose, 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 // @ts-expect-error TODO const index = Number(item.key) || key; return /*#__PURE__*/React.createElement(SectionProvider, { 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, { noPadding: true }, /*#__PURE__*/React.createElement(Loading, { loading: loading, type: "boxLoader" }, renderSection(item))) : renderSection(item)); }) : null); }; export default Card; export { default as CardSection } from "./CardSection";