@d-matrix/utils 1.17.1 → 1.18.0
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/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +1 -0
- package/dist/react/types.d.ts +2 -1
- package/dist/react/useForwardRef.d.ts +9 -0
- package/dist/react/useForwardRef.js +22 -0
- package/package.json +1 -1
- package/readme.md +14 -0
package/dist/react/index.d.ts
CHANGED
package/dist/react/index.js
CHANGED
package/dist/react/types.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import type { ForwardRefExoticComponent } from 'react';
|
|
1
|
+
import type { ForwardRefExoticComponent, NamedExoticComponent, ComponentPropsWithoutRef, ComponentPropsWithRef, RefAttributes, ElementType } from 'react';
|
|
2
|
+
export declare type ComponentRef<T extends ElementType> = T extends NamedExoticComponent<ComponentPropsWithoutRef<T> & RefAttributes<infer Method>> ? Method : ComponentPropsWithRef<T> extends RefAttributes<infer Method> ? Method : never;
|
|
2
3
|
export declare type InferRef<T> = T extends ForwardRefExoticComponent<infer Ref> ? Ref extends React.RefAttributes<infer RefElement> ? RefElement : never : never;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ForwardedRef } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* https://stackoverflow.com/questions/66060217/i-cant-type-the-ref-correctly-using-useref-hook-in-typescript
|
|
4
|
+
* 解决 Property 'current' does not exist on type '(instance: HTMLInputElement | null) => void' TS类型错误
|
|
5
|
+
* @param ref
|
|
6
|
+
* @param initialValue
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export declare const useForwardRef: <T>(ref: ForwardedRef<T>, initialValue?: any) => import("react").MutableRefObject<T>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* https://stackoverflow.com/questions/66060217/i-cant-type-the-ref-correctly-using-useref-hook-in-typescript
|
|
4
|
+
* 解决 Property 'current' does not exist on type '(instance: HTMLInputElement | null) => void' TS类型错误
|
|
5
|
+
* @param ref
|
|
6
|
+
* @param initialValue
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export const useForwardRef = (ref, initialValue = null) => {
|
|
10
|
+
const targetRef = useRef(initialValue);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
if (!ref)
|
|
13
|
+
return;
|
|
14
|
+
if (typeof ref === 'function') {
|
|
15
|
+
ref(targetRef.current);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
ref.current = targetRef.current;
|
|
19
|
+
}
|
|
20
|
+
}, [ref]);
|
|
21
|
+
return targetRef;
|
|
22
|
+
};
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -164,6 +164,20 @@ const Parent = () => {
|
|
|
164
164
|
};
|
|
165
165
|
```
|
|
166
166
|
|
|
167
|
+
- `useForwardRef = <T>(ref: ForwardedRef<T>, initialValue: any = null): React.MutableRefObject<T>`
|
|
168
|
+
|
|
169
|
+
解决使用`React.forwardRef`后,在调用`ref.current.someMethod()`时, 出现`Property 'current' does not exist on type '(instance: HTMLInputElement | null) => void'` TS类型错误,具体问题见[这里](https://stackoverflow.com/questions/66060217/i-cant-type-the-ref-correctly-using-useref-hook-in-typescript)
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
const Input = React.forwardRef<HTMLInputElement, React.ComponentPropsWithRef<'input'>>((props, ref) => {
|
|
173
|
+
const forwardRef = useForwardRef<HTMLInputElement>(ref);
|
|
174
|
+
useEffect(() => {
|
|
175
|
+
forwardRef.current.focus();
|
|
176
|
+
});
|
|
177
|
+
return <input type="text" ref={forwardRef} value={props.value} />;
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
167
181
|
### dom
|
|
168
182
|
|
|
169
183
|
- `scrollToTop(element: Element | null | undefined): void`
|