@etsoo/react 1.7.7 → 1.7.9

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
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) | 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
  }
@@ -84,4 +84,62 @@ export var ReactUtils;
84
84
  }
85
85
  }
86
86
  ReactUtils.triggerChange = triggerChange;
87
+ /**
88
+ * Update refs
89
+ * @param refs Refs
90
+ * @param data Data
91
+ * @param callback Callback to update refs' value
92
+ */
93
+ function updateRefs(refs, data, callback) {
94
+ const local = callback == null
95
+ ? undefined
96
+ : typeof callback === 'function'
97
+ ? callback
98
+ : (item, value) => {
99
+ item[callback] = value;
100
+ };
101
+ let k;
102
+ for (k in refs) {
103
+ const ref = refs[k];
104
+ const item = ref === null || ref === void 0 ? void 0 : ref.current;
105
+ if (item == null)
106
+ continue;
107
+ const value = data[k];
108
+ if (local) {
109
+ local(item, value);
110
+ }
111
+ else {
112
+ // Straightforward set string value to property "value"
113
+ item.value = value !== null && value !== void 0 ? value : '';
114
+ }
115
+ }
116
+ }
117
+ ReactUtils.updateRefs = updateRefs;
118
+ /**
119
+ * Update data with refs
120
+ * @param refs Refs
121
+ * @param data Data
122
+ * @param callback Callback to return new value
123
+ */
124
+ function updateRefValues(refs, data, callback) {
125
+ const local = callback == null
126
+ ? undefined
127
+ : typeof callback === 'function'
128
+ ? callback
129
+ : (item) => item[callback];
130
+ let k;
131
+ for (k in refs) {
132
+ const ref = refs[k];
133
+ const item = ref === null || ref === void 0 ? void 0 : ref.current;
134
+ if (item == null)
135
+ continue;
136
+ if (local) {
137
+ data[k] = local(item);
138
+ }
139
+ else {
140
+ data[k] = item.value;
141
+ }
142
+ }
143
+ }
144
+ ReactUtils.updateRefValues = updateRefValues;
87
145
  })(ReactUtils || (ReactUtils = {}));
@@ -185,7 +185,7 @@ export type GridColumn<T> = {
185
185
  /**
186
186
  * Render props
187
187
  */
188
- renderProps?: GridColumnRenderProps;
188
+ renderProps?: GridColumnRenderProps | ((data: T | undefined) => GridColumnRenderProps);
189
189
  /**
190
190
  * Header cell renderer
191
191
  */
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.7",
3
+ "version": "1.7.9",
4
4
  "description": "TypeScript ReactJs UI Independent Framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -50,28 +50,28 @@
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.35",
53
+ "@etsoo/appscript": "^1.4.53",
54
54
  "@etsoo/notificationbase": "^1.1.28",
55
- "@etsoo/shared": "^1.2.10",
55
+ "@etsoo/shared": "^1.2.12",
56
56
  "@types/react": "^18.2.21",
57
57
  "@types/react-dom": "^18.2.7",
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
- "@babel/cli": "^7.22.10",
66
- "@babel/core": "^7.22.11",
67
- "@babel/plugin-transform-runtime": "^7.22.10",
68
- "@babel/preset-env": "^7.22.14",
69
- "@babel/runtime-corejs3": "^7.22.11",
70
- "@testing-library/jest-dom": "^6.1.2",
65
+ "@babel/cli": "^7.22.15",
66
+ "@babel/core": "^7.22.19",
67
+ "@babel/plugin-transform-runtime": "^7.22.15",
68
+ "@babel/preset-env": "^7.22.15",
69
+ "@babel/runtime-corejs3": "^7.22.15",
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 } from '@etsoo/shared';
1
2
  import React from 'react';
2
3
 
3
4
  /**
@@ -98,4 +99,82 @@ 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
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?: ((item: T, value: D[keyof D & string]) => void) | keyof T
118
+ ) {
119
+ const local: typeof callback =
120
+ callback == null
121
+ ? undefined
122
+ : typeof callback === 'function'
123
+ ? callback
124
+ : (item, value) => {
125
+ item[callback] = value as any;
126
+ };
127
+
128
+ let k: keyof typeof refs;
129
+ for (k in refs) {
130
+ const ref = refs[k];
131
+ const item = ref?.current;
132
+ if (item == null) continue;
133
+ const value = data[k];
134
+
135
+ if (local) {
136
+ local(item, value);
137
+ } else {
138
+ // Straightforward set string value to property "value"
139
+ (item as any).value = value ?? '';
140
+ }
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Update data with refs
146
+ * @param refs Refs
147
+ * @param data Data
148
+ * @param callback Callback to return new value
149
+ */
150
+ export function updateRefValues<D, T = HTMLInputElement>(
151
+ refs: Partial<
152
+ DataTypes.DI<
153
+ ReadonlyArray<keyof D & string>,
154
+ React.MutableRefObject<T | undefined>
155
+ >
156
+ >,
157
+ data: D,
158
+ callback?: ((item: T) => any) | keyof T
159
+ ) {
160
+ const local: typeof callback =
161
+ callback == null
162
+ ? undefined
163
+ : typeof callback === 'function'
164
+ ? callback
165
+ : (item) => item[callback];
166
+
167
+ let k: keyof typeof refs;
168
+ for (k in refs) {
169
+ const ref = refs[k];
170
+ const item = ref?.current;
171
+ if (item == null) continue;
172
+
173
+ if (local) {
174
+ data[k] = local(item);
175
+ } else {
176
+ data[k] = (item as any).value;
177
+ }
178
+ }
179
+ }
101
180
  }
@@ -253,7 +253,9 @@ export type GridColumn<T> = {
253
253
  /**
254
254
  * Render props
255
255
  */
256
- renderProps?: GridColumnRenderProps;
256
+ renderProps?:
257
+ | GridColumnRenderProps
258
+ | ((data: T | undefined) => GridColumnRenderProps);
257
259
 
258
260
  /**
259
261
  * Header cell renderer
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
+ }