@d-matrix/utils 1.17.0 → 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/echarts.d.ts CHANGED
@@ -17,17 +17,20 @@ export declare function fill<T extends Record<string, any>, XAxisField extends k
17
17
  * 判断max-min是否被5等分(是否小于0.01),小于0.01表示不能被5等分,基础倍率增加0.1后,再次判断,直到被5等分
18
18
  * @param first
19
19
  * @param maxDiff
20
+ * @param decimalPlaces
21
+ * @param splitNumber
20
22
  * @returns 最大差值倍率
21
23
  */
22
- export declare const getDiffRate: (first: number, maxDiff: number, splitNumber?: number) => number;
24
+ export declare const getDiffRate: (first: number, maxDiff: number, decimalPlaces?: number, splitNumber?: number) => number;
23
25
  /**
24
26
  * 计算echarts YAxis的max和min属性,以达到根据实际数据动态调整,使折线图的波动明显。且第一个点始终在Y轴中间位置
25
27
  * @param data 原始数据
26
28
  * @param key Y轴字段
29
+ * @param decimalPlaces Y轴值的精度(小数位数)
27
30
  * @param splitNumber Y轴splitNumber属性
28
31
  * @returns
29
32
  */
30
- export declare function calcYAxisRange<T extends Record<string, any>, Key extends keyof T>(data: T[], key: Key, splitNumber?: number): {
33
+ export declare function calcYAxisRange<T extends Record<string, any>, Key extends keyof T>(data: T[], key: Key, decimalPlaces?: number, splitNumber?: number): {
31
34
  max: number;
32
35
  min: number;
33
36
  };
package/dist/echarts.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import dayjs from 'dayjs';
2
- import Decimal from 'decimal.js-light';
3
2
  import deepmerge from 'deepmerge';
4
3
  import { trueTypeOf } from './operator';
5
4
  const combineMerge = (target, source, options) => {
@@ -63,13 +62,14 @@ export function fill(dataSource, xAxisField, yAxisField) {
63
62
  * 判断max-min是否被5等分(是否小于0.01),小于0.01表示不能被5等分,基础倍率增加0.1后,再次判断,直到被5等分
64
63
  * @param first
65
64
  * @param maxDiff
65
+ * @param decimalPlaces
66
+ * @param splitNumber
66
67
  * @returns 最大差值倍率
67
68
  */
68
- export const getDiffRate = (first, maxDiff, splitNumber = 5) => {
69
+ export const getDiffRate = (first, maxDiff, decimalPlaces = 2, splitNumber = 5) => {
69
70
  let diffRate = 1.2;
70
71
  if (first === 0)
71
72
  return diffRate;
72
- const decimalPlaces = new Decimal(first).dp();
73
73
  const minDiff = 1 / Math.pow(10, decimalPlaces);
74
74
  function calc(f, d) {
75
75
  const max = f + d * diffRate;
@@ -86,10 +86,11 @@ export const getDiffRate = (first, maxDiff, splitNumber = 5) => {
86
86
  * 计算echarts YAxis的max和min属性,以达到根据实际数据动态调整,使折线图的波动明显。且第一个点始终在Y轴中间位置
87
87
  * @param data 原始数据
88
88
  * @param key Y轴字段
89
+ * @param decimalPlaces Y轴值的精度(小数位数)
89
90
  * @param splitNumber Y轴splitNumber属性
90
91
  * @returns
91
92
  */
92
- export function calcYAxisRange(data, key, splitNumber = 5) {
93
+ export function calcYAxisRange(data, key, decimalPlaces = 2, splitNumber = 5) {
93
94
  var _a, _b;
94
95
  const maxValue = (_a = Math.max(...data.map((o) => o[key]))) !== null && _a !== void 0 ? _a : 0;
95
96
  const minValue = (_b = Math.min(...data.map((o) => o[key]))) !== null && _b !== void 0 ? _b : 0;
@@ -104,7 +105,7 @@ export function calcYAxisRange(data, key, splitNumber = 5) {
104
105
  // 例如:first = 1, max = 1.3, min = 0.6, maxDiff = abs(min - first) = 0.4
105
106
  const maxDiff = Math.max(Math.abs(maxValue - firstValue), Math.abs(minValue - firstValue));
106
107
  // 差值缩放比例
107
- const diffRate = getDiffRate(firstValue, maxDiff, splitNumber);
108
+ const diffRate = getDiffRate(firstValue, maxDiff, decimalPlaces, splitNumber);
108
109
  const max = firstValue + (maxDiff === 0 ? firstValue / 6 : maxDiff * diffRate);
109
110
  const min = firstValue - (maxDiff === 0 ? firstValue / 6 : maxDiff * diffRate);
110
111
  return { max, min };
@@ -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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
3
  "sideEffects": false,
4
- "version": "1.17.0",
4
+ "version": "1.18.0",
5
5
  "description": "A dozen of utils for Front-End Development",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {
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`
@@ -446,7 +460,7 @@ deep merge Echarts配置,用法见[测试用例](./tests//echarts/echarts.cy.t
446
460
 
447
461
  填充的点的Y轴值为前一个点的值, 时间示例: [9:23, 9:27] => [9:23, 9:25, 9:27, 9:30],更多,见[测试用例](./tests/echarts/fill.cy.ts)
448
462
 
449
- - `calcYAxisRange<T extends Record<string, any>, Key extends keyof T>(data: T[], key: Key, splitNumber = 5): { max:number; min:number }`
463
+ - `calcYAxisRange<T extends Record<string, any>, Key extends keyof T>(data: T[], key: Key, decimalPlaces = 2, splitNumber = 5): { max:number; min:number }`
450
464
 
451
465
  计算echarts YAxis的max和min属性,以达到根据实际数据动态调整,使折线图的波动明显。且第一个点始终在Y轴中间位置,[效果图](https://raw.githubusercontent.com/mrdulin/pic-bucket-01/master/Dingtalk_20240724140535.jpg)
452
466