@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.
70 lines (66 loc) • 2.65 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
const _excluded = ["height", "theme", "active"];
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-238qiz-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-238qiz-1"
})(["display:flex;width:100%;height:100%;align-items:center;justify-content:center;"]);
const StyledHistogramColumn = styled((_ref) => {
let {
height,
theme,
active
} = _ref,
props = _objectWithoutProperties(_ref, _excluded);
return /*#__PURE__*/React.createElement("div", props);
}).attrs(({
height
}) => {
return {
style: {
height: `${height.toFixed(2)}%`
}
};
}).withConfig({
displayName: "Histogram__StyledHistogramColumn",
componentId: "sc-238qiz-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); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
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;