-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* merge mergeRefs * changeset * remove `useMergeRefs` tests from hooks spec file * deps not depss * lint
- Loading branch information
Showing
5 changed files
with
63 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@leafygreen-ui/hooks': patch | ||
--- | ||
|
||
Merges `useMergeRefs` into `useMergeRef` directory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
import * as React from 'react'; | ||
|
||
/** | ||
* Copied from https://github.com/gregberge/react-merge-refs | ||
* @param refs | ||
* @returns | ||
* Merges an array of refs into a single memoized callback ref or `null`. | ||
*/ | ||
export function useMergeRefs<Instance>( | ||
refs: Array<React.Ref<Instance> | undefined>, | ||
): React.RefCallback<Instance> | null { | ||
return React.useMemo(() => { | ||
if (refs.every(ref => ref == null)) { | ||
return null; | ||
} | ||
|
||
export default function useMergeRefs<T = any>( | ||
refs: Array< | ||
React.MutableRefObject<T> | React.LegacyRef<T> | undefined | null | ||
>, | ||
): React.RefCallback<T> { | ||
return value => { | ||
refs.forEach(ref => { | ||
if (typeof ref === 'function') { | ||
ref(value); | ||
} else if (ref != null) { | ||
(ref as React.MutableRefObject<T | null>).current = value; | ||
} | ||
}); | ||
}; | ||
return value => { | ||
refs.forEach(ref => { | ||
if (typeof ref === 'function') { | ||
ref(value); | ||
} else if (ref != null) { | ||
(ref as React.MutableRefObject<Instance | null>).current = value; | ||
} | ||
}); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, refs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters