@elastic/eui
Version:
Elastic UI Component Library
158 lines (153 loc) • 9.33 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React, { useMemo } from 'react';
import PropTypes from "prop-types";
import { useEuiTheme } from '../../../services';
import { logicalStyles } from '../../../global_styling';
import { useInnerText } from '../../inner_text';
import { calculateThumbPosition, EUI_THUMB_SIZE } from './utils';
import { euiRangeTicksStyles, euiRangeTickStyles } from './range_ticks.styles';
import { jsx as ___EmotionJSX } from "@emotion/react";
var EuiTickValue = function EuiTickValue(_ref) {
var disabled = _ref.disabled,
ticks = _ref.ticks,
min = _ref.min,
max = _ref.max,
value = _ref.value,
onChange = _ref.onChange,
percentageWidth = _ref.percentageWidth,
tickValue = _ref.tickValue,
compressed = _ref.compressed,
trackWidth = _ref.trackWidth;
var euiTheme = useEuiTheme();
var hasCustomTicks = !!ticks;
var tickObject = useMemo(function () {
return hasCustomTicks ? ticks.find(function (o) {
return o.value === tickValue;
}) : {
value: tickValue,
label: tickValue
};
}, [hasCustomTicks, ticks, tickValue]);
var isMinTick = (tickObject === null || tickObject === void 0 ? void 0 : tickObject.value) === min;
var isMaxTick = (tickObject === null || tickObject === void 0 ? void 0 : tickObject.value) === max;
var label = tickObject ? tickObject.label : tickValue;
// Math worked out by trial and error
// Shifts the label into the reserved margin of EuiRangeTrack
var labelShiftVal = useMemo(function () {
return (isMinTick || isMaxTick) && label.length > 3 ? Math.min(label.length * 0.25, 1.25) : 0;
}, [isMinTick, isMaxTick, label]);
var tickStyle = useMemo(function () {
var styles = {};
var shift = "-".concat(labelShiftVal, "em");
if (isMaxTick && labelShiftVal) {
styles.right = '0%';
styles.marginRight = shift;
} else {
var position = calculateThumbPosition(tickValue, min, max, trackWidth);
var thumbOffset = labelShiftVal ? 0 : EUI_THUMB_SIZE / 2;
styles.left = "calc(".concat(position, "% + ").concat(thumbOffset, "px)");
if (labelShiftVal) styles.marginLeft = shift;
}
styles.maxWidth = hasCustomTicks ? undefined : "".concat(percentageWidth, "%");
return logicalStyles(styles);
}, [isMaxTick, labelShiftVal, trackWidth, tickValue, min, max, hasCustomTicks, percentageWidth]);
// Some ticks need an actual DOM element instead of using a ::before
var pseudoTick = tickObject && !!labelShiftVal && (isMinTick || isMaxTick);
var pseudoShift = useMemo(function () {
if (!labelShiftVal) return {};
var marginProperty = isMaxTick ? 'marginRight' : 'marginLeft';
var tickOffset = euiTheme.euiTheme.size.xs; // xs derived from .euiRangeTicks left/right offset
return logicalStyles(_defineProperty({}, marginProperty, "calc(".concat(labelShiftVal, "em + ").concat(tickOffset, ")")));
}, [labelShiftVal, isMaxTick, euiTheme.euiTheme.size.xs]);
var styles = euiRangeTickStyles(euiTheme);
var cssTickStyles = [styles.euiRangeTick, value === String(tickValue) && styles.selected, hasCustomTicks && styles.isCustom, labelShiftVal && isMinTick && styles.isMin, labelShiftVal && isMaxTick && styles.isMax, !pseudoTick && styles.hasPseudoTickMark, compressed ? styles.compressed : styles.regular];
var _useInnerText = useInnerText(),
_useInnerText2 = _slicedToArray(_useInnerText, 2),
ref = _useInnerText2[0],
innerText = _useInnerText2[1];
return ___EmotionJSX("button", {
type: "button",
className: "euiRangeTick",
css: cssTickStyles,
value: tickValue,
disabled: disabled,
onClick: onChange,
style: tickStyle,
tabIndex: -1,
ref: ref,
title: typeof label === 'string' ? label : innerText
}, pseudoTick && ___EmotionJSX("span", {
className: "euiRangeTick__pseudo",
css: styles.euiRangeTick__pseudo,
"aria-hidden": true,
style: pseudoShift
}), label);
};
EuiTickValue.propTypes = {
tickSequence: PropTypes.arrayOf(PropTypes.number.isRequired).isRequired,
trackWidth: PropTypes.number.isRequired,
onChange: PropTypes.func,
value: PropTypes.oneOfType([PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.number.isRequired]).isRequired, PropTypes.any.isRequired]),
max: PropTypes.number.isRequired,
min: PropTypes.number.isRequired,
/**
* The number to increment or decrement between each interval
* @default 1
*/
step: PropTypes.number,
tickValue: PropTypes.any.isRequired,
percentageWidth: PropTypes.number.isRequired
};
export var EuiRangeTicks = function EuiRangeTicks(props) {
var ticks = props.ticks,
tickSequence = props.tickSequence,
max = props.max,
min = props.min,
_props$tickInterval = props.tickInterval,
tickInterval = _props$tickInterval === void 0 ? 1 : _props$tickInterval,
compressed = props.compressed;
// Calculate the width of each tick mark
var percentageWidth = useMemo(function () {
return tickInterval / (max - min + tickInterval) * 100;
}, [tickInterval, min, max]);
var euiTheme = useEuiTheme();
var styles = euiRangeTicksStyles(euiTheme);
var cssStyles = [styles.euiRangeTicks, compressed ? styles.compressed : styles.regular, ticks && styles.isCustom];
return ___EmotionJSX("div", {
className: "euiRangeTicks",
css: cssStyles
}, tickSequence.map(function (tickValue) {
return ___EmotionJSX(EuiTickValue, _extends({
key: tickValue
}, props, {
percentageWidth: percentageWidth,
tickValue: tickValue
}));
}));
};
EuiRangeTicks.propTypes = {
tickSequence: PropTypes.arrayOf(PropTypes.number.isRequired).isRequired,
trackWidth: PropTypes.number.isRequired,
onChange: PropTypes.func,
value: PropTypes.oneOfType([PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.number.isRequired]).isRequired, PropTypes.any.isRequired]),
max: PropTypes.number.isRequired,
min: PropTypes.number.isRequired,
step: PropTypes.number
};