@elastic/eui
Version:
Elastic UI Component Library
248 lines (247 loc) • 11.5 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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; }
/*
* 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, { useState } from 'react';
import { useGeneratedHtmlId } from '../../../services';
import { EuiI18n } from '../../i18n';
import { EuiFlexGroup, EuiFlexItem } from '../../flex';
import { EuiSelect, EuiFieldNumber, EuiFormLabel, EuiSwitch } from '../../form';
import { EuiScreenReaderOnly } from '../../accessibility';
import { useI18nTimeOptions } from '../super_date_picker/time_options';
import { EuiQuickSelectPanel } from '../super_date_picker/quick_select_popover/quick_select_panel';
import { jsx as ___EmotionJSX } from "@emotion/react";
var MILLISECONDS_IN_SECOND = 1000;
var MILLISECONDS_IN_MINUTE = MILLISECONDS_IN_SECOND * 60;
var MILLISECONDS_IN_HOUR = MILLISECONDS_IN_MINUTE * 60;
var fromMilliseconds = function fromMilliseconds(milliseconds, unit) {
var round = function round(value) {
return parseFloat(value.toFixed(2));
};
if (unit === 'h' || !unit && milliseconds > MILLISECONDS_IN_HOUR) {
return {
units: 'h',
value: round(milliseconds / MILLISECONDS_IN_HOUR)
};
}
if (unit === 'm' || !unit && milliseconds > MILLISECONDS_IN_MINUTE) {
return {
units: 'm',
value: round(milliseconds / MILLISECONDS_IN_MINUTE)
};
}
return {
units: 's',
value: round(milliseconds / MILLISECONDS_IN_SECOND)
};
};
var toMilliseconds = function toMilliseconds(units, value) {
switch (units) {
case 'h':
return Math.round(value * MILLISECONDS_IN_HOUR);
case 'm':
return Math.round(value * MILLISECONDS_IN_MINUTE);
case 's':
default:
return Math.round(value * MILLISECONDS_IN_SECOND);
}
};
var getMinInterval = function getMinInterval(minInterval, unit) {
if (!minInterval) return 0;
var _fromMilliseconds = fromMilliseconds(minInterval, unit),
value = _fromMilliseconds.value;
return Math.floor(value || 0);
};
export var EuiRefreshInterval = function EuiRefreshInterval(_ref) {
var _ref$isPaused = _ref.isPaused,
isPaused = _ref$isPaused === void 0 ? true : _ref$isPaused,
_ref$refreshInterval = _ref.refreshInterval,
refreshInterval = _ref$refreshInterval === void 0 ? 1000 : _ref$refreshInterval,
_ref$minInterval = _ref.minInterval,
minInterval = _ref$minInterval === void 0 ? 0 : _ref$minInterval,
intervalUnits = _ref.intervalUnits,
onRefreshChange = _ref.onRefreshChange;
var _useState = useState(function () {
return _objectSpread(_objectSpread({}, fromMilliseconds(refreshInterval || 0, intervalUnits)), {}, {
min: getMinInterval(minInterval, intervalUnits)
});
}),
_useState2 = _slicedToArray(_useState, 2),
state = _useState2[0],
setState = _useState2[1];
var refreshSelectionId = useGeneratedHtmlId({
prefix: 'euiRefreshInterval'
});
var _useI18nTimeOptions = useI18nTimeOptions(),
refreshUnitsOptions = _useI18nTimeOptions.refreshUnitsOptions;
var applyRefreshInterval = function applyRefreshInterval(nextState) {
var units = nextState.units,
value = nextState.value;
if (value === '') {
return;
}
if (!onRefreshChange) {
return;
}
var refreshIntervalMs = Math.max(toMilliseconds(units, value), minInterval || 0);
onRefreshChange({
refreshInterval: refreshIntervalMs,
intervalUnits: units,
isPaused: refreshIntervalMs <= 0 ? true : !!isPaused
});
};
var onValueChange = function onValueChange(event) {
var sanitizedValue = parseFloat(event.target.value);
var newValue = isNaN(sanitizedValue) ? '' : sanitizedValue;
var nextState = _objectSpread(_objectSpread({}, state), {}, {
value: newValue
});
setState(nextState);
applyRefreshInterval(nextState);
};
var onUnitsChange = function onUnitsChange(event) {
var units = event.target.value;
var nextState = _objectSpread(_objectSpread({}, state), {}, {
units: units,
min: getMinInterval(minInterval, units)
});
setState(nextState);
applyRefreshInterval(nextState);
};
var startRefresh = function startRefresh() {
var value = state.value,
units = state.units;
if (value !== '' && value > 0 && onRefreshChange !== undefined) {
onRefreshChange({
refreshInterval: toMilliseconds(units, value),
intervalUnits: units,
isPaused: false
});
}
};
var handleKeyDown = function handleKeyDown(_ref2) {
var key = _ref2.key;
if (key === 'Enter') {
startRefresh();
}
};
var toggleRefresh = function toggleRefresh() {
if (!onRefreshChange || state.value === '') {
return;
}
var units = state.units,
value = state.value;
onRefreshChange({
refreshInterval: toMilliseconds(units, value),
intervalUnits: units,
isPaused: !isPaused
});
};
var renderScreenReaderText = function renderScreenReaderText() {
var value = state.value,
units = state.units;
var options = refreshUnitsOptions.find(function (_ref3) {
var value = _ref3.value;
return value === units;
});
var optionText = options ? options.text : '';
var fullDescription = isPaused ? ___EmotionJSX(EuiI18n, {
token: "euiRefreshInterval.fullDescriptionOff",
default: "Refresh is off, interval set to {optionValue} {optionText}.",
values: {
optionValue: value,
optionText: optionText
}
}) : ___EmotionJSX(EuiI18n, {
token: "euiRefreshInterval.fullDescriptionOn",
default: "Refresh is on, interval set to {optionValue} {optionText}.",
values: {
optionValue: value,
optionText: optionText
}
});
return ___EmotionJSX(EuiScreenReaderOnly, null, ___EmotionJSX("p", {
id: refreshSelectionId
}, fullDescription));
};
var value = state.value,
units = state.units,
min = state.min;
return ___EmotionJSX(EuiI18n, {
tokens: ['euiRefreshInterval.toggleLabel', 'euiRefreshInterval.toggleAriaLabel', 'euiRefreshInterval.valueAriaLabel', 'euiRefreshInterval.unitsAriaLabel'],
defaults: ['Refresh every', 'Toggle refresh', 'Refresh interval value', 'Refresh interval units']
}, function (_ref4) {
var _ref5 = _slicedToArray(_ref4, 4),
toggleLabel = _ref5[0],
toggleAriaLabel = _ref5[1],
valueAriaLabel = _ref5[2],
unitsAriaLabel = _ref5[3];
return ___EmotionJSX(EuiQuickSelectPanel, null, ___EmotionJSX(EuiFlexGroup, {
alignItems: "center",
gutterSize: "s",
responsive: false,
wrap: true
}, ___EmotionJSX(EuiFlexItem, {
grow: false
}, ___EmotionJSX(EuiSwitch, {
"data-test-subj": "superDatePickerToggleRefreshButton",
checked: !isPaused,
onChange: toggleRefresh,
compressed: true
// The IDs attached to this visible label are unused - we override with our own aria-label
,
label: ___EmotionJSX(EuiFormLabel, null, toggleLabel),
"aria-label": toggleAriaLabel,
"aria-labelledby": undefined,
"aria-describedby": refreshSelectionId
})), ___EmotionJSX(EuiFlexItem, {
style: {
minWidth: 60
}
}, ___EmotionJSX(EuiFieldNumber, {
compressed: true,
fullWidth: true,
value: value,
min: min,
onChange: onValueChange,
onKeyDown: handleKeyDown,
isInvalid: !isPaused && (value === '' || value <= 0),
disabled: isPaused,
"aria-label": valueAriaLabel,
"aria-describedby": refreshSelectionId,
"data-test-subj": "superDatePickerRefreshIntervalInput"
})), ___EmotionJSX(EuiFlexItem, {
style: {
minWidth: 100
},
grow: 2
}, ___EmotionJSX(EuiSelect, {
compressed: true,
fullWidth: true,
"aria-label": unitsAriaLabel,
"aria-describedby": refreshSelectionId,
value: units,
disabled: isPaused,
options: refreshUnitsOptions,
onChange: onUnitsChange,
onKeyDown: handleKeyDown,
"data-test-subj": "superDatePickerRefreshIntervalUnitsSelect"
}))), renderScreenReaderText());
});
};
EuiRefreshInterval.displayName = 'EuiRefreshInterval';