mirror of
https://github.com/Sonarr/Sonarr
synced 2026-05-08 13:01:10 +02:00
Add useCombinedRefs hook
This commit is contained in:
parent
710a6ea078
commit
6d3ef494b4
1 changed files with 39 additions and 0 deletions
39
frontend/src/Helpers/Hooks/useCombinedRefs.ts
Normal file
39
frontend/src/Helpers/Hooks/useCombinedRefs.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import { ForwardedRef, useCallback, useRef } from 'react';
|
||||
|
||||
type OptionalRef<T> = ForwardedRef<T> | undefined;
|
||||
|
||||
function setRef<T>(ref: OptionalRef<T>, value: T | null) {
|
||||
if (typeof ref === 'function') {
|
||||
ref(value);
|
||||
} else if (ref) {
|
||||
ref.current = value;
|
||||
}
|
||||
}
|
||||
|
||||
function useCombinedRefs<T>(...refs: OptionalRef<T>[]) {
|
||||
const previousRefs = useRef<OptionalRef<T>[]>([]);
|
||||
|
||||
return useCallback((value: T | null) => {
|
||||
let index = 0;
|
||||
for (; index < refs.length; index++) {
|
||||
const ref = refs[index];
|
||||
const prev = previousRefs.current[index];
|
||||
|
||||
if (prev !== ref) {
|
||||
setRef(prev, null);
|
||||
}
|
||||
setRef(ref, value);
|
||||
}
|
||||
|
||||
for (; index < previousRefs.current.length; index++) {
|
||||
const prev = previousRefs.current[index];
|
||||
setRef(prev, null);
|
||||
}
|
||||
|
||||
previousRefs.current = refs;
|
||||
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, refs);
|
||||
}
|
||||
|
||||
export default useCombinedRefs;
|
||||
Loading…
Reference in a new issue