1 | import { __extends } from "tslib";
|
2 | import * as React from 'react';
|
3 | import { Async } from './Async';
|
4 | import { EventGroup } from './EventGroup';
|
5 | import { warnConditionallyRequiredProps } from './warn/warnConditionallyRequiredProps';
|
6 | import { warnMutuallyExclusive } from './warn/warnMutuallyExclusive';
|
7 | import { warnDeprecations } from './warn/warnDeprecations';
|
8 | /**
|
9 | * BaseComponent class, which provides basic helpers for all components.
|
10 | *
|
11 | * @public
|
12 | * {@docCategory BaseComponent}
|
13 | *
|
14 | * @deprecated Do not use. We are moving away from class component.
|
15 | */
|
16 | var BaseComponent = /** @class */ (function (_super) {
|
17 | __extends(BaseComponent, _super);
|
18 | /**
|
19 | * BaseComponent constructor
|
20 | * @param props - The props for the component.
|
21 | * @param context - The context for the component.
|
22 | */
|
23 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
24 | function BaseComponent(props, context) {
|
25 | var _this = _super.call(this, props, context) || this;
|
26 | // eslint-disable-next-line deprecation/deprecation
|
27 | _makeAllSafe(_this, BaseComponent.prototype, [
|
28 | 'componentDidMount',
|
29 | 'shouldComponentUpdate',
|
30 | 'getSnapshotBeforeUpdate',
|
31 | 'render',
|
32 | 'componentDidUpdate',
|
33 | 'componentWillUnmount',
|
34 | ]);
|
35 | return _this;
|
36 | }
|
37 | /**
|
38 | * When the component receives props, make sure the componentRef is updated.
|
39 | */
|
40 | BaseComponent.prototype.componentDidUpdate = function (prevProps, prevState) {
|
41 | this._updateComponentRef(prevProps, this.props);
|
42 | };
|
43 | /**
|
44 | * When the component has mounted, update the componentRef.
|
45 | */
|
46 | BaseComponent.prototype.componentDidMount = function () {
|
47 | this._setComponentRef(this.props.componentRef, this);
|
48 | };
|
49 | /**
|
50 | * If we have disposables, dispose them automatically on unmount.
|
51 | */
|
52 | BaseComponent.prototype.componentWillUnmount = function () {
|
53 | this._setComponentRef(this.props.componentRef, null);
|
54 | if (this.__disposables) {
|
55 | for (var i = 0, len = this._disposables.length; i < len; i++) {
|
56 | var disposable = this.__disposables[i];
|
57 | if (disposable.dispose) {
|
58 | disposable.dispose();
|
59 | }
|
60 | }
|
61 | this.__disposables = null;
|
62 | }
|
63 | };
|
64 | Object.defineProperty(BaseComponent.prototype, "className", {
|
65 | /**
|
66 | * Gets the object's class name.
|
67 | */
|
68 | get: function () {
|
69 | if (!this.__className) {
|
70 | var funcNameRegex = /function (.{1,})\(/;
|
71 | var results = funcNameRegex.exec(this.constructor.toString());
|
72 | this.__className = results && results.length > 1 ? results[1] : '';
|
73 | }
|
74 | return this.__className;
|
75 | },
|
76 | enumerable: true,
|
77 | configurable: true
|
78 | });
|
79 | Object.defineProperty(BaseComponent.prototype, "_disposables", {
|
80 | /**
|
81 | * Allows subclasses to push things to this._disposables to be auto disposed.
|
82 | */
|
83 | get: function () {
|
84 | if (!this.__disposables) {
|
85 | this.__disposables = [];
|
86 | }
|
87 | return this.__disposables;
|
88 | },
|
89 | enumerable: true,
|
90 | configurable: true
|
91 | });
|
92 | Object.defineProperty(BaseComponent.prototype, "_async", {
|
93 | /**
|
94 | * Gets the async instance associated with the component, created on demand. The async instance gives
|
95 | * subclasses a way to execute setTimeout/setInterval async calls safely, where the callbacks
|
96 | * will be cleared/ignored automatically after unmounting. The helpers within the async object also
|
97 | * preserve the this pointer so that you don't need to "bind" the callbacks.
|
98 | */
|
99 | get: function () {
|
100 | if (!this.__async) {
|
101 | this.__async = new Async(this);
|
102 | this._disposables.push(this.__async);
|
103 | }
|
104 | return this.__async;
|
105 | },
|
106 | enumerable: true,
|
107 | configurable: true
|
108 | });
|
109 | Object.defineProperty(BaseComponent.prototype, "_events", {
|
110 | /**
|
111 | * Gets the event group instance assocaited with the component, created on demand. The event instance
|
112 | * provides on/off methods for listening to DOM (or regular javascript object) events. The event callbacks
|
113 | * will be automatically disconnected after unmounting. The helpers within the events object also
|
114 | * preserve the this reference so that you don't need to "bind" the callbacks.
|
115 | */
|
116 | get: function () {
|
117 | if (!this.__events) {
|
118 | this.__events = new EventGroup(this);
|
119 | this._disposables.push(this.__events);
|
120 | }
|
121 | return this.__events;
|
122 | },
|
123 | enumerable: true,
|
124 | configurable: true
|
125 | });
|
126 | /**
|
127 | * Helper to return a memoized ref resolver function.
|
128 | * @param refName - Name of the member to assign the ref to.
|
129 | * @returns A function instance keyed from the given refname.
|
130 | * @deprecated Use `createRef` from React.createRef.
|
131 | */
|
132 | BaseComponent.prototype._resolveRef = function (refName) {
|
133 | var _this = this;
|
134 | if (!this.__resolves) {
|
135 | this.__resolves = {};
|
136 | }
|
137 | if (!this.__resolves[refName]) {
|
138 | this.__resolves[refName] = function (ref) {
|
139 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
140 | return (_this[refName] = ref);
|
141 | };
|
142 | }
|
143 | return this.__resolves[refName];
|
144 | };
|
145 | /**
|
146 | * Updates the componentRef (by calling it with "this" when necessary.)
|
147 | */
|
148 | BaseComponent.prototype._updateComponentRef = function (currentProps, newProps) {
|
149 | if (newProps === void 0) { newProps = {}; }
|
150 | // currentProps *should* always be defined, but verify that just in case a subclass is manually
|
151 | // calling a lifecycle method with no parameters (which has happened) or other odd usage.
|
152 | if (currentProps && newProps && currentProps.componentRef !== newProps.componentRef) {
|
153 | this._setComponentRef(currentProps.componentRef, null);
|
154 | this._setComponentRef(newProps.componentRef, this);
|
155 | }
|
156 | };
|
157 | /**
|
158 | * Warns when a deprecated props are being used.
|
159 | *
|
160 | * @param deprecationMap - The map of deprecations, where key is the prop name and the value is
|
161 | * either null or a replacement prop name.
|
162 | */
|
163 | BaseComponent.prototype._warnDeprecations = function (deprecationMap) {
|
164 | warnDeprecations(this.className, this.props, deprecationMap);
|
165 | };
|
166 | /**
|
167 | * Warns when props which are mutually exclusive with each other are both used.
|
168 | *
|
169 | * @param mutuallyExclusiveMap - The map of mutually exclusive props.
|
170 | */
|
171 | BaseComponent.prototype._warnMutuallyExclusive = function (mutuallyExclusiveMap) {
|
172 | warnMutuallyExclusive(this.className, this.props, mutuallyExclusiveMap);
|
173 | };
|
174 | /**
|
175 | * Warns when props are required if a condition is met.
|
176 | *
|
177 | * @param requiredProps - The name of the props that are required when the condition is met.
|
178 | * @param conditionalPropName - The name of the prop that the condition is based on.
|
179 | * @param condition - Whether the condition is met.
|
180 | */
|
181 | BaseComponent.prototype._warnConditionallyRequiredProps = function (requiredProps, conditionalPropName, condition) {
|
182 | warnConditionallyRequiredProps(this.className, this.props, requiredProps, conditionalPropName, condition);
|
183 | };
|
184 | BaseComponent.prototype._setComponentRef = function (ref, value) {
|
185 | if (!this._skipComponentRefResolution && ref) {
|
186 | if (typeof ref === 'function') {
|
187 | ref(value);
|
188 | }
|
189 | if (typeof ref === 'object') {
|
190 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
191 | ref.current = value;
|
192 | }
|
193 | }
|
194 | };
|
195 | return BaseComponent;
|
196 | }(React.Component));
|
197 | export { BaseComponent };
|
198 | /**
|
199 | * Helper to override a given method with a wrapper method that can try/catch the original, but also
|
200 | * ensures that the BaseComponent's methods are called before the subclass's. This ensures that
|
201 | * componentWillUnmount in the base is called and that things in the _disposables array are disposed.
|
202 | */
|
203 | // eslint-disable-next-line deprecation/deprecation
|
204 | function _makeAllSafe(obj, prototype, methodNames) {
|
205 | for (var i = 0, len = methodNames.length; i < len; i++) {
|
206 | _makeSafe(obj, prototype, methodNames[i]);
|
207 | }
|
208 | }
|
209 | // eslint-disable-next-line deprecation/deprecation
|
210 | function _makeSafe(obj, prototype, methodName) {
|
211 | /* eslint-disable @typescript-eslint/no-explicit-any */
|
212 | var classMethod = obj[methodName];
|
213 | var prototypeMethod = prototype[methodName];
|
214 | if (classMethod || prototypeMethod) {
|
215 | obj[methodName] = function () {
|
216 | var args = [];
|
217 | for (var _i = 0; _i < arguments.length; _i++) {
|
218 | args[_i] = arguments[_i];
|
219 | }
|
220 | /* eslint-enable @typescript-eslint/no-explicit-any */
|
221 | var retVal;
|
222 | if (prototypeMethod) {
|
223 | retVal = prototypeMethod.apply(this, args);
|
224 | }
|
225 | if (classMethod !== prototypeMethod) {
|
226 | retVal = classMethod.apply(this, args);
|
227 | }
|
228 | return retVal;
|
229 | };
|
230 | }
|
231 | }
|
232 | /**
|
233 | * Simple constant function for returning null, used to render empty templates in JSX.
|
234 | *
|
235 | * @public
|
236 | */
|
237 | export function nullRender() {
|
238 | return null;
|
239 | }
|
240 | //# sourceMappingURL=BaseComponent.js.map |
\ | No newline at end of file |