@slimr/react 2.1.45 → 2.1.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -49
- package/cjs/index.d.ts +1 -1
- package/cjs/index.js +1 -1
- package/cjs/index.js.map +1 -1
- package/cjs/index.ts +1 -1
- package/cjs/merge-refs.d.ts +16 -0
- package/cjs/merge-refs.js +32 -0
- package/cjs/merge-refs.js.map +1 -0
- package/cjs/merge-refs.ts +27 -0
- package/esm/index.d.ts +1 -1
- package/esm/index.js +1 -1
- package/esm/index.js.map +1 -1
- package/esm/index.ts +1 -1
- package/esm/merge-refs.d.ts +16 -0
- package/esm/merge-refs.js +28 -0
- package/esm/merge-refs.js.map +1 -0
- package/esm/merge-refs.ts +27 -0
- package/package.json +7 -4
- package/src/index.ts +1 -1
- package/src/merge-refs.ts +27 -0
package/README.md
CHANGED
|
@@ -4,17 +4,7 @@ A collection of useful 1st and third party react components, hooks, and util. In
|
|
|
4
4
|
|
|
5
5
|
## Context
|
|
6
6
|
|
|
7
|
-
`@slimr` is a set of slim React (hence '@slimr') libs
|
|
8
|
-
|
|
9
|
-
- [@slimr/css](https://www.npmjs.com/package/@slimr/css) - Framework agnostic css-in-js features inspired by the popular Emotion lib
|
|
10
|
-
- [@slimr/forms](https://www.npmjs.com/package/@slimr/forms) - A minimalistic form hook
|
|
11
|
-
- [@slimr/hooks](https://www.npmjs.com/package/@slimr/hooks) - A collection of useful 1st and third party react hooks
|
|
12
|
-
- [@slimr/markdown](https://www.npmjs.com/package/@slimr/markdown) - A simple component and slim markdown-to-html parser
|
|
13
|
-
- [@slimr/mdi-paths](https://www.npmjs.com/package/@slimr/mdi-paths) - A basic Icon component and Material Design icon svg paths, code-split by path.
|
|
14
|
-
- [@slimr/router](https://www.npmjs.com/package/@slimr/router) - A novel React-web router that supports stack routing
|
|
15
|
-
- [@slimr/styled](https://www.npmjs.com/package/@slimr/styled) - css-in-js features inspired by the popular styled-components and Chakra-UI libs
|
|
16
|
-
- [@slimr/swr](https://www.npmjs.com/package/@slimr/swr) - A React hook for fetching data that supports stale-while-refresh eager rendering
|
|
17
|
-
- [@slimr/util](https://www.npmjs.com/package/@slimr/util) - Framework agnostic Javascript polyfills
|
|
7
|
+
`@slimr` is a set of slim React (hence '@slimr') libs. Check them all out on [github](https://github.com/bdombro/slimr)!
|
|
18
8
|
|
|
19
9
|
## Setup
|
|
20
10
|
|
|
@@ -36,55 +26,29 @@ export default defineConfig({
|
|
|
36
26
|
### Bundled from other libs
|
|
37
27
|
|
|
38
28
|
- [@slimr/forms](https://www.npmjs.com/package/@slimr/forms) - A minimalistic form hook
|
|
39
|
-
- [@slimr/hooks](https://www.npmjs.com/package/@slimr/hooks) - A collection of useful 1st and third party react hooks
|
|
40
29
|
- [@slimr/markdown](https://www.npmjs.com/package/@slimr/markdown) - A simple component and slim markdown-to-html parser
|
|
41
30
|
- [@slimr/router](https://www.npmjs.com/package/@slimr/router) - A novel React-web router that supports stack routing
|
|
42
31
|
- [@slimr/styled](https://www.npmjs.com/package/@slimr/styled) - css-in-js features inspired by the popular styled-components and Chakra-UI libs
|
|
43
32
|
- [@slimr/swr](https://www.npmjs.com/package/@slimr/swr) - A React hook for fetching data that supports stale-while-refresh eager rendering
|
|
44
|
-
- [@slimr/util](https://www.npmjs.com/package/@slimr/util) - Framework agnostic Javascript polyfills
|
|
45
33
|
- [react-use](https://www.npmjs.com/package/react-use) - an excellent collection of hooks
|
|
46
34
|
|
|
47
|
-
###
|
|
35
|
+
### mergeRefs
|
|
48
36
|
|
|
49
|
-
|
|
37
|
+
Merge React refs so that multiple refs can be used on a single element. Is
|
|
38
|
+
used to merge refs from a forwardRef and a local ref from useRef.
|
|
50
39
|
|
|
51
|
-
|
|
40
|
+
Credits: react-merge-refs
|
|
52
41
|
|
|
53
|
-
|
|
42
|
+
```typescript
|
|
43
|
+
const MyComponent = forwardRef((props, ref1) => {
|
|
44
|
+
const ref2 = useRef(null)
|
|
45
|
+
return (<div ref={mergeRefs([ref1, ref2])} />)
|
|
46
|
+
})
|
|
47
|
+
```
|
|
54
48
|
|
|
55
|
-
|
|
56
|
-
import {FormError, useForm} from '@slimr/forms'
|
|
57
|
-
import {formToValues} from '@slimr/util'
|
|
58
|
-
|
|
59
|
-
function MyForm() {
|
|
60
|
-
const { Form, submitting, submitted, accepted, errors} = useForm()
|
|
61
|
-
|
|
62
|
-
const onSubmit = async (e: React.FormEventHandler<HTMLFormElement> => {
|
|
63
|
-
const vals = formToJson(e.target as HTMLFormElement)
|
|
64
|
-
const errors: Record<string, string> = {}
|
|
65
|
-
if (!vals.name) {
|
|
66
|
-
errors.name = 'Name is required'
|
|
67
|
-
}
|
|
68
|
-
if (!vals.terms) {
|
|
69
|
-
errors.checkbox = 'You must agree to the terms'
|
|
70
|
-
}
|
|
71
|
-
if (Object.keys(errors).length) {
|
|
72
|
-
throw new FormError(errors)
|
|
73
|
-
}
|
|
74
|
-
}
|
|
49
|
+
### useDeepCompareMemo and useShallowCompareMemo
|
|
75
50
|
|
|
76
|
-
|
|
77
|
-
<Form onSubmit={onSubmit}>
|
|
78
|
-
<input disabled={submitting || accepted} name="name" />
|
|
79
|
-
<div>{errors.name}<div>
|
|
80
|
-
<input disabled={submitting || accepted} name="terms" type="checkbox" />
|
|
81
|
-
<div>{errors.terms}<div>
|
|
82
|
-
<button type="submit">Submit</button>
|
|
83
|
-
<button type="reset">Reset</button>
|
|
84
|
-
</Form>
|
|
85
|
-
)
|
|
86
|
-
}
|
|
87
|
-
```
|
|
51
|
+
like react-use's useDeepEffects, but for memos
|
|
88
52
|
|
|
89
53
|
### useSet2
|
|
90
54
|
|
package/cjs/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export * from '@slimr/markdown';
|
|
|
3
3
|
export * from '@slimr/router';
|
|
4
4
|
export * from '@slimr/styled';
|
|
5
5
|
export * from '@slimr/swr';
|
|
6
|
-
export * from '@slimr/util';
|
|
7
6
|
export * from 'react-use';
|
|
7
|
+
export * from './merge-refs.js';
|
|
8
8
|
export * from './useMemos.js';
|
|
9
9
|
export * from './useSet2.js';
|
package/cjs/index.js
CHANGED
|
@@ -19,8 +19,8 @@ __exportStar(require("@slimr/markdown"), exports);
|
|
|
19
19
|
__exportStar(require("@slimr/router"), exports);
|
|
20
20
|
__exportStar(require("@slimr/styled"), exports);
|
|
21
21
|
__exportStar(require("@slimr/swr"), exports);
|
|
22
|
-
__exportStar(require("@slimr/util"), exports);
|
|
23
22
|
__exportStar(require("react-use"), exports);
|
|
23
|
+
__exportStar(require("./merge-refs.js"), exports);
|
|
24
24
|
__exportStar(require("./useMemos.js"), exports);
|
|
25
25
|
__exportStar(require("./useSet2.js"), exports);
|
|
26
26
|
//# sourceMappingURL=index.js.map
|
package/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA4B;AAC5B,kDAA+B;AAC/B,gDAA6B;AAC7B,gDAA6B;AAC7B,6CAA0B;AAC1B,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA4B;AAC5B,kDAA+B;AAC/B,gDAA6B;AAC7B,gDAA6B;AAC7B,6CAA0B;AAC1B,4CAAyB;AAEzB,kDAA+B;AAC/B,gDAA6B;AAC7B,+CAA4B"}
|
package/cjs/index.ts
CHANGED
|
@@ -3,8 +3,8 @@ export * from '@slimr/markdown'
|
|
|
3
3
|
export * from '@slimr/router'
|
|
4
4
|
export * from '@slimr/styled'
|
|
5
5
|
export * from '@slimr/swr'
|
|
6
|
-
export * from '@slimr/util'
|
|
7
6
|
export * from 'react-use'
|
|
8
7
|
|
|
8
|
+
export * from './merge-refs.js'
|
|
9
9
|
export * from './useMemos.js'
|
|
10
10
|
export * from './useSet2.js'
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
/**
|
|
3
|
+
* Merge React refs so that multiple refs can be used on a single element. Is
|
|
4
|
+
* used to merge refs from a forwardRef and a local ref from useRef.
|
|
5
|
+
*
|
|
6
|
+
* Credits: react-merge-refs
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const MyComponent = forwardRef((props, ref1) => {
|
|
11
|
+
* const ref2 = useRef(null)
|
|
12
|
+
* return (<div ref={mergeRefs([ref1, ref2])} />)
|
|
13
|
+
* })
|
|
14
|
+
* ```
|
|
15
|
+
**/
|
|
16
|
+
export declare function mergeRefs<T = any>(refs: Array<React.MutableRefObject<T> | React.LegacyRef<T>>): React.RefCallback<T>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mergeRefs = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Merge React refs so that multiple refs can be used on a single element. Is
|
|
6
|
+
* used to merge refs from a forwardRef and a local ref from useRef.
|
|
7
|
+
*
|
|
8
|
+
* Credits: react-merge-refs
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const MyComponent = forwardRef((props, ref1) => {
|
|
13
|
+
* const ref2 = useRef(null)
|
|
14
|
+
* return (<div ref={mergeRefs([ref1, ref2])} />)
|
|
15
|
+
* })
|
|
16
|
+
* ```
|
|
17
|
+
**/
|
|
18
|
+
function mergeRefs(refs) {
|
|
19
|
+
return value => {
|
|
20
|
+
refs.forEach(ref => {
|
|
21
|
+
if (typeof ref === 'function') {
|
|
22
|
+
ref(value);
|
|
23
|
+
}
|
|
24
|
+
else if (ref != null) {
|
|
25
|
+
;
|
|
26
|
+
ref.current = value;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
exports.mergeRefs = mergeRefs;
|
|
32
|
+
//# sourceMappingURL=merge-refs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge-refs.js","sourceRoot":"","sources":["../src/merge-refs.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;IAaI;AACJ,SAAgB,SAAS,CACvB,IAA2D;IAE3D,OAAO,KAAK,CAAC,EAAE;QACb,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACjB,IAAI,OAAO,GAAG,KAAK,UAAU,EAAE;gBAC7B,GAAG,CAAC,KAAK,CAAC,CAAA;aACX;iBAAM,IAAI,GAAG,IAAI,IAAI,EAAE;gBACtB,CAAC;gBAAC,GAAwC,CAAC,OAAO,GAAG,KAAK,CAAA;aAC3D;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;AACH,CAAC;AAZD,8BAYC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merge React refs so that multiple refs can be used on a single element. Is
|
|
3
|
+
* used to merge refs from a forwardRef and a local ref from useRef.
|
|
4
|
+
*
|
|
5
|
+
* Credits: react-merge-refs
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const MyComponent = forwardRef((props, ref1) => {
|
|
10
|
+
* const ref2 = useRef(null)
|
|
11
|
+
* return (<div ref={mergeRefs([ref1, ref2])} />)
|
|
12
|
+
* })
|
|
13
|
+
* ```
|
|
14
|
+
**/
|
|
15
|
+
export function mergeRefs<T = any>(
|
|
16
|
+
refs: Array<React.MutableRefObject<T> | React.LegacyRef<T>>
|
|
17
|
+
): React.RefCallback<T> {
|
|
18
|
+
return value => {
|
|
19
|
+
refs.forEach(ref => {
|
|
20
|
+
if (typeof ref === 'function') {
|
|
21
|
+
ref(value)
|
|
22
|
+
} else if (ref != null) {
|
|
23
|
+
;(ref as React.MutableRefObject<T | null>).current = value
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
}
|
package/esm/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export * from '@slimr/markdown';
|
|
|
3
3
|
export * from '@slimr/router';
|
|
4
4
|
export * from '@slimr/styled';
|
|
5
5
|
export * from '@slimr/swr';
|
|
6
|
-
export * from '@slimr/util';
|
|
7
6
|
export * from 'react-use';
|
|
7
|
+
export * from './merge-refs.js';
|
|
8
8
|
export * from './useMemos.js';
|
|
9
9
|
export * from './useSet2.js';
|
package/esm/index.js
CHANGED
|
@@ -3,8 +3,8 @@ export * from '@slimr/markdown';
|
|
|
3
3
|
export * from '@slimr/router';
|
|
4
4
|
export * from '@slimr/styled';
|
|
5
5
|
export * from '@slimr/swr';
|
|
6
|
-
export * from '@slimr/util';
|
|
7
6
|
export * from 'react-use';
|
|
7
|
+
export * from './merge-refs.js';
|
|
8
8
|
export * from './useMemos.js';
|
|
9
9
|
export * from './useSet2.js';
|
|
10
10
|
//# sourceMappingURL=index.js.map
|
package/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AAEzB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA"}
|
package/esm/index.ts
CHANGED
|
@@ -3,8 +3,8 @@ export * from '@slimr/markdown'
|
|
|
3
3
|
export * from '@slimr/router'
|
|
4
4
|
export * from '@slimr/styled'
|
|
5
5
|
export * from '@slimr/swr'
|
|
6
|
-
export * from '@slimr/util'
|
|
7
6
|
export * from 'react-use'
|
|
8
7
|
|
|
8
|
+
export * from './merge-refs.js'
|
|
9
9
|
export * from './useMemos.js'
|
|
10
10
|
export * from './useSet2.js'
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/// <reference types="react" resolution-mode="require"/>
|
|
2
|
+
/**
|
|
3
|
+
* Merge React refs so that multiple refs can be used on a single element. Is
|
|
4
|
+
* used to merge refs from a forwardRef and a local ref from useRef.
|
|
5
|
+
*
|
|
6
|
+
* Credits: react-merge-refs
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const MyComponent = forwardRef((props, ref1) => {
|
|
11
|
+
* const ref2 = useRef(null)
|
|
12
|
+
* return (<div ref={mergeRefs([ref1, ref2])} />)
|
|
13
|
+
* })
|
|
14
|
+
* ```
|
|
15
|
+
**/
|
|
16
|
+
export declare function mergeRefs<T = any>(refs: Array<React.MutableRefObject<T> | React.LegacyRef<T>>): React.RefCallback<T>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merge React refs so that multiple refs can be used on a single element. Is
|
|
3
|
+
* used to merge refs from a forwardRef and a local ref from useRef.
|
|
4
|
+
*
|
|
5
|
+
* Credits: react-merge-refs
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const MyComponent = forwardRef((props, ref1) => {
|
|
10
|
+
* const ref2 = useRef(null)
|
|
11
|
+
* return (<div ref={mergeRefs([ref1, ref2])} />)
|
|
12
|
+
* })
|
|
13
|
+
* ```
|
|
14
|
+
**/
|
|
15
|
+
export function mergeRefs(refs) {
|
|
16
|
+
return value => {
|
|
17
|
+
refs.forEach(ref => {
|
|
18
|
+
if (typeof ref === 'function') {
|
|
19
|
+
ref(value);
|
|
20
|
+
}
|
|
21
|
+
else if (ref != null) {
|
|
22
|
+
;
|
|
23
|
+
ref.current = value;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=merge-refs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge-refs.js","sourceRoot":"","sources":["../src/merge-refs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;IAaI;AACJ,MAAM,UAAU,SAAS,CACvB,IAA2D;IAE3D,OAAO,KAAK,CAAC,EAAE;QACb,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACjB,IAAI,OAAO,GAAG,KAAK,UAAU,EAAE;gBAC7B,GAAG,CAAC,KAAK,CAAC,CAAA;aACX;iBAAM,IAAI,GAAG,IAAI,IAAI,EAAE;gBACtB,CAAC;gBAAC,GAAwC,CAAC,OAAO,GAAG,KAAK,CAAA;aAC3D;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merge React refs so that multiple refs can be used on a single element. Is
|
|
3
|
+
* used to merge refs from a forwardRef and a local ref from useRef.
|
|
4
|
+
*
|
|
5
|
+
* Credits: react-merge-refs
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const MyComponent = forwardRef((props, ref1) => {
|
|
10
|
+
* const ref2 = useRef(null)
|
|
11
|
+
* return (<div ref={mergeRefs([ref1, ref2])} />)
|
|
12
|
+
* })
|
|
13
|
+
* ```
|
|
14
|
+
**/
|
|
15
|
+
export function mergeRefs<T = any>(
|
|
16
|
+
refs: Array<React.MutableRefObject<T> | React.LegacyRef<T>>
|
|
17
|
+
): React.RefCallback<T> {
|
|
18
|
+
return value => {
|
|
19
|
+
refs.forEach(ref => {
|
|
20
|
+
if (typeof ref === 'function') {
|
|
21
|
+
ref(value)
|
|
22
|
+
} else if (ref != null) {
|
|
23
|
+
;(ref as React.MutableRefObject<T | null>).current = value
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@slimr/react",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.47",
|
|
4
4
|
"author": "Brian Dombrowski",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"private": false,
|
|
@@ -32,9 +32,12 @@
|
|
|
32
32
|
"start": "nodemon -w src -e '*' -x 'npm run build && cd ../demo && npm start'"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@slimr/forms": "^4.1.
|
|
36
|
-
"@slimr/
|
|
37
|
-
"@slimr/
|
|
35
|
+
"@slimr/forms": "^4.1.46",
|
|
36
|
+
"@slimr/markdown": "^2.1.35",
|
|
37
|
+
"@slimr/router": "^2.1.51",
|
|
38
|
+
"@slimr/styled": "^2.1.42",
|
|
39
|
+
"@slimr/swr": "^2.1.34",
|
|
40
|
+
"@slimr/util": "^3.2.38",
|
|
38
41
|
"react-use": "17"
|
|
39
42
|
},
|
|
40
43
|
"peerDependencies": {
|
package/src/index.ts
CHANGED
|
@@ -3,8 +3,8 @@ export * from '@slimr/markdown'
|
|
|
3
3
|
export * from '@slimr/router'
|
|
4
4
|
export * from '@slimr/styled'
|
|
5
5
|
export * from '@slimr/swr'
|
|
6
|
-
export * from '@slimr/util'
|
|
7
6
|
export * from 'react-use'
|
|
8
7
|
|
|
8
|
+
export * from './merge-refs.js'
|
|
9
9
|
export * from './useMemos.js'
|
|
10
10
|
export * from './useSet2.js'
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merge React refs so that multiple refs can be used on a single element. Is
|
|
3
|
+
* used to merge refs from a forwardRef and a local ref from useRef.
|
|
4
|
+
*
|
|
5
|
+
* Credits: react-merge-refs
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const MyComponent = forwardRef((props, ref1) => {
|
|
10
|
+
* const ref2 = useRef(null)
|
|
11
|
+
* return (<div ref={mergeRefs([ref1, ref2])} />)
|
|
12
|
+
* })
|
|
13
|
+
* ```
|
|
14
|
+
**/
|
|
15
|
+
export function mergeRefs<T = any>(
|
|
16
|
+
refs: Array<React.MutableRefObject<T> | React.LegacyRef<T>>
|
|
17
|
+
): React.RefCallback<T> {
|
|
18
|
+
return value => {
|
|
19
|
+
refs.forEach(ref => {
|
|
20
|
+
if (typeof ref === 'function') {
|
|
21
|
+
ref(value)
|
|
22
|
+
} else if (ref != null) {
|
|
23
|
+
;(ref as React.MutableRefObject<T | null>).current = value
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
}
|