1 | ;
|
2 | /*
|
3 | * Copyright 2015 Palantir Technologies, Inc. All rights reserved.
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.AbstractComponent = void 0;
|
19 | var tslib_1 = require("tslib");
|
20 | var React = tslib_1.__importStar(require("react"));
|
21 | var utils_1 = require("./utils");
|
22 | /**
|
23 | * An abstract component that Blueprint components can extend
|
24 | * in order to add some common functionality like runtime props validation.
|
25 | *
|
26 | * @deprecated componentWillReceiveProps is deprecated in React 16.9; use AbstractComponent2 instead
|
27 | */
|
28 | var AbstractComponent = /** @class */ (function (_super) {
|
29 | tslib_1.__extends(AbstractComponent, _super);
|
30 | function AbstractComponent(props, context) {
|
31 | var _this = _super.call(this, props, context) || this;
|
32 | // Not bothering to remove entries when their timeouts finish because clearing invalid ID is a no-op
|
33 | _this.timeoutIds = [];
|
34 | /**
|
35 | * Clear all known timeouts.
|
36 | */
|
37 | _this.clearTimeouts = function () {
|
38 | if (_this.timeoutIds.length > 0) {
|
39 | for (var _i = 0, _a = _this.timeoutIds; _i < _a.length; _i++) {
|
40 | var timeoutId = _a[_i];
|
41 | window.clearTimeout(timeoutId);
|
42 | }
|
43 | _this.timeoutIds = [];
|
44 | }
|
45 | };
|
46 | if (!utils_1.isNodeEnv("production")) {
|
47 | _this.validateProps(_this.props);
|
48 | }
|
49 | return _this;
|
50 | }
|
51 | AbstractComponent.prototype.componentWillReceiveProps = function (nextProps) {
|
52 | if (!utils_1.isNodeEnv("production")) {
|
53 | this.validateProps(nextProps);
|
54 | }
|
55 | };
|
56 | AbstractComponent.prototype.componentWillUnmount = function () {
|
57 | this.clearTimeouts();
|
58 | };
|
59 | /**
|
60 | * Set a timeout and remember its ID.
|
61 | * All stored timeouts will be cleared when component unmounts.
|
62 | *
|
63 | * @returns a "cancel" function that will clear timeout when invoked.
|
64 | */
|
65 | AbstractComponent.prototype.setTimeout = function (callback, timeout) {
|
66 | var handle = window.setTimeout(callback, timeout);
|
67 | this.timeoutIds.push(handle);
|
68 | return function () { return window.clearTimeout(handle); };
|
69 | };
|
70 | /**
|
71 | * Ensures that the props specified for a component are valid.
|
72 | * Implementations should check that props are valid and usually throw an Error if they are not.
|
73 | * Implementations should not duplicate checks that the type system already guarantees.
|
74 | *
|
75 | * This method should be used instead of React's
|
76 | * [propTypes](https://facebook.github.io/react/docs/reusable-components.html#prop-validation) feature.
|
77 | * Like propTypes, these runtime checks run only in development mode.
|
78 | */
|
79 | AbstractComponent.prototype.validateProps = function (_) {
|
80 | // implement in subclass
|
81 | };
|
82 | return AbstractComponent;
|
83 | }(React.Component));
|
84 | exports.AbstractComponent = AbstractComponent;
|
85 | //# sourceMappingURL=abstractComponent.js.map |
\ | No newline at end of file |