UNPKG

2.72 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.refHandler = exports.getRef = exports.mergeRefs = exports.combineRefs = exports.setRef = exports.isRefCallback = exports.isRefObject = void 0;
19function isRefObject(value) {
20 return value != null && typeof value !== "function";
21}
22exports.isRefObject = isRefObject;
23function isRefCallback(value) {
24 return typeof value === "function";
25}
26exports.isRefCallback = isRefCallback;
27/**
28 * Assign the given ref to a target, either a React ref object or a callback which takes the ref as its first argument.
29 */
30function setRef(refTarget, ref) {
31 if (isRefObject(refTarget)) {
32 refTarget.current = ref;
33 }
34 else if (isRefCallback(refTarget)) {
35 refTarget(ref);
36 }
37}
38exports.setRef = setRef;
39/** @deprecated use mergeRefs() instead */
40function combineRefs(ref1, ref2) {
41 return mergeRefs(ref1, ref2);
42}
43exports.combineRefs = combineRefs;
44/**
45 * Utility for merging refs into one singular callback ref.
46 * If using in a functional component, would recomend using `useMemo` to preserve function identity.
47 */
48function mergeRefs() {
49 var refs = [];
50 for (var _i = 0; _i < arguments.length; _i++) {
51 refs[_i] = arguments[_i];
52 }
53 return function (value) {
54 refs.forEach(function (ref) {
55 setRef(ref, value);
56 });
57 };
58}
59exports.mergeRefs = mergeRefs;
60function getRef(ref) {
61 var _a;
62 if (ref === null) {
63 return null;
64 }
65 return (_a = ref.current) !== null && _a !== void 0 ? _a : ref;
66}
67exports.getRef = getRef;
68/**
69 * Creates a ref handler which assigns the ref returned by React for a mounted component to a field on the target object.
70 * The target object is usually a component class.
71 *
72 * If provided, it will also update the given `refProp` with the value of the ref.
73 */
74function refHandler(refTargetParent, refTargetKey, refProp) {
75 return function (ref) {
76 refTargetParent[refTargetKey] = ref;
77 setRef(refProp, ref);
78 };
79}
80exports.refHandler = refHandler;
81//# sourceMappingURL=refs.js.map
\No newline at end of file