@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.
108 lines (100 loc) • 4.35 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import * as React from "react";
import styled from "styled-components";
import transition from "../../../utils/transition";
import { left as leftRight } from "../../../utils/rtl";
import defaultTheme from "../../../defaultTheme";
export var calculateLeftPosition = function calculateLeftPosition(valueNow, valueMin, valueMax, isFirst, isSimple) {
// if first, stick it to the left edge
if (isFirst) {
// if simple (one handle and without histogram)
if (isSimple) {
return +((valueNow - valueMin) / (valueMax - valueMin) * 100).toFixed(1);
}
return +((valueNow - valueMin) / (valueMax - valueMin + 1) * 100).toFixed(1);
} // for every other handle stick on the right edge
return +((valueNow - valueMin + 1) / (valueMax - valueMin + 1) * 100).toFixed(1);
};
export var isFirst = function isFirst(value, valueNow, index, hasHistogram) {
if (Array.isArray(value)) {
var max = Math.max.apply(Math, _toConsumableArray(value));
var min = Math.min.apply(Math, _toConsumableArray(value));
var maxEqualsMin = max === min;
var minEqualsValueNow = min === valueNow;
if (index !== 0 && maxEqualsMin) return false;
return maxEqualsMin || minEqualsValueNow;
}
return !hasHistogram;
};
var StyledHandle = styled(function (_ref) {
var left = _ref.left,
theme = _ref.theme,
onTop = _ref.onTop,
props = _objectWithoutProperties(_ref, ["left", "theme", "onTop"]);
return /*#__PURE__*/React.createElement("div", props);
}).attrs(function (_ref2) {
var _style;
var left = _ref2.left,
onTop = _ref2.onTop,
theme = _ref2.theme;
return {
style: (_style = {}, _defineProperty(_style, leftRight({
theme: theme
}), "calc(".concat(left, "% - 12px)")), _defineProperty(_style, "zIndex", onTop ? 20 : undefined), _style)
};
}).withConfig({
displayName: "Handle__StyledHandle",
componentId: "sc-1douguf-0"
})(["display:flex;position:absolute;right:0;bottom:0;flex:0 0 24px;align-items:center;justify-content:center;box-sizing:border-box;width:24px;height:24px;border-radius:24px;box-shadow:", ";background-color:", ";cursor:pointer;transition:", ";-webkit-tap-highlight-color:transparent;-webkit-touch-callout:none;-webkit-user-select:none;:after{content:\"\";display:block;width:8px;height:8px;background-color:", ";border-radius:8px;}:hover,:focus,:active{outline:none;box-shadow:", ";}"], function (_ref3) {
var theme = _ref3.theme;
return theme.orbit.boxShadowAction;
}, function (_ref4) {
var theme = _ref4.theme;
return theme.orbit.paletteWhite;
}, transition(["box-shadow"], "fast", "ease-in-out"), function (_ref5) {
var theme = _ref5.theme;
return theme.orbit.paletteBlueNormal;
}, function (_ref6) {
var theme = _ref6.theme;
return theme.orbit.boxShadowActionActive;
});
StyledHandle.defaultProps = {
theme: defaultTheme
};
var Handle = function Handle(_ref7) {
var tabIndex = _ref7.tabIndex,
onMouseDown = _ref7.onMouseDown,
onFocus = _ref7.onFocus,
onTouchStart = _ref7.onTouchStart,
valueMax = _ref7.valueMax,
valueMin = _ref7.valueMin,
onTop = _ref7.onTop,
value = _ref7.value,
index = _ref7.index,
ariaValueText = _ref7.ariaValueText,
ariaLabel = _ref7.ariaLabel,
hasHistogram = _ref7.hasHistogram,
dataTest = _ref7.dataTest;
var valueNow = Array.isArray(value) ? value[index] : value;
var first = isFirst(value, valueNow, index, hasHistogram);
var isSimple = !hasHistogram && !Array.isArray(value);
var left = calculateLeftPosition(valueNow, valueMin, valueMax, first, isSimple);
return /*#__PURE__*/React.createElement(StyledHandle, {
"data-test": dataTest,
tabIndex: tabIndex,
onTop: onTop,
role: "slider",
onMouseDown: onMouseDown,
onFocus: onFocus,
onTouchStart: onTouchStart,
"aria-valuemax": valueMax,
"aria-valuemin": valueMin,
"aria-valuenow": valueNow,
"aria-label": Array.isArray(ariaLabel) && typeof index !== "undefined" ? ariaLabel[index] : ariaLabel,
"aria-valuetext": ariaValueText,
left: left
});
};
export default Handle;