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.

62 lines (61 loc) 2.44 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import * as React from "react"; import cx from "clsx"; import handleKeyDown from "../../utils/handleKeyDown"; import Separator from "../../Separator"; import { ItinerarySegmentProvider } from "./context"; import { spaceAfterClasses } from "../../common/tailwind"; const ItinerarySegment = ({ children, spaceAfter, dataTest, noElevation, actionable = true, banner, onClick, onExpand, onCollapse }) => { const content = React.Children.toArray(children); const [opened, setOpened] = React.useState(false); const handleClick = ev => { const target = ev.target; const isHorizontalScrollOverflowing = target.closest(".orbit-horizontal-scroll[data-overflowing]"); if (isHorizontalScrollOverflowing || document && document.getSelection()?.type === "Range") return; ev.stopPropagation(); if (onClick) onClick(ev); if (!opened && onExpand) onExpand(ev); if (opened && onCollapse) onCollapse(ev); setOpened(prev => !prev); }; const isInteractive = actionable || onClick || onExpand || onCollapse; const parts = /*#__PURE__*/React.createElement("div", _extends({ className: "pt-300" }, isInteractive && { role: "button", tabIndex: 0, onClick: handleClick, onKeyDown: handleKeyDown(() => setOpened(prev => !prev)) }), React.Children.map(children, (el, i) => { if (! /*#__PURE__*/React.isValidElement(el)) return null; return /*#__PURE__*/React.createElement(ItinerarySegmentProvider, { index: i, opened: opened, last: i === content.length - 1, isNextHidden: Boolean(content[i + 1]?.props?.hidden), isPrevHidden: Boolean(content[i - 1]?.props?.hidden), isBanner: !!banner, count: content.length, isHidden: !!(el.props && el.props.hidden), noElevation: !!noElevation }, el); })); return /*#__PURE__*/React.createElement("div", { className: cx("orbit-itinerary-segment duration-fast transition ease-in-out", "rounded-300 pb-300 px-0", spaceAfter && spaceAfterClasses[spaceAfter], !noElevation && "shadow-level1", actionable && !noElevation && "hover:shadow-level2 focus:shadow-level2"), "data-test": dataTest }, parts, Boolean(banner) && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Separator, { spaceAfter: "small" }), banner)); }; export default ItinerarySegment;