@jsb188/react 1.1.8 → 1.1.11

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/utils/schema.ts CHANGED
@@ -1,18 +1,21 @@
1
1
  import { useState } from 'react';
2
- import i18n from '@jsb188/app/i18n';
3
- import type { FormItemSchemaObj, FormSchemaObj } from '@jsb188/app/types/form.d';
4
- import { getObject, setObject } from '@jsb188/app/utils/object';
2
+ import i18n from '@jsb188/app/i18n/index.ts';
3
+ import type {
4
+ FormItemSchemaObj,
5
+ FormSchemaObj,
6
+ } from '@jsb188/app/types/form.d.ts';
7
+ import { getObject, setObject } from '@jsb188/app/utils/object.ts';
5
8
 
6
9
  /**
7
10
  * Types
8
11
  */
9
12
 
10
13
  interface ComputedSchema {
11
- formValues: any;
12
- setFormValues: (formValues: any) => void;
13
- listData: (FormItemSchemaObj & { name: string })[];
14
- validate?: () => any;
15
- isButtonDisabled?: (formValues: any) => boolean;
14
+ formValues: any;
15
+ setFormValues: (formValues: any) => void;
16
+ listData: (FormItemSchemaObj & { name: string })[];
17
+ validate?: () => any;
18
+ isButtonDisabled?: (formValues: any) => boolean;
16
19
  }
17
20
 
18
21
  /**
@@ -20,37 +23,47 @@ interface ComputedSchema {
20
23
  */
21
24
 
22
25
  export function makeFormValues(
23
- schema: any,
24
- dataForSchema?: any,
25
- currentData?: any,
26
+ schema: any,
27
+ dataForSchema?: any,
28
+ currentData?: any,
26
29
  ) {
27
- const newFormValues: Record<string, any> = {};
28
- const { listData } = schema;
29
-
30
- for (const obj of listData) {
31
- const { item: { name, setter, items } } = obj;
32
-
33
- if (currentData || dataForSchema) {
34
- if (Array.isArray(items)) {
35
- items.forEach(({ item: subItem }: any) => {
36
- if (typeof subItem.setter === 'function') {
37
- setObject(newFormValues, subItem.name, subItem.setter(
38
- currentData,
39
- dataForSchema,
40
- ));
41
- } else if (currentData) {
42
- setObject(newFormValues, subItem.name, getObject(currentData, subItem.name));
43
- }
44
- });
45
- } else if (typeof setter === 'function') {
46
- setObject(newFormValues, name, setter(currentData, dataForSchema));
47
- } else if (currentData) {
48
- setObject(newFormValues, name, getObject(currentData, name));
49
- }
50
- }
51
- }
52
-
53
- return newFormValues;
30
+ const newFormValues: Record<string, any> = {};
31
+ const { listData } = schema;
32
+
33
+ for (const obj of listData) {
34
+ const item = obj?.item || {};
35
+ const { name, setter, items } = item;
36
+
37
+ if (!name && !Array.isArray(items)) {
38
+ continue;
39
+ }
40
+
41
+ if (currentData || dataForSchema) {
42
+ if (Array.isArray(items)) {
43
+ items.forEach(({ item: subItem }: any) => {
44
+ if (typeof subItem.setter === 'function') {
45
+ setObject(
46
+ newFormValues,
47
+ subItem.name,
48
+ subItem.setter(currentData, dataForSchema),
49
+ );
50
+ } else if (currentData) {
51
+ setObject(
52
+ newFormValues,
53
+ subItem.name,
54
+ getObject(currentData, subItem.name),
55
+ );
56
+ }
57
+ });
58
+ } else if (typeof setter === 'function') {
59
+ setObject(newFormValues, name, setter(currentData, dataForSchema));
60
+ } else if (currentData) {
61
+ setObject(newFormValues, name, getObject(currentData, name));
62
+ }
63
+ }
64
+ }
65
+
66
+ return newFormValues;
54
67
  }
55
68
 
56
69
  /**
@@ -58,96 +71,100 @@ export function makeFormValues(
58
71
  */
59
72
 
60
73
  export function useSchema(
61
- schema: FormSchemaObj,
62
- dataForSchema: any,
63
- currentData?: any,
74
+ schema: FormSchemaObj,
75
+ dataForSchema: any,
76
+ currentData?: any,
64
77
  ): ComputedSchema {
65
- const { listData, rules, isButtonDisabled } = schema;
66
- const [formValues, setFormValues] = useState(makeFormValues(schema, dataForSchema, currentData));
67
-
68
- // Make listData from schema
69
-
70
- const computedListData: any[] = listData.map((schemaItem: FormItemSchemaObj) => {
71
- const { __type, hidden, locked, item } = schemaItem;
72
- if (
73
- hidden === true ||
74
- (typeof hidden === 'function' && hidden(dataForSchema))
75
- ) {
76
- return null;
77
- }
78
-
79
- let lockedValue;
80
- if (typeof locked === 'function') {
81
- lockedValue = locked(dataForSchema);
82
- } else if (typeof locked === 'boolean') {
83
- lockedValue = locked;
84
- }
85
-
86
- const computedItem: Record<string, any> = { ...item };
87
- if (computedItem) {
88
- for (const key in computedItem) {
89
- if (
90
- typeof computedItem[key] === 'function' &&
91
- !key.startsWith('onClick') &&
92
- !['getter', 'setter'].includes(key)
93
- ) {
94
- computedItem[key] = computedItem[key](dataForSchema);
95
- }
96
- }
97
- }
98
-
99
- return {
100
- __type,
101
- locked: lockedValue,
102
- item: computedItem,
103
- };
104
- }).filter(Boolean);
105
-
106
- // Create validation function
107
-
108
- const validate = (): any => {
109
- if (rules) {
110
- const __errorFields: string[] = [];
111
-
112
- let firstError: any = null;
113
- for (const ruleObj of rules) {
114
- const { for: key, rule, error } = ruleObj;
115
- const value = formValues[key];
116
- const isValid = rule(value, formValues);
117
-
118
- if (!isValid) {
119
- if (!__errorFields.includes(key)) {
120
- __errorFields.push(key);
121
- }
122
-
123
- if (!firstError) {
124
- if (error) {
125
- firstError = typeof error === 'function' ? error(value) : error;
126
- } else {
127
- firstError = {
128
- message: i18n.t('error.unknown_error_msg'),
129
- };
130
- }
131
- }
132
- }
133
- }
134
-
135
- setFormValues({
136
- ...formValues,
137
- __errorFields,
138
- });
139
-
140
- return firstError;
141
- }
142
-
143
- return null;
144
- };
145
-
146
- return {
147
- formValues,
148
- setFormValues,
149
- listData: computedListData,
150
- validate,
151
- isButtonDisabled,
152
- };
78
+ const { listData, rules, isButtonDisabled } = schema;
79
+ const [formValues, setFormValues] = useState(
80
+ makeFormValues(schema, dataForSchema, currentData),
81
+ );
82
+
83
+ // Make listData from schema
84
+
85
+ const computedListData: any[] = listData
86
+ .map((schemaItem: FormItemSchemaObj) => {
87
+ const { __type, hidden, locked, item } = schemaItem;
88
+ if (
89
+ hidden === true ||
90
+ (typeof hidden === 'function' && hidden(dataForSchema))
91
+ ) {
92
+ return null;
93
+ }
94
+
95
+ let lockedValue;
96
+ if (typeof locked === 'function') {
97
+ lockedValue = locked(dataForSchema);
98
+ } else if (typeof locked === 'boolean') {
99
+ lockedValue = locked;
100
+ }
101
+
102
+ const computedItem: Record<string, any> = { ...item };
103
+ if (computedItem) {
104
+ for (const key in computedItem) {
105
+ if (
106
+ typeof computedItem[key] === 'function' &&
107
+ !key.startsWith('onClick') &&
108
+ !['getter', 'setter'].includes(key)
109
+ ) {
110
+ computedItem[key] = computedItem[key](dataForSchema);
111
+ }
112
+ }
113
+ }
114
+
115
+ return {
116
+ __type,
117
+ locked: lockedValue,
118
+ item: computedItem,
119
+ };
120
+ })
121
+ .filter(Boolean);
122
+
123
+ // Create validation function
124
+
125
+ const validate = (): any => {
126
+ if (rules) {
127
+ const __errorFields: string[] = [];
128
+
129
+ let firstError: any = null;
130
+ for (const ruleObj of rules) {
131
+ const { for: key, rule, error } = ruleObj;
132
+ const value = formValues[key];
133
+ const isValid = rule(value, formValues);
134
+
135
+ if (!isValid) {
136
+ if (!__errorFields.includes(key)) {
137
+ __errorFields.push(key);
138
+ }
139
+
140
+ if (!firstError) {
141
+ if (error) {
142
+ firstError = typeof error === 'function' ? error(value) : error;
143
+ } else {
144
+ firstError = {
145
+ message: i18n.t('error.unknown_error_msg'),
146
+ };
147
+ }
148
+ }
149
+ }
150
+ }
151
+
152
+ setFormValues({
153
+ ...formValues,
154
+ __errorFields,
155
+ });
156
+
157
+ return firstError;
158
+ }
159
+
160
+ return null;
161
+ };
162
+
163
+ return {
164
+ formValues,
165
+ setFormValues,
166
+ listData: computedListData,
167
+ validate,
168
+ isButtonDisabled,
169
+ };
153
170
  }
package/states/toasts.ts DELETED
@@ -1,30 +0,0 @@
1
- import type { WSDataUpdateObj } from '@jsb188/app/types/app.d';
2
- import { atom, useAtomValue, useSetAtom } from 'jotai';
3
-
4
- /**
5
- * Types; Fragment/observer hooks
6
- */
7
-
8
- export type ToastObj = Omit<WSDataUpdateObj, 'resetQueryKeys' | 'resetQueryRule' | 'fragment'>;
9
-
10
- /**
11
- * Toast; state
12
- */
13
-
14
- const toastState = atom<ToastObj | null>(null);
15
-
16
- /**
17
- * Export; get toast value
18
- */
19
-
20
- export function useToastValue() {
21
- return useAtomValue(toastState);
22
- }
23
-
24
- /**
25
- * Export; set toast state
26
- */
27
-
28
- export function useSetToast() {
29
- return useSetAtom(toastState);
30
- }