UNPKG

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