@elastic/eui
Version:
Elastic UI Component Library
68 lines (64 loc) • 3.77 kB
JavaScript
;
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.htmlIdGenerator = htmlIdGenerator;
exports.useGeneratedHtmlId = void 0;
var _react = _interopRequireWildcard(require("react"));
var _uuid = require("uuid");
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; }
/*
* 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.
*/
/**
* This function returns a function to generate ids.
* This can be used to generate unique, but predictable ids to pair labels
* with their inputs. It takes an optional prefix as a parameter. If you don't
* specify it, it generates a random id prefix. If you specify a custom prefix
* it should begin with an letter to be HTML4 compliant.
*/
function htmlIdGenerator() {
var idPrefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var staticUuid = (0, _uuid.v1)();
return function () {
var idSuffix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var prefix = "".concat(idPrefix).concat(idPrefix !== '' ? '_' : 'i');
var suffix = idSuffix ? "_".concat(idSuffix) : '';
return "".concat(prefix).concat(suffix ? staticUuid : (0, _uuid.v1)()).concat(suffix);
};
}
/**
* Generates a memoized ID that remains static until component unmount.
* This prevents IDs from being re-randomized on every component update.
*/
// We can remove this deprecated hook once EUI no longer needs to support React 16-17
var useDeprecatedGeneratedHtmlId = function useDeprecatedGeneratedHtmlId() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
prefix = _ref.prefix,
suffix = _ref.suffix,
conditionalId = _ref.conditionalId;
return (0, _react.useMemo)(function () {
return conditionalId || htmlIdGenerator(prefix)(suffix);
}, [conditionalId, prefix, suffix]);
};
var useNewGeneratedHtmlId = function useNewGeneratedHtmlId() {
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref2$prefix = _ref2.prefix,
prefix = _ref2$prefix === void 0 ? '' : _ref2$prefix,
_ref2$suffix = _ref2.suffix,
suffix = _ref2$suffix === void 0 ? '' : _ref2$suffix,
conditionalId = _ref2.conditionalId;
// Using the default export and dot notation here is intentional
// to prevent React <18 import errors.
var id = _react.default.useId();
return (0, _react.useMemo)(function () {
return conditionalId || "".concat(prefix).concat(id).concat(suffix);
}, [id, conditionalId, prefix, suffix]);
};
var useGeneratedHtmlId = exports.useGeneratedHtmlId = 'useId' in _react.default ? useNewGeneratedHtmlId : useDeprecatedGeneratedHtmlId;