UNPKG

@procore/core-react

Version:
143 lines (136 loc) • 6.56 kB
var _excluded = ["className", "disabled", "onChange", "max", "min", "showLabel", "step", "style", "value"]; function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } import React from 'react'; import { StyledContainer, StyledIncrementContainer, StyledIncrementOverlay, StyledLabel, StyledSlider } from './Slider.styles'; export var getIncrements = function getIncrements(_ref) { var min = _ref.min, max = _ref.max, step = _ref.step; if (!step) { return 0; } return (max - min) / step; }; export var getPercentage = function getPercentage(_ref2) { var max = _ref2.max, min = _ref2.min, value = _ref2.value; return (value - min) / (max - min) * 100; }; export var getLabel = function getLabel(_ref3) { var max = _ref3.max, min = _ref3.min, step = _ref3.step, index = _ref3.index; if (min === 0) { return index * step; } else if (min >= 0) { return index * step + min; } else if (min <= 0) { return (index - getIncrements({ max: max, min: min, step: step })) * step + max; } else { return 0; } }; export var getDefaultValue = function getDefaultValue(_ref4) { var min = _ref4.min, max = _ref4.max, step = _ref4.step; // See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/range#value var defaultVal = max < min ? min : min + (max - min) / 2; return step ? Math.round(defaultVal / step) * step : defaultVal; }; /** A slider is a bar with selectable/grabbable handle. It allows users to click and drag horizontally to a desired value. It is used when users need to select a value from a predefined range of values. Sliders should always have a corresponding value or increment displayed within proximity of the component. @since 10.19.0 @see [Storybook](https://stories.core.procore.com/?path=/story/core-react_demos-slider--demo) @see [Design Guidelines](https://design.procore.com/slider) */ export var Slider = /*#__PURE__*/React.forwardRef(function Slider(_ref5, ref) { var className = _ref5.className, disabled = _ref5.disabled, onChange = _ref5.onChange, _ref5$max = _ref5.max, max = _ref5$max === void 0 ? 100 : _ref5$max, _ref5$min = _ref5.min, min = _ref5$min === void 0 ? 0 : _ref5$min, showLabel = _ref5.showLabel, step = _ref5.step, style = _ref5.style, value_ = _ref5.value, props = _objectWithoutProperties(_ref5, _excluded); var _React$useState = React.useState(false), _React$useState2 = _slicedToArray(_React$useState, 2), isDragging = _React$useState2[0], setIsDragging = _React$useState2[1]; var value = value_ !== null && value_ !== void 0 ? value_ : getDefaultValue({ max: max, min: min, step: step }); var increments = getIncrements({ max: max, min: min, step: step }); // check if the "step" value does not cleanly // divide into max - min if (increments % 1 !== 0) { console.warn('@procore/core-react: SLIDER COMPONENT WARNING: Your step value does not cleanly divide into max - min. This will cause your Slider to display incorrectly.'); } return /*#__PURE__*/React.createElement(StyledContainer, { className: className, style: style, $showLabel: showLabel && Boolean(step) }, /*#__PURE__*/React.createElement(StyledSlider, _extends({}, props, { disabled: disabled, onChange: onChange, max: max, min: min, $percent: getPercentage({ max: max, min: min, value: value }), ref: ref, step: step, type: "range", value: value, onMouseDown: function onMouseDown() { return setIsDragging(true); }, onMouseUp: function onMouseUp() { return setIsDragging(false); }, $isDragging: isDragging })), step && /*#__PURE__*/React.createElement(StyledIncrementContainer, { "aria-hidden": "true" }, Array(Math.floor(increments) + 1).fill(null).map(function (_, index) { return /*#__PURE__*/React.createElement(StyledIncrementOverlay, { key: "core-slider-increment-overlay-".concat(index, "-").concat(min, "-").concat(max, "-").concat(step) }, showLabel && /*#__PURE__*/React.createElement(StyledLabel, { intent: "small" }, getLabel({ min: min, max: max, step: step, index: index }))); }))); }); //# sourceMappingURL=Slider.js.map