-
Notifications
You must be signed in to change notification settings - Fork 33
feat: Allow ref callback for Chart and ChartPanel #2174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
| import React from 'react'; | ||
| import { mergeRefs } from './mergeRefs'; | ||
|
|
||
| describe('mergeRefs', () => { | ||
| test('merges ref objects', () => { | ||
| const refA = React.createRef(); | ||
| const refB = React.createRef(); | ||
| const mergedRef = mergeRefs(refA, refB); | ||
|
|
||
| const refValue = {}; | ||
| mergedRef(refValue); | ||
| expect(refA.current).toBe(refValue); | ||
| expect(refB.current).toBe(refValue); | ||
| }); | ||
|
|
||
| test('merges ref callbacks', () => { | ||
| const refA: React.RefCallback<unknown> = jest.fn(); | ||
| const refB: React.RefCallback<unknown> = jest.fn(); | ||
| const mergedRef = mergeRefs(refA, refB); | ||
|
|
||
| const refValue = {}; | ||
| mergedRef(refValue); | ||
| expect(refA).toHaveBeenCalledWith(refValue); | ||
| expect(refB).toHaveBeenCalledWith(refValue); | ||
| }); | ||
|
|
||
| test('ignores null/undefined refs', () => { | ||
| const refA = React.createRef(); | ||
| const refB: React.RefCallback<unknown> = jest.fn(); | ||
| const refC = null; | ||
| const refD = undefined; | ||
| const mergedRef = mergeRefs(refA, refB, refC, refD); | ||
|
|
||
| const refValue = {}; | ||
| mergedRef(refValue); | ||
| expect(refA.current).toBe(refValue); | ||
| expect(refB).toHaveBeenCalledWith(refValue); | ||
| expect(refC).toBe(null); | ||
| expect(refD).toBe(undefined); | ||
| }); | ||
| }); |
This file contains hidden or 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,31 @@ | ||
| import type React 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: Array< | ||
| React.MutableRefObject<T> | React.LegacyRef<T> | null | undefined | ||
| > | ||
| ): React.RefCallback<T> { | ||
| return value => { | ||
| refs.forEach(ref => { | ||
| if (ref != null) { | ||
| if (typeof ref === 'function') { | ||
| ref(value); | ||
| } 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 = value; | ||
| } | ||
| } | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| export default mergeRefs; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.