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.

61 lines 2.34 kB
import * as React from "react"; import styled from "styled-components"; import defaultTheme from "../../../defaultTheme"; import Loading from "../../../Loading"; const StyledHistogram = styled.div.withConfig({ displayName: "Histogram__StyledHistogram", componentId: "sc-1r9ktah-0" })(["display:flex;justify-content:flex-start;align-items:flex-end;width:100%;height:52px;padding-bottom:3px;overflow:hidden;"]); const StyledLoadingContainer = styled.div.withConfig({ displayName: "Histogram__StyledLoadingContainer", componentId: "sc-1r9ktah-1" })(["display:flex;width:100%;height:100%;align-items:center;justify-content:center;"]); const StyledHistogramColumn = styled(({ height, theme, active, ...props }) => /*#__PURE__*/React.createElement("div", props)).attrs(({ height }) => { return { style: { height: `${height.toFixed(2)}%` } }; }).withConfig({ displayName: "Histogram__StyledHistogramColumn", componentId: "sc-1r9ktah-2" })(["position:relative;min-width:3px;flex:1 0 auto;border-radius:1px;background-color:", ";margin-right:1px;:last-child{margin:0;}:after{display:block;position:absolute;bottom:-3px;content:\" \";width:100%;height:1px;border-radius:1px;background-color:inherit;}"], ({ theme, active }) => active ? theme.orbit.paletteBlueNormal : theme.orbit.paletteBlueLightActive); StyledHistogramColumn.defaultProps = { theme: defaultTheme }; const Histogram = ({ data, value, min, loading = false, loadingText, step }) => { const maxValue = !!data && Math.max(...data); const highlightFrom = Array.isArray(value) ? value[0] : 0; const highlightTo = Array.isArray(value) ? value[value.length - 1] : value; return /*#__PURE__*/React.createElement(StyledHistogram, { role: "presentation" }, loading ? /*#__PURE__*/React.createElement(StyledLoadingContainer, null, /*#__PURE__*/React.createElement(Loading, { type: "inlineLoader", text: loadingText })) : data && data.map((column, index) => { const properIndex = (index + min) * step; return /*#__PURE__*/React.createElement(StyledHistogramColumn, { key: encodeURIComponent(properIndex.toString()), height: maxValue && +(column / maxValue * 100).toFixed(1), active: properIndex >= highlightFrom && properIndex <= highlightTo }); })); }; export default Histogram;