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