@etsoo/react 1.7.8 → 1.7.10

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.
@@ -1,3 +1,4 @@
1
+ import { DataTypes } from '@etsoo/shared';
1
2
  import React from 'react';
2
3
  /**
3
4
  * React utils
@@ -22,4 +23,18 @@ export declare namespace ReactUtils {
22
23
  * @param cancelable Cancelable
23
24
  */
24
25
  function triggerChange(input: HTMLInputElement, value: string, cancelable: boolean): void;
26
+ /**
27
+ * Update refs
28
+ * @param refs Refs
29
+ * @param data Data
30
+ * @param callback Callback to update refs' value, return false continue to process
31
+ */
32
+ function updateRefs<D, T = HTMLInputElement>(refs: Partial<DataTypes.DI<ReadonlyArray<keyof D & string>, React.MutableRefObject<T | undefined>>>, data: D, callback?: ((item: T, value: D[keyof D & string]) => void | boolean) | keyof T): void;
33
+ /**
34
+ * Update data with refs
35
+ * @param refs Refs
36
+ * @param data Data
37
+ * @param callback Callback to return new value
38
+ */
39
+ function updateRefValues<D, T = HTMLInputElement>(refs: Partial<DataTypes.DI<ReadonlyArray<keyof D & string>, React.MutableRefObject<T | undefined>>>, data: D, callback?: ((item: T) => any) | keyof T): void;
25
40
  }
@@ -1,3 +1,4 @@
1
+ import { DomUtils } from '@etsoo/shared';
1
2
  /**
2
3
  * React utils
3
4
  */
@@ -84,4 +85,67 @@ export var ReactUtils;
84
85
  }
85
86
  }
86
87
  ReactUtils.triggerChange = triggerChange;
88
+ /**
89
+ * Update refs
90
+ * @param refs Refs
91
+ * @param data Data
92
+ * @param callback Callback to update refs' value, return false continue to process
93
+ */
94
+ function updateRefs(refs, data, callback) {
95
+ const local = callback == null
96
+ ? undefined
97
+ : typeof callback === 'function'
98
+ ? callback
99
+ : (item, value) => {
100
+ item[callback] = value;
101
+ };
102
+ let k;
103
+ for (k in refs) {
104
+ const ref = refs[k];
105
+ const item = ref === null || ref === void 0 ? void 0 : ref.current;
106
+ if (item == null)
107
+ continue;
108
+ const value = data[k];
109
+ if (local && local(item, value) !== false) {
110
+ continue;
111
+ }
112
+ else if (item instanceof HTMLInputElement) {
113
+ item.value = `${value !== null && value !== void 0 ? value : ''}`;
114
+ }
115
+ else {
116
+ item.value = value;
117
+ }
118
+ }
119
+ }
120
+ ReactUtils.updateRefs = updateRefs;
121
+ /**
122
+ * Update data with refs
123
+ * @param refs Refs
124
+ * @param data Data
125
+ * @param callback Callback to return new value
126
+ */
127
+ function updateRefValues(refs, data, callback) {
128
+ const local = callback == null
129
+ ? undefined
130
+ : typeof callback === 'function'
131
+ ? callback
132
+ : (item) => item[callback];
133
+ let k;
134
+ for (k in refs) {
135
+ const ref = refs[k];
136
+ const item = ref === null || ref === void 0 ? void 0 : ref.current;
137
+ if (item == null)
138
+ continue;
139
+ if (local) {
140
+ data[k] = local(item);
141
+ }
142
+ else if (item instanceof HTMLInputElement) {
143
+ data[k] = DomUtils.getInputValue(item);
144
+ }
145
+ else {
146
+ data[k] = item.value;
147
+ }
148
+ }
149
+ }
150
+ ReactUtils.updateRefValues = updateRefValues;
87
151
  })(ReactUtils || (ReactUtils = {}));
package/lib/index.d.ts CHANGED
@@ -24,6 +24,7 @@ export * from './uses/useCombinedRefs';
24
24
  export * from './uses/useDelayedExecutor';
25
25
  export * from './uses/useDimensions';
26
26
  export * from './uses/useParamsEx';
27
+ export * from './uses/useRefs';
27
28
  export * from './uses/useSearchParamsEx';
28
29
  export * from './uses/useTimeout';
29
30
  export * from './uses/useWindowScroll';
package/lib/index.js CHANGED
@@ -28,6 +28,7 @@ export * from './uses/useCombinedRefs';
28
28
  export * from './uses/useDelayedExecutor';
29
29
  export * from './uses/useDimensions';
30
30
  export * from './uses/useParamsEx';
31
+ export * from './uses/useRefs';
31
32
  export * from './uses/useSearchParamsEx';
32
33
  export * from './uses/useTimeout';
33
34
  export * from './uses/useWindowScroll';
@@ -0,0 +1,8 @@
1
+ import { DataTypes } from '@etsoo/shared';
2
+ import React from 'react';
3
+ /**
4
+ * Create multiple refs
5
+ * @param fields Fields
6
+ * @returns Result
7
+ */
8
+ export declare function useRefs<F extends ReadonlyArray<string>, T = HTMLInputElement>(fields: F): DataTypes.DI<F, React.MutableRefObject<T | undefined>>;
@@ -0,0 +1,11 @@
1
+ import React from 'react';
2
+ /**
3
+ * Create multiple refs
4
+ * @param fields Fields
5
+ * @returns Result
6
+ */
7
+ export function useRefs(fields) {
8
+ const refs = {};
9
+ fields.forEach((field) => (refs[field] = React.useRef()));
10
+ return refs;
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.7.8",
3
+ "version": "1.7.10",
4
4
  "description": "TypeScript ReactJs UI Independent Framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -50,7 +50,7 @@
50
50
  "@emotion/css": "^11.11.2",
51
51
  "@emotion/react": "^11.11.1",
52
52
  "@emotion/styled": "^11.11.0",
53
- "@etsoo/appscript": "^1.4.45",
53
+ "@etsoo/appscript": "^1.4.53",
54
54
  "@etsoo/notificationbase": "^1.1.28",
55
55
  "@etsoo/shared": "^1.2.12",
56
56
  "@types/react": "^18.2.21",
@@ -58,20 +58,20 @@
58
58
  "@types/react-window": "^1.8.5",
59
59
  "react": "^18.2.0",
60
60
  "react-dom": "^18.2.0",
61
- "react-router-dom": "^6.15.0",
61
+ "react-router-dom": "^6.16.0",
62
62
  "react-window": "^1.8.9"
63
63
  },
64
64
  "devDependencies": {
65
65
  "@babel/cli": "^7.22.15",
66
- "@babel/core": "^7.22.17",
66
+ "@babel/core": "^7.22.19",
67
67
  "@babel/plugin-transform-runtime": "^7.22.15",
68
68
  "@babel/preset-env": "^7.22.15",
69
69
  "@babel/runtime-corejs3": "^7.22.15",
70
70
  "@testing-library/jest-dom": "^6.1.3",
71
71
  "@testing-library/react": "^14.0.0",
72
72
  "@types/jest": "^29.5.4",
73
- "jest": "^29.6.4",
74
- "jest-environment-jsdom": "^29.6.4",
73
+ "jest": "^29.7.0",
74
+ "jest-environment-jsdom": "^29.7.0",
75
75
  "ts-jest": "^29.1.1",
76
76
  "typescript": "^5.2.2"
77
77
  }
@@ -1,3 +1,4 @@
1
+ import { DataTypes, DomUtils } from '@etsoo/shared';
1
2
  import React from 'react';
2
3
 
3
4
  /**
@@ -98,4 +99,87 @@ export namespace ReactUtils {
98
99
  input.dispatchEvent(inputEvent);
99
100
  }
100
101
  }
102
+
103
+ /**
104
+ * Update refs
105
+ * @param refs Refs
106
+ * @param data Data
107
+ * @param callback Callback to update refs' value, return false continue to process
108
+ */
109
+ export function updateRefs<D, T = HTMLInputElement>(
110
+ refs: Partial<
111
+ DataTypes.DI<
112
+ ReadonlyArray<keyof D & string>,
113
+ React.MutableRefObject<T | undefined>
114
+ >
115
+ >,
116
+ data: D,
117
+ callback?:
118
+ | ((item: T, value: D[keyof D & string]) => void | boolean)
119
+ | keyof T
120
+ ) {
121
+ const local: typeof callback =
122
+ callback == null
123
+ ? undefined
124
+ : typeof callback === 'function'
125
+ ? callback
126
+ : (item, value) => {
127
+ item[callback] = value as any;
128
+ };
129
+
130
+ let k: keyof typeof refs;
131
+ for (k in refs) {
132
+ const ref = refs[k];
133
+ const item = ref?.current;
134
+ if (item == null) continue;
135
+ const value = data[k];
136
+
137
+ if (local && local(item, value) !== false) {
138
+ continue;
139
+ } else if (item instanceof HTMLInputElement) {
140
+ item.value = `${value ?? ''}`;
141
+ } else {
142
+ (item as any).value = value;
143
+ }
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Update data with refs
149
+ * @param refs Refs
150
+ * @param data Data
151
+ * @param callback Callback to return new value
152
+ */
153
+ export function updateRefValues<D, T = HTMLInputElement>(
154
+ refs: Partial<
155
+ DataTypes.DI<
156
+ ReadonlyArray<keyof D & string>,
157
+ React.MutableRefObject<T | undefined>
158
+ >
159
+ >,
160
+ data: D,
161
+ callback?: ((item: T) => any) | keyof T
162
+ ) {
163
+ const local: typeof callback =
164
+ callback == null
165
+ ? undefined
166
+ : typeof callback === 'function'
167
+ ? callback
168
+ : (item) => item[callback];
169
+
170
+ let k: keyof typeof refs;
171
+ for (k in refs) {
172
+ const ref = refs[k];
173
+ const item = ref?.current;
174
+ if (item == null) continue;
175
+
176
+ if (local) {
177
+ data[k] = local(item);
178
+ } else if (item instanceof HTMLInputElement) {
179
+ data[k] = DomUtils.getInputValue(item) as any;
180
+ } else {
181
+ data[k] = (item as any).value;
182
+ }
183
+ }
184
+ }
101
185
  }
package/src/index.ts CHANGED
@@ -37,6 +37,7 @@ export * from './uses/useCombinedRefs';
37
37
  export * from './uses/useDelayedExecutor';
38
38
  export * from './uses/useDimensions';
39
39
  export * from './uses/useParamsEx';
40
+ export * from './uses/useRefs';
40
41
  export * from './uses/useSearchParamsEx';
41
42
  export * from './uses/useTimeout';
42
43
  export * from './uses/useWindowScroll';
@@ -0,0 +1,15 @@
1
+ import { DataTypes } from '@etsoo/shared';
2
+ import React from 'react';
3
+
4
+ /**
5
+ * Create multiple refs
6
+ * @param fields Fields
7
+ * @returns Result
8
+ */
9
+ export function useRefs<F extends ReadonlyArray<string>, T = HTMLInputElement>(
10
+ fields: F
11
+ ): DataTypes.DI<F, React.MutableRefObject<T | undefined>> {
12
+ const refs: Record<string, React.MutableRefObject<T | undefined>> = {};
13
+ fields.forEach((field) => (refs[field] = React.useRef<T>()));
14
+ return refs as any;
15
+ }