UNPKG

@elastic/eui

Version:

Elastic UI Component Library

146 lines (140 loc) 7.6 kB
"use strict"; 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); } Object.defineProperty(exports, "__esModule", { value: true }); exports.EuiI18n = void 0; exports.useEuiI18n = useEuiI18n; var _react = _interopRequireWildcard(require("react")); var _context = require("../context"); var _context2 = require("../context/context"); var _i18n_util = require("./i18n_util"); var _react2 = require("@emotion/react"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } 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. */ 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 = (0, _i18n_util.processStringToChildren)(renderable, values, i18nMappingFunc); if (typeof children === 'string') { // likewise, `processStringToChildren` returns a string or ReactChild[] 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 (0, _react2.jsx)(_react.Fragment, null, children); }; // same reasons as above, we can't promise the transforms match the default's type return /*#__PURE__*/_react.default.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 = exports.EuiI18n = function EuiI18n(props) { return (0, _react2.jsx)(_context.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 = (0, _react.useContext)(_context2.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 }); }); } }