UNPKG

4.94 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = void 0;
11
12var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
13
14var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
15
16var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
17
18var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
19
20var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
21
22var React = _interopRequireWildcard(require("react"));
23
24var ReactDOM = _interopRequireWildcard(require("react-dom"));
25
26var _propTypes = _interopRequireDefault(require("prop-types"));
27
28var _utils = require("@material-ui/utils");
29
30var _setRef = _interopRequireDefault(require("../utils/setRef"));
31
32function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
33
34function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
35
36var warnedOnce = false;
37/**
38 * ⚠️⚠️⚠️
39 * If you want the DOM element of a Material-UI component check out
40 * [FAQ: How can I access the DOM element?](/getting-started/faq/#how-can-i-access-the-dom-element)
41 * first.
42 *
43 * This component uses `findDOMNode` which is deprecated in React.StrictMode.
44 *
45 * Helper component to allow attaching a ref to a
46 * wrapped element to access the underlying DOM element.
47 *
48 * It's highly inspired by https://github.com/facebook/react/issues/11401#issuecomment-340543801.
49 * For example:
50 * ```jsx
51 * import React from 'react';
52 * import RootRef from '@material-ui/core/RootRef';
53 *
54 * function MyComponent() {
55 * const domRef = React.useRef();
56 *
57 * React.useEffect(() => {
58 * console.log(domRef.current); // DOM node
59 * }, []);
60 *
61 * return (
62 * <RootRef rootRef={domRef}>
63 * <SomeChildComponent />
64 * </RootRef>
65 * );
66 * }
67 * ```
68 *
69 * @deprecated
70 */
71
72var RootRef = /*#__PURE__*/function (_React$Component) {
73 (0, _inherits2.default)(RootRef, _React$Component);
74
75 var _super = _createSuper(RootRef);
76
77 function RootRef() {
78 (0, _classCallCheck2.default)(this, RootRef);
79 return _super.apply(this, arguments);
80 }
81
82 (0, _createClass2.default)(RootRef, [{
83 key: "componentDidMount",
84 value: function componentDidMount() {
85 this.ref = ReactDOM.findDOMNode(this);
86 (0, _setRef.default)(this.props.rootRef, this.ref);
87 }
88 }, {
89 key: "componentDidUpdate",
90 value: function componentDidUpdate(prevProps) {
91 var ref = ReactDOM.findDOMNode(this);
92
93 if (prevProps.rootRef !== this.props.rootRef || this.ref !== ref) {
94 if (prevProps.rootRef !== this.props.rootRef) {
95 (0, _setRef.default)(prevProps.rootRef, null);
96 }
97
98 this.ref = ref;
99 (0, _setRef.default)(this.props.rootRef, this.ref);
100 }
101 }
102 }, {
103 key: "componentWillUnmount",
104 value: function componentWillUnmount() {
105 this.ref = null;
106 (0, _setRef.default)(this.props.rootRef, null);
107 }
108 }, {
109 key: "render",
110 value: function render() {
111 if (process.env.NODE_ENV !== 'production') {
112 if (!warnedOnce) {
113 warnedOnce = true;
114 console.warn(['Material-UI: The RootRef component is deprecated.', 'The component relies on the ReactDOM.findDOMNode API which is deprecated in React.StrictMode.', 'Instead, you can get a reference to the underlying DOM node of the components via the `ref` prop.'].join('/n'));
115 }
116 }
117
118 return this.props.children;
119 }
120 }]);
121 return RootRef;
122}(React.Component);
123
124process.env.NODE_ENV !== "production" ? RootRef.propTypes = {
125 /**
126 * The wrapped element.
127 */
128 children: _propTypes.default.element.isRequired,
129
130 /**
131 * A ref that points to the first DOM node of the wrapped element.
132 */
133 rootRef: _utils.refType.isRequired
134} : void 0;
135
136if (process.env.NODE_ENV !== 'production') {
137 process.env.NODE_ENV !== "production" ? RootRef.propTypes = (0, _utils.exactProp)(RootRef.propTypes) : void 0;
138}
139
140var _default = RootRef;
141exports.default = _default;
\No newline at end of file