@elastic/eui
Version:
Elastic UI Component Library
139 lines (133 loc) • 6.31 kB
JavaScript
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, { Fragment, useContext } from 'react';
import { EuiI18nConsumer } from '../context';
import { I18nContext } from '../context/context';
import { processStringToChildren } from './i18n_util';
import { jsx as ___EmotionJSX } from "@emotion/react";
function errorOnMissingValues(token) {
throw new Error("I18n mapping for token \"".concat(token, "\" is a formatting function but no values were provided."));
}
function lookupToken(options) {
var token = options.token,
i18nMapping = options.i18nMapping,
valueDefault = options.valueDefault,
i18nMappingFunc = options.i18nMappingFunc,
values = options.values,
render = options.render;
var renderable = i18nMapping && i18nMapping[token] || valueDefault;
if (typeof renderable === 'function') {
if (values === undefined) {
return errorOnMissingValues(token);
}
// @ts-ignore TypeScript complains that `DEFAULT` doesn't have a call signature but we verified `renderable` is a function
var rendered = renderable(values);
return i18nMappingFunc && typeof rendered === 'string' ? i18nMappingFunc(rendered) : rendered;
} else if (values === undefined || typeof renderable !== 'string') {
if (i18nMappingFunc && typeof valueDefault === 'string') {
renderable = i18nMappingFunc(valueDefault);
}
// there's a hole in the typings here as there is no guarantee that i18nMappingFunc
// returned the same type of the default value, but we need to keep that assumption
return renderable;
}
var children = processStringToChildren(renderable, values, i18nMappingFunc);
if (typeof children === 'string') {
// likewise, `processStringToChildren` returns a string or ReactNode[] depending on
// the type of `values`, so we will make the assumption that the default value is correct.
return children;
}
var Component = render ? render(children) : function () {
return ___EmotionJSX(Fragment, null, children);
};
// same reasons as above, we can't promise the transforms match the default's type
return /*#__PURE__*/React.createElement(Component, values);
}
function isI18nTokensShape(x) {
return x.tokens != null;
}
// Must use the generics <T extends {}>
// If instead typed with React.FunctionComponent there isn't feedback given back to the dev
// when using a `values` object with a renderer callback.
var EuiI18n = function EuiI18n(props) {
return ___EmotionJSX(EuiI18nConsumer, null, function (i18nConfig) {
var mapping = i18nConfig.mapping,
mappingFunc = i18nConfig.mappingFunc,
render = i18nConfig.render;
if (isI18nTokensShape(props)) {
return props.children(props.tokens.map(function (token, idx) {
return lookupToken({
token: token,
i18nMapping: mapping,
i18nMappingFunc: mappingFunc,
valueDefault: props.defaults[idx],
values: props.values,
render: render
});
}));
}
var tokenValue = lookupToken({
token: props.token,
i18nMapping: mapping,
valueDefault: props.default,
i18nMappingFunc: mappingFunc,
values: props.values,
render: render
});
if (props.children) {
return props.children(tokenValue);
} else {
return tokenValue;
}
});
};
// A single default could be a string, react child, or render function
// An array with multiple defaults can only be an array of strings or elements
function useEuiI18n() {
var i18nConfig = useContext(I18nContext);
var mapping = i18nConfig.mapping,
mappingFunc = i18nConfig.mappingFunc,
render = i18nConfig.render;
for (var _len = arguments.length, props = new Array(_len), _key = 0; _key < _len; _key++) {
props[_key] = arguments[_key];
}
if (typeof props[0] === 'string') {
var _token = props[0],
_defaultValue = props[1],
_values = props[2];
return lookupToken({
token: _token,
i18nMapping: mapping,
valueDefault: _defaultValue,
i18nMappingFunc: mappingFunc,
values: _values,
render: render
});
} else {
var _ref = props,
_ref2 = _slicedToArray(_ref, 2),
_tokens = _ref2[0],
_defaultValues = _ref2[1];
return _tokens.map(function (token, idx) {
return lookupToken({
token: token,
i18nMapping: mapping,
valueDefault: _defaultValues[idx],
i18nMappingFunc: mappingFunc,
render: render
});
});
}
}
export { EuiI18n, useEuiI18n };