@d-matrix/utils 1.17.1 → 1.18.1

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/echarts.js CHANGED
@@ -68,7 +68,7 @@ export function fill(dataSource, xAxisField, yAxisField) {
68
68
  */
69
69
  export const getDiffRate = (first, maxDiff, decimalPlaces = 2, splitNumber = 5) => {
70
70
  let diffRate = 1.2;
71
- if (first === 0)
71
+ if (first === 0 || maxDiff === 0)
72
72
  return diffRate;
73
73
  const minDiff = 1 / Math.pow(10, decimalPlaces);
74
74
  function calc(f, d) {
@@ -6,3 +6,4 @@ export * from './useCopyToClipboard';
6
6
  export * from './enhancedComponent';
7
7
  export * from './useDeepCompareRef';
8
8
  export * from './types';
9
+ export * from './useForwardRef';
@@ -6,3 +6,4 @@ export * from './useCopyToClipboard';
6
6
  export * from './enhancedComponent';
7
7
  export * from './useDeepCompareRef';
8
8
  export * from './types';
9
+ export * from './useForwardRef';
@@ -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/dist/types.d.ts CHANGED
@@ -13,3 +13,5 @@ export declare type FunctionPropertyNames<T> = {
13
13
  export declare type NonFunctionPropertyNames<T> = {
14
14
  [P in keyof T]-?: T[P] extends Function ? never : P;
15
15
  }[keyof T];
16
+ /** 获取对象中key的值,返回由这些值组成的union type */
17
+ export declare type ValueOf<T> = T[keyof T];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
3
  "sideEffects": false,
4
- "version": "1.17.1",
4
+ "version": "1.18.1",
5
5
  "description": "A dozen of utils for Front-End Development",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {
package/readme.md CHANGED
@@ -99,7 +99,7 @@ const Test = () => {
99
99
 
100
100
  - `EnhancedComponent.prototype.setStateAsync(state)`
101
101
 
102
- setState 方法的同步版本
102
+ `setState()`方法的同步版本
103
103
 
104
104
  ```ts
105
105
  import { react } from '@d-matrix/utils';
@@ -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`
@@ -288,6 +302,24 @@ const t1 = {
288
302
  type T1 = FunctionPropertyNames<typeof t1>; // 'result'
289
303
  ```
290
304
 
305
+ - `ValueOf<T>`
306
+
307
+ 获取对象中`key`的值,返回由这些值组成的union type
308
+
309
+ ```ts
310
+ const map = {
311
+ 0: '0m',
312
+ 1: '1m',
313
+ 2: '2m',
314
+ 3: '3m',
315
+ 4: '4m',
316
+ 5: '5m',
317
+ 6: '6m',
318
+ } as const;
319
+
320
+ type T0 = ValueOf<typeof map>; // '0m' | '1m' | '2m' | '3m' | '4m' | '5m' | '6m'
321
+ ```
322
+
291
323
  ### algorithm
292
324
 
293
325
  - `moveMulti<T extends unknown>(arr: T[], indexes: number[], start: number): T[]`