1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
4 | Object.defineProperty(exports, "__esModule", {
|
5 | value: true
|
6 | });
|
7 | exports.useComposeRef = exports.supportRef = exports.supportNodeRef = exports.getNodeRef = exports.fillRef = exports.composeRef = void 0;
|
8 | var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
9 | var _react = require("react");
|
10 | var _reactIs = require("react-is");
|
11 | var _useMemo = _interopRequireDefault(require("./hooks/useMemo"));
|
12 | var fillRef = exports.fillRef = function fillRef(ref, node) {
|
13 | if (typeof ref === 'function') {
|
14 | ref(node);
|
15 | } else if ((0, _typeof2.default)(ref) === 'object' && ref && 'current' in ref) {
|
16 | ref.current = node;
|
17 | }
|
18 | };
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var composeRef = exports.composeRef = function composeRef() {
|
24 | for (var _len = arguments.length, refs = new Array(_len), _key = 0; _key < _len; _key++) {
|
25 | refs[_key] = arguments[_key];
|
26 | }
|
27 | var refList = refs.filter(Boolean);
|
28 | if (refList.length <= 1) {
|
29 | return refList[0];
|
30 | }
|
31 | return function (node) {
|
32 | refs.forEach(function (ref) {
|
33 | fillRef(ref, node);
|
34 | });
|
35 | };
|
36 | };
|
37 | var useComposeRef = exports.useComposeRef = function useComposeRef() {
|
38 | for (var _len2 = arguments.length, refs = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
39 | refs[_key2] = arguments[_key2];
|
40 | }
|
41 | return (0, _useMemo.default)(function () {
|
42 | return composeRef.apply(void 0, refs);
|
43 | }, refs, function (prev, next) {
|
44 | return prev.length !== next.length || prev.every(function (ref, i) {
|
45 | return ref !== next[i];
|
46 | });
|
47 | });
|
48 | };
|
49 | var supportRef = exports.supportRef = function supportRef(nodeOrComponent) {
|
50 | var _type$prototype, _nodeOrComponent$prot;
|
51 | var type = (0, _reactIs.isMemo)(nodeOrComponent) ? nodeOrComponent.type.type : nodeOrComponent.type;
|
52 |
|
53 |
|
54 | if (typeof type === 'function' && !((_type$prototype = type.prototype) !== null && _type$prototype !== void 0 && _type$prototype.render) && type.$$typeof !== _reactIs.ForwardRef) {
|
55 | return false;
|
56 | }
|
57 |
|
58 |
|
59 | if (typeof nodeOrComponent === 'function' && !((_nodeOrComponent$prot = nodeOrComponent.prototype) !== null && _nodeOrComponent$prot !== void 0 && _nodeOrComponent$prot.render) && nodeOrComponent.$$typeof !== _reactIs.ForwardRef) {
|
60 | return false;
|
61 | }
|
62 | return true;
|
63 | };
|
64 | function isReactElement(node) {
|
65 | return (0, _react.isValidElement)(node) && !(0, _reactIs.isFragment)(node);
|
66 | }
|
67 | var supportNodeRef = exports.supportNodeRef = function supportNodeRef(node) {
|
68 | return isReactElement(node) && supportRef(node);
|
69 | };
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | var getNodeRef = exports.getNodeRef = Number(_react.version.split('.')[0]) >= 19 ?
|
77 |
|
78 | function (node) {
|
79 | if (isReactElement(node)) {
|
80 | return node.props.ref;
|
81 | }
|
82 | return null;
|
83 | } :
|
84 |
|
85 | function (node) {
|
86 | if (isReactElement(node)) {
|
87 | return node.ref;
|
88 | }
|
89 | return null;
|
90 | }; |
\ | No newline at end of file |