70 lines
2.0 KiB
JavaScript
70 lines
2.0 KiB
JavaScript
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
import { isValidElement } from 'react';
|
|
import { isFragment, isMemo } from 'react-is';
|
|
import useMemo from "./hooks/useMemo";
|
|
export function fillRef(ref, node) {
|
|
if (typeof ref === 'function') {
|
|
ref(node);
|
|
} else if (_typeof(ref) === 'object' && ref && 'current' in ref) {
|
|
ref.current = node;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Merge refs into one ref function to support ref passing.
|
|
*/
|
|
export function composeRef() {
|
|
for (var _len = arguments.length, refs = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
refs[_key] = arguments[_key];
|
|
}
|
|
var refList = refs.filter(function (ref) {
|
|
return ref;
|
|
});
|
|
if (refList.length <= 1) {
|
|
return refList[0];
|
|
}
|
|
return function (node) {
|
|
refs.forEach(function (ref) {
|
|
fillRef(ref, node);
|
|
});
|
|
};
|
|
}
|
|
export function useComposeRef() {
|
|
for (var _len2 = arguments.length, refs = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
refs[_key2] = arguments[_key2];
|
|
}
|
|
return useMemo(function () {
|
|
return composeRef.apply(void 0, refs);
|
|
}, refs, function (prev, next) {
|
|
return prev.length !== next.length || prev.every(function (ref, i) {
|
|
return ref !== next[i];
|
|
});
|
|
});
|
|
}
|
|
export function supportRef(nodeOrComponent) {
|
|
var _type$prototype, _nodeOrComponent$prot;
|
|
var type = isMemo(nodeOrComponent) ? nodeOrComponent.type.type : nodeOrComponent.type;
|
|
|
|
// Function component node
|
|
if (typeof type === 'function' && !((_type$prototype = type.prototype) !== null && _type$prototype !== void 0 && _type$prototype.render)) {
|
|
return false;
|
|
}
|
|
|
|
// Class component
|
|
if (typeof nodeOrComponent === 'function' && !((_nodeOrComponent$prot = nodeOrComponent.prototype) !== null && _nodeOrComponent$prot !== void 0 && _nodeOrComponent$prot.render)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
export function supportNodeRef(node) {
|
|
if (! /*#__PURE__*/isValidElement(node)) {
|
|
return false;
|
|
}
|
|
if (isFragment(node)) {
|
|
return false;
|
|
}
|
|
return supportRef(node);
|
|
}
|
|
/* eslint-enable */ |