UNPKG

2.56 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.UIDConsumer = exports.UIDFork = exports.UIDReset = void 0;
4var React = require("react");
5var react_1 = require("react");
6var UIDComponent_1 = require("./UIDComponent");
7var context_1 = require("./context");
8var hooks_1 = require("./hooks");
9/**
10 * UID isolation component, required for SSR and testing.
11 * Wrap your application with it to guarantee UID consistency between SSR and CSR.
12 * @param {String} [prefix] - prefix for all generated ids
13 * @example
14 * <UIDReset>
15 * <App />
16 * </UIDReset/>
17 * @see https://github.com/thearnica/react-uid#server-side-friendly-uid
18 */
19var UIDReset = function (_a) {
20 var children = _a.children, _b = _a.prefix, prefix = _b === void 0 ? '' : _b;
21 var valueSource = (0, react_1.useState)(function () { return (0, context_1.createSource)(prefix); })[0];
22 return React.createElement(context_1.source.Provider, { value: valueSource }, children);
23};
24exports.UIDReset = UIDReset;
25/**
26 * Creates a sub-ids for nested components, isolating from inside a branch.
27 * Useful for self-contained elements or code splitting
28 * @see https://github.com/thearnica/react-uid#code-splitting
29 */
30var UIDFork = function (_a) {
31 var children = _a.children, _b = _a.prefix, prefix = _b === void 0 ? '' : _b;
32 var id = (0, hooks_1.useUID)();
33 var valueSource = (0, react_1.useState)(function () { return (0, context_1.createSource)(id + '-' + prefix); })[0];
34 return React.createElement(context_1.source.Provider, { value: valueSource }, children);
35};
36exports.UIDFork = UIDFork;
37/**
38 * UID in form of renderProps. Supports nesting and SSR. Prefer {@link useUID} hook version if possible.
39 * @see https://github.com/thearnica/react-uid#server-side-friendly-uid
40 * @see https://github.com/thearnica/react-uid#react-components
41 * @example
42 * // get UID to connect label to input
43 * <UIDConsumer>
44 * {(id)} => <label htmlFor={id}><input id={id}/>}
45 * </UIDConsumer>
46 *
47 * // get uid to generate uid for a keys in a list
48 * <UIDConsumer>
49 * {(, uid)} => items.map(item => <li key={uid(item) />)}
50 * </UIDConsumer>
51 *
52 * @see {@link useUID} - a hook version of this component
53 * @see {@link UID} - not SSR compatible version
54 */
55var UIDConsumer = function (_a) {
56 var name = _a.name, children = _a.children;
57 return (React.createElement(context_1.source.Consumer, null, function (value) { return React.createElement(UIDComponent_1.UID, { name: name, idSource: value, children: children }); }));
58};
59exports.UIDConsumer = UIDConsumer;