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.

85 lines (80 loc) 2.93 kB
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import * as React from "react"; import styled from "styled-components"; import { left as leftRight } from "../../../utils/rtl"; import defaultTheme from "../../../defaultTheme"; const StyledBar = styled.div.withConfig({ displayName: "Bar__StyledBar", componentId: "rdurdt-0" })(["display:block;position:relative;width:100%;height:24px;cursor:pointer;-webkit-tap-highlight-color:transparent;-webkit-touch-callout:none;-webkit-user-select:none;"]); export const calculateBarPosition = (value, max, min, hasHistogram) => { if (Array.isArray(value)) { return { left: +((value[0] - min) / (max - min + 1) * 100).toFixed(1), width: +((value[value.length - 1] - value[0] + 1) / (max - min + 1) * 100).toFixed(1) }; } const addition = hasHistogram ? 1 : 0; return { left: 0, width: +((value - min + addition) / (max - min + addition) * 100).toFixed(1) }; }; export const StyledBarPart = styled((_ref) => { let { width, left, theme, active } = _ref, props = _objectWithoutProperties(_ref, ["width", "left", "theme", "active"]); return React.createElement("div", props); }).attrs(({ width, left, theme }) => { return { style: { width: `${width}%`, [leftRight({ theme })]: `${left}%` } }; }).withConfig({ displayName: "Bar__StyledBarPart", componentId: "rdurdt-1" })(["position:absolute;height:4px;top:10px;border-radius:4px;background-color:", ";"], ({ theme, active }) => active ? theme.orbit.paletteProductNormal : theme.orbit.paletteInkLighter); StyledBarPart.defaultProps = { theme: defaultTheme }; // $FlowExpected const Bar = React.forwardRef(({ onMouseDown, value, max, min, hasHistogram }, ref) => { const { left, width } = calculateBarPosition(value, max, min, hasHistogram); return React.createElement(StyledBar, { ref: ref, onMouseDown: onMouseDown }, React.createElement(StyledBarPart, { width: 100, left: 0 }), React.createElement(StyledBarPart, { active: true, left: left, width: width })); }); Bar.displayName = "Bar"; export default Bar;