UNPKG

2.4 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 if (typeof b !== "function" && b !== null)
11 throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12 extendStatics(d, b);
13 function __() { this.constructor = d; }
14 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 };
16})();
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.UID = void 0;
19var React = require("react");
20var context_1 = require("./context");
21// --------------------------------------------
22var prefixId = function (id, prefix, name) {
23 var uid = prefix + id;
24 return String(name ? name(uid) : uid);
25};
26/**
27 * @deprecated
28 * UID in form of renderProps (not SSR friendly)
29 * @see https://github.com/thearnica/react-uid#react-components
30 * @example
31 * // get UID to connect label to input
32 * <UID>
33 * {(id)} => <label htmlFor={id}><input id={id}/>}
34 * </UID>
35 *
36 * // get uid to generate uid for a keys in a list
37 * <UID>
38 * {(, uid)} => items.map(item => <li key={uid(item) />)}
39 * </UID>
40 */
41var UID = /** @class */ (function (_super) {
42 __extends(UID, _super);
43 function UID() {
44 var _this = _super !== null && _super.apply(this, arguments) || this;
45 _this.state = {
46 quartz: _this.props.idSource || context_1.counter,
47 prefix: (0, context_1.getPrefix)(_this.props.idSource),
48 id: (0, context_1.getId)(_this.props.idSource || context_1.counter),
49 };
50 _this.uid = function (item) {
51 return prefixId(_this.state.id + '-' + _this.state.quartz.uid(item), _this.state.prefix, _this.props.name);
52 };
53 return _this;
54 }
55 UID.prototype.render = function () {
56 var _a = this.props, children = _a.children, name = _a.name;
57 var _b = this.state, id = _b.id, prefix = _b.prefix;
58 return children(prefixId(id, prefix, name), this.uid);
59 };
60 return UID;
61}(React.Component));
62exports.UID = UID;