-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow ref callback for Chart and ChartPanel (#2174)
Needed for deephaven/deephaven-plugins#657 because when using ChartPanel `containerRef` is undefined on first render due to the loading logic. This means we need to allow a ref callback so we can use a callback to set state in the plugin and add event listeners to the container when it actually exists
- Loading branch information
1 parent
7191d7a
commit 003583a
Showing
8 changed files
with
106 additions
and
37 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
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 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
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 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 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,43 @@ | ||
import { | ||
type LegacyRef, | ||
type MutableRefObject, | ||
type Ref, | ||
type RefCallback, | ||
useMemo, | ||
} from 'react'; | ||
|
||
/** | ||
* Merge multiple react refs into a single ref callback. | ||
* This can be used to merge callback and object refs into a single ref. | ||
* Merged callback refs will be called while object refs will have their current property set. | ||
* @param refs The refs to merge | ||
* @returns A ref callback that will set the value on all refs | ||
*/ | ||
export function mergeRefs<T = unknown>( | ||
...refs: readonly (MutableRefObject<T> | LegacyRef<T> | null | undefined)[] | ||
): RefCallback<T> { | ||
return newRef => { | ||
refs.forEach(ref => { | ||
if (ref != null) { | ||
if (typeof ref === 'function') { | ||
ref(newRef); | ||
} else { | ||
// React marks RefObject as readonly, but it's just to indicate React manages it | ||
// We can still write to its current value | ||
// eslint-disable-next-line no-param-reassign | ||
(ref as React.MutableRefObject<T | null>).current = newRef; | ||
} | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
/** | ||
* Merges multiple refs into one ref that can be assigned to the component. | ||
* In turn all the refs passed in will be assigned when the ref returned is assigned. | ||
* @param refs Array of refs to assign | ||
*/ | ||
export function useMergeRef<T>(...refs: readonly Ref<T>[]): RefCallback<T> { | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
return useMemo(() => mergeRefs(...refs), refs); | ||
} |