@fluentui/react-component-ref
Version:
A set of components and utils to deal with React refs.
146 lines (143 loc) • 5.94 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.Ref = void 0;
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var React = _interopRequireWildcard(require("react"));
var ReactIs = _interopRequireWildcard(require("react-is"));
var ReactDOM = _interopRequireWildcard(require("react-dom"));
var _utils = require("./utils");
var _excluded = ["children", "innerRef"];
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
// ========================================================
// react/packages/react-reconciler/src/ReactFiber.js
// ========================================================
/**
* Detects if a passed element is a Fiber object instead of an element. Is needed as `ReactDOM.findDOMNode()` returns
* a Fiber in `react-test-renderer` that can cause issues with tests. Is used only in non-production env.
*
* @see https://github.com/facebook/react/issues/7371#issuecomment-317396864
* @see https://github.com/Semantic-Org/Semantic-UI-React/issues/4061#issuecomment-694895617
*/
function isFiberRef(node) {
if (node === null) {
return false;
}
if (node instanceof Element || node instanceof Text) {
return false;
}
return !!(node.type && node.tag);
}
var Ref = exports.Ref = /*#__PURE__*/function (_React$Component) {
function Ref() {
var _this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
_this.nodeForFind = void 0;
_this.nodeForForward = void 0;
_this.state = {
kind: null
};
_this.handleForwardRefOverride = function (node) {
var _this$props = _this.props,
children = _this$props.children,
innerRef = _this$props.innerRef;
(0, _utils.handleRef)(children.ref, node);
(0, _utils.handleRef)(innerRef, node);
_this.nodeForForward = node;
};
_this.handleSelfOverride = function (node) {
var _this$props2 = _this.props,
children = _this$props2.children,
innerRef = _this$props2.innerRef;
(0, _utils.handleRef)(children.props.innerRef, node);
(0, _utils.handleRef)(innerRef, node);
};
return _this;
}
(0, _inheritsLoose2.default)(Ref, _React$Component);
Ref.getDerivedStateFromProps = function getDerivedStateFromProps(props) {
var child = React.Children.only(props.children);
if (child.type === Ref) {
return {
kind: 'self'
};
}
if (ReactIs.isForwardRef(child)) {
return {
kind: 'forward'
};
}
return {
kind: 'find'
};
};
var _proto = Ref.prototype;
_proto.componentDidMount = function componentDidMount() {
if (this.state.kind === 'find') {
var currentNode = ReactDOM.findDOMNode(this);
if (process.env.NODE_ENV !== 'production') {
if (isFiberRef(currentNode)) {
currentNode = null;
}
}
this.nodeForFind = currentNode;
(0, _utils.handleRef)(this.props.innerRef, currentNode);
}
};
_proto.componentDidUpdate = function componentDidUpdate(prevProps) {
if (this.state.kind === 'forward') {
if (prevProps.innerRef !== this.props.innerRef) {
(0, _utils.handleRef)(this.props.innerRef, this.nodeForForward);
}
} else if (this.state.kind === 'find') {
var currentNode = ReactDOM.findDOMNode(this);
if (process.env.NODE_ENV !== 'production') {
if (isFiberRef(currentNode)) {
currentNode = null;
}
}
var isNodeChanged = this.nodeForFind !== currentNode;
var isRefChanged = prevProps.innerRef !== this.props.innerRef;
if (isNodeChanged) {
this.nodeForFind = currentNode;
}
if (isNodeChanged || isRefChanged) {
(0, _utils.handleRef)(this.props.innerRef, currentNode);
}
}
};
_proto.componentWillUnmount = function componentWillUnmount() {
if (this.state.kind === 'find') {
(0, _utils.handleRef)(this.props.innerRef, null);
this.nodeForFind = null;
}
};
_proto.render = function render() {
var _this$props3 = this.props,
children = _this$props3.children,
innerRef = _this$props3.innerRef,
rest = (0, _objectWithoutPropertiesLoose2.default)(_this$props3, _excluded);
var childWithProps = rest && Object.keys(rest).length > 0 ? /*#__PURE__*/React.cloneElement(children, rest) : children;
if (this.state.kind === 'find') {
return childWithProps;
}
if (this.state.kind === 'forward') {
return /*#__PURE__*/React.cloneElement(childWithProps, {
ref: this.handleForwardRefOverride
});
}
if (this.state.kind === 'self') {
return /*#__PURE__*/React.cloneElement(childWithProps, {
innerRef: this.handleSelfOverride
});
}
return null;
};
return Ref;
}(React.Component);
//# sourceMappingURL=Ref.js.map