Skip to content

Commit b896d27

Browse files
committed
add JSDoc
1 parent 35a09fb commit b896d27

File tree

8 files changed

+102
-8
lines changed

8 files changed

+102
-8
lines changed

src/assignRef.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import { ReactRef } from './types';
22

3+
/**
4+
* Assigns a value for a given ref, no matter of the ref format
5+
* @param {RefObject} ref - a callback function or ref object
6+
* @param value - a new value
7+
*
8+
* @see https://github.com/theKashey/use-callback-ref#assignref
9+
* @example
10+
* const refObject = useRef();
11+
* const refFn = (ref) => {....}
12+
*
13+
* assignRef(refObject, "refValue");
14+
* assignRef(refFn, "refValue");
15+
*/
316
export function assignRef<T>(ref: ReactRef<T>, value: T | null): ReactRef<T> {
417
if (typeof ref === 'function') {
518
ref(value);

src/createRef.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import { RefObject, useImperativeHandle } from 'react';
1+
import { RefObject } from 'react';
22

3+
/**
4+
* creates a Ref object with on change callback
5+
* @param callback
6+
* @returns {RefObject}
7+
*
8+
* @see {@link useCallbackRef}
9+
* @see https://reactjs.org/docs/refs-and-the-dom.html#creating-refs
10+
*/
311
export function createCallbackRef<T>(
412
callback: (newValue: T | null, lastValue: T | null) => any
513
): RefObject<T> {

src/mergeRef.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
import * as React from 'react';
2-
import { createCallbackRef } from './createRef';
3-
import { assignRef } from './assignRef';
4-
import { ReactRef } from './types';
1+
import {MutableRefObject} from 'react';
2+
import {createCallbackRef} from './createRef';
3+
import {assignRef} from './assignRef';
4+
import {ReactRef} from './types';
55

6+
/**
7+
* Merges two or more refs together providing a single interface to set their value
8+
* @param {RefObject|Ref} refs
9+
* @returns {MutableRefObject} - a new ref, which translates all changes to {refs}
10+
*
11+
* @see {@link useMergeRefs} to be used in ReactComponents
12+
* @example
13+
* const Component = React.forwardRef((props, ref) => {
14+
* const ownRef = useRef();
15+
* const domRef = mergeRefs([ref, ownRef]); // 👈 merge together
16+
* return <div ref={domRef}>...</div>
17+
* }
18+
*/
619
export function mergeRefs<T>(
720
refs: ReactRef<T>[]
8-
): React.MutableRefObject<T | null> {
21+
): MutableRefObject<T | null> {
922
return createCallbackRef<T>(newValue =>
1023
refs.forEach(ref => assignRef(ref, newValue))
1124
);

src/refToCallback.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import {ReactRef, RefCallback} from './types';
2-
import {useRef, useState} from 'react';
32

3+
/**
4+
* Unmemoized version of {@link useRefToCallback}
5+
* @see {@link useRefToCallback}
6+
* @param ref
7+
*/
48
export function refToCallback<T>(ref: ReactRef<T>): RefCallback<T> {
59
return newValue => {
610
if (typeof ref === 'function') {
@@ -25,6 +29,22 @@ const weakMemoize = (ref: ReactRef<any>) => {
2529
return cb;
2630
};
2731

32+
/**
33+
* Transforms a given `ref` into `callback`.
34+
*
35+
* To transform `callback` into ref use {@link useCallbackRef|useCallbackRef(undefined, callback)}
36+
*
37+
* @param {ReactRef} ref
38+
* @returns {Function}
39+
*
40+
* @see https://github.com/theKashey/use-callback-ref#reftocallback
41+
*
42+
* @example
43+
* const ref = useRef(0);
44+
* const setRef = useRefToCallback(ref);
45+
* 👉 setRef(10);
46+
* ✅ ref.current === 10
47+
*/
2848
export function useRefToCallback<T>(ref: ReactRef<T>): RefCallback<T> {
2949
return weakMemoize(ref);
3050
}

src/useMergeRef.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ import { useCallbackRef } from './useRef';
33
import { assignRef } from './assignRef';
44
import { ReactRef } from './types';
55

6+
/**
7+
* Merges two or more refs together providing a single interface to set their value
8+
* @param {RefObject|Ref} refs
9+
* @returns {MutableRefObject} - a new ref, which translates all changes to {refs}
10+
*
11+
* @see {@link mergeRefs} a version without buit-in memoization
12+
* @see https://github.com/theKashey/use-callback-ref#usemergerefs
13+
* @example
14+
* const Component = React.forwardRef((props, ref) => {
15+
* const ownRef = useRef();
16+
* const domRef = useMergeRefs([ref, ownRef]); // 👈 merge together
17+
* return <div ref={domRef}>...</div>
18+
* }
19+
*/
620
export function useMergeRefs<T>(
721
refs: ReactRef<T>[],
822
defaultValue?: T

src/useRef.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
import { MutableRefObject, useState } from 'react';
22

3+
/**
4+
* creates a MutableRef with ref change callback
5+
* @param initialValue - initial ref value
6+
* @param {Function} callback - a callback to run when value changes
7+
*
8+
* @example
9+
* const ref = useCallbackRef(0, (newValue, oldValue) => console.log(oldValue, '->', newValue);
10+
* ref.current = 1;
11+
* // prints 0 -> 1
12+
*
13+
* @see https://reactjs.org/docs/hooks-reference.html#useref
14+
* @see https://github.com/theKashey/use-callback-ref#usecallbackref---to-replace-reactuseref
15+
* @returns {MutableRefObject}
16+
*/
317
export function useCallbackRef<T>(
418
initialValue: T | null,
519
callback: (newValue: T | null, lastValue: T | null) => void

src/useTransformRef.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ import { ReactRef, RefObject } from './types';
22
import { useCallbackRef } from './useRef';
33
import { assignRef } from './assignRef';
44

5+
/**
6+
* Create a _lense_ on Ref, making it possible to transform ref value
7+
* @param {ReactRef} ref
8+
* @param {Function} transformer. 👉 Ref would be __NOT updated__ on `transformer` update.
9+
* @returns {RefObject}
10+
*
11+
* @see https://github.com/theKashey/use-callback-ref#usetransformref-to-replace-reactuseimperativehandle
12+
* @example
13+
*
14+
* const ResizableWithRef = forwardRef((props, ref) =>
15+
* <Resizable {...props} ref={useTransformRef(ref, i => i ? i.resizable : null)}/>
16+
* );
17+
*/
518
export function useTransformRef<T, K>(
619
ref: ReactRef<K>,
720
transformer: (original: T) => K

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"noImplicitReturns": true,
1111
"noFallthroughCasesInSwitch": true,
1212
"noImplicitAny": true,
13-
"removeComments": true,
1413
"importHelpers": true,
1514
"target": "es2015",
1615
"lib": [

0 commit comments

Comments
 (0)