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.

96 lines (89 loc) 3.3 kB
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"; const StyledCard = styled.div.withConfig({ displayName: "Card__StyledCard", componentId: "ff0z2v-0" })(["width:100%;box-sizing:border-box;position:relative;font-family:", ";margin-bottom:", ";"], ({ theme }) => theme.orbit.fontFamily, getSpacingToken); StyledCard.defaultProps = { theme: defaultTheme }; const Card = ({ title, icon, actions, description, children, dataTest, 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 (React.isValidElement(item)) { // if (item.props.children && item.type.name !== "CardSection") { // return React.createElement(CardSection, { // ...item.props.children.props, // key: index, // }); // } return React.cloneElement(item); } return null; }; return React.createElement(StyledCard, { spaceAfter: spaceAfter, "data-test": dataTest }, title && !loading && React.createElement(CardWrapper, { bottomBorder: !children || expandedSections.some(val => val === 0) }, React.createElement(Header, { icon: icon, description: description, dataA11ySection: dataA11ySection, actions: actions, title: title, header: header })), children ? React.Children.map(children, (item, key) => { 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 React.createElement(SectionProvider, { value: { addSection, removeSection, roundedBorders: { top: topRoundedBorder, bottom: bottomRounderBorder }, index, noBorderTop: index === 0 && title, isOpened: expandedSections.some(val => val === index) } }, loading ? React.createElement(CardWrapper, { noPadding: true }, React.createElement(Loading, { loading: loading, type: "boxLoader" }, renderSection(item, index))) : renderSection(item, index)); }) : null); }; export default Card; export { default as CardSection } from "./CardSection";