aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/refs.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/helpers/refs.ts')
-rw-r--r--src/utils/helpers/refs.ts16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/utils/helpers/refs.ts b/src/utils/helpers/refs.ts
new file mode 100644
index 0000000..74a695a
--- /dev/null
+++ b/src/utils/helpers/refs.ts
@@ -0,0 +1,16 @@
+import type { LegacyRef, MutableRefObject, RefCallback } from 'react';
+import type { Nullable } from '../../types';
+
+export const mergeRefs =
+ <T = unknown>(
+ refs: (MutableRefObject<T> | LegacyRef<T> | undefined | null)[]
+ ): RefCallback<T> =>
+ (value) => {
+ refs.forEach((ref) => {
+ if (typeof ref === 'function') {
+ ref(value);
+ } else if (ref !== null) {
+ (ref as MutableRefObject<Nullable<T>>).current = value;
+ }
+ });
+ };