UNPKG

2.01 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = void 0;
9exports.resetUuid = resetUuid;
10var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
12var React = _interopRequireWildcard(require("react"));
13function getUseId() {
14 // We need fully clone React function here to avoid webpack warning React 17 do not export `useId`
15 var fullClone = (0, _objectSpread2.default)({}, React);
16 return fullClone.useId;
17}
18var uuid = 0;
19
20/** @private Note only worked in develop env. Not work in production. */
21function resetUuid() {
22 if (process.env.NODE_ENV !== 'production') {
23 uuid = 0;
24 }
25}
26var useOriginId = getUseId();
27var _default = exports.default = useOriginId ?
28// Use React `useId`
29function useId(id) {
30 var reactId = useOriginId();
31
32 // Developer passed id is single source of truth
33 if (id) {
34 return id;
35 }
36
37 // Test env always return mock id
38 if (process.env.NODE_ENV === 'test') {
39 return 'test-id';
40 }
41 return reactId;
42} :
43// Use compatible of `useId`
44function useCompatId(id) {
45 // Inner id for accessibility usage. Only work in client side
46 var _React$useState = React.useState('ssr-id'),
47 _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
48 innerId = _React$useState2[0],
49 setInnerId = _React$useState2[1];
50 React.useEffect(function () {
51 var nextId = uuid;
52 uuid += 1;
53 setInnerId("rc_unique_".concat(nextId));
54 }, []);
55
56 // Developer passed id is single source of truth
57 if (id) {
58 return id;
59 }
60
61 // Test env always return mock id
62 if (process.env.NODE_ENV === 'test') {
63 return 'test-id';
64 }
65
66 // Return react native id or inner id
67 return innerId;
68};
\No newline at end of file