Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
111 additions
and
15 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
dist | ||
dist | ||
coverage |
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,10 @@ | ||
import {ReactRef} from "./types"; | ||
|
||
export function assignRef<T>(ref: ReactRef<T>, value: T | null): ReactRef<T> { | ||
if (typeof ref === 'function') { | ||
ref(value) | ||
} else if (ref != null) { | ||
ref.current = value | ||
} | ||
return ref; | ||
} |
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,3 +1,5 @@ | ||
export {useCallbackRef} from './useRef'; | ||
export {createCallbackRef} from './createRef'; | ||
export {mergeRefs} from './mergeRef' | ||
export {mergeRefs} from './mergeRef' | ||
export {assignRef} from './assignRef'; | ||
export {transformRef} from './transformRef'; |
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,19 +1,14 @@ | ||
import * as React from 'react'; | ||
import {useCallbackRef} from "./useRef"; | ||
import {assignRef} from "./assignRef"; | ||
|
||
type ReactRef<T> = ( | ||
((newValue: T | null) => void) | | ||
React.MutableRefObject<T | null> | ||
); | ||
|
||
export function mergeRefs<T>(refs: ReactRef<T>[], defaultValue?: T): React.MutableRefObject<T | null> { | ||
return useCallbackRef<T>(defaultValue, newValue => { | ||
refs.forEach(ref => { | ||
if (typeof ref === 'function') { | ||
ref(newValue) | ||
} else if (ref != null) { | ||
ref.current = newValue | ||
} | ||
}) | ||
}); | ||
return useCallbackRef<T>(defaultValue, newValue => ( | ||
refs.forEach(ref => assignRef(ref, newValue)) | ||
)); | ||
} |
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,11 @@ | ||
import {ReactRef, RefCallback} from "./types"; | ||
|
||
export function retToCallback<T>(ref: ReactRef<T>): RefCallback<T> { | ||
return newValue => { | ||
if (typeof ref === 'function') { | ||
ref(newValue) | ||
} else if (ref != null) { | ||
ref.current = newValue | ||
} | ||
} | ||
} |
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,10 @@ | ||
import {ReactRef, RefObject} from "./types"; | ||
import {useCallbackRef} from "./useRef"; | ||
import {assignRef} from "./assignRef"; | ||
|
||
export function transformRef<T, K>(ref: ReactRef<K>, transformer: (original: T) => K): RefObject<T> { | ||
return useCallbackRef<T>( | ||
undefined, | ||
value => assignRef(ref, transformer(value)) | ||
) | ||
} |
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,6 @@ | ||
import * as React from "react"; | ||
|
||
export type RefCallback<T> = ((newValue: T | null) => void); | ||
export type RefObject<T> = React.MutableRefObject<T | null>; | ||
|
||
export type ReactRef<T> = RefCallback<T> | RefObject<T>; |
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