UNPKG

3.72 kBJavaScriptView Raw
1/*
2 * Copyright 2020 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 */
16import * as React from "react";
17/**
18 * Returns true if `node` is null/undefined, false, empty string, or an array
19 * composed of those. If `node` is an array, only one level of the array is
20 * checked, for performance reasons.
21 */
22export function isReactNodeEmpty(node, skipArray) {
23 if (skipArray === void 0) { skipArray = false; }
24 return (node == null ||
25 node === "" ||
26 node === false ||
27 (!skipArray &&
28 Array.isArray(node) &&
29 // only recurse one level through arrays, for performance
30 (node.length === 0 || node.every(function (n) { return isReactNodeEmpty(n, true); }))));
31}
32/**
33 * Returns true if children are a mappable children array
34 *
35 * @internal
36 */
37export function isReactChildrenElementOrElements(children) {
38 return !isReactNodeEmpty(children, true) && children !== true;
39}
40/**
41 * Converts a React node to an element: non-empty string or number or
42 * `React.Fragment` (React 16.3+) is wrapped in given tag name; empty strings
43 * and booleans are discarded.
44 */
45export function ensureElement(child, tagName) {
46 if (tagName === void 0) { tagName = "span"; }
47 if (child == null || typeof child === "boolean") {
48 return undefined;
49 }
50 else if (typeof child === "string") {
51 // cull whitespace strings
52 return child.trim().length > 0 ? React.createElement(tagName, {}, child) : undefined;
53 }
54 else if (typeof child === "number" || typeof child.type === "symbol" || Array.isArray(child)) {
55 // React.Fragment has a symbol type, ReactNodeArray extends from Array
56 return React.createElement(tagName, {}, child);
57 }
58 else if (isReactElement(child)) {
59 return child;
60 }
61 else {
62 // child is inferred as {}
63 return undefined;
64 }
65}
66function isReactElement(child) {
67 return (typeof child === "object" &&
68 typeof child.type !== "undefined" &&
69 typeof child.props !== "undefined");
70}
71/**
72 * @deprecated will be removed in 4.0
73 */
74export function getDisplayName(ComponentClass) {
75 return ComponentClass.displayName || ComponentClass.name || "Unknown";
76}
77/**
78 * Returns true if the given JSX element matches the given component type.
79 *
80 * NOTE: This function only checks equality of `displayName` for performance and
81 * to tolerate multiple minor versions of a component being included in one
82 * application bundle.
83 *
84 * @param element JSX element in question
85 * @param ComponentType desired component type of element
86 */
87// eslint-disable-next-line @typescript-eslint/ban-types
88export function isElementOfType(element, ComponentType) {
89 return (element != null &&
90 element.type != null &&
91 element.type.displayName != null &&
92 element.type.displayName === ComponentType.displayName);
93}
94/**
95 * Returns React.createRef if it's available, or a ref-like object if not.
96 *
97 * @deprecated use React.createRef or React.useRef
98 */
99export function createReactRef() {
100 return typeof React.createRef !== "undefined" ? React.createRef() : { current: null };
101}
102//# sourceMappingURL=reactUtils.js.map
\No newline at end of file