UNPKG

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