提交 | 用户 | age
|
bab28a
|
1 |
import { isArray, isFunction, isEmpty, isObject, isString, isNil } from '@/utils/is'; |
X |
2 |
import { dateUtil } from '@/utils/dateUtil'; |
2882d6
|
3 |
import { unref } from 'vue'; |
84c9d7
|
4 |
import type { Ref, ComputedRef } from 'vue'; |
0bb76a
|
5 |
import type { FormProps, FormSchemaInner as FormSchema } from '../types/form'; |
a3b9ff
|
6 |
import { cloneDeep, get, set, unset } from 'lodash-es'; |
ec3d51
|
7 |
|
84c9d7
|
8 |
interface UseFormValuesContext { |
V |
9 |
defaultValueRef: Ref<any>; |
|
10 |
getSchema: ComputedRef<FormSchema[]>; |
3ff70b
|
11 |
getProps: ComputedRef<FormProps>; |
4ff1c4
|
12 |
formModel: Recordable; |
84c9d7
|
13 |
} |
79eb90
|
14 |
|
L |
15 |
/** |
9882e8
|
16 |
* @description deconstruct array-link key. This method will mutate the target. |
79eb90
|
17 |
*/ |
L |
18 |
function tryDeconstructArray(key: string, value: any, target: Recordable) { |
|
19 |
const pattern = /^\[(.+)\]$/; |
|
20 |
if (pattern.test(key)) { |
|
21 |
const match = key.match(pattern); |
|
22 |
if (match && match[1]) { |
|
23 |
const keys = match[1].split(','); |
|
24 |
value = Array.isArray(value) ? value : [value]; |
|
25 |
keys.forEach((k, index) => { |
|
26 |
set(target, k.trim(), value[index]); |
|
27 |
}); |
|
28 |
return true; |
|
29 |
} |
|
30 |
} |
|
31 |
} |
|
32 |
|
|
33 |
/** |
9882e8
|
34 |
* @description deconstruct object-link key. This method will mutate the target. |
79eb90
|
35 |
*/ |
L |
36 |
function tryDeconstructObject(key: string, value: any, target: Recordable) { |
|
37 |
const pattern = /^\{(.+)\}$/; |
|
38 |
if (pattern.test(key)) { |
|
39 |
const match = key.match(pattern); |
|
40 |
if (match && match[1]) { |
|
41 |
const keys = match[1].split(','); |
|
42 |
value = isObject(value) ? value : {}; |
|
43 |
keys.forEach((k) => { |
|
44 |
set(target, k.trim(), value[k.trim()]); |
|
45 |
}); |
|
46 |
return true; |
|
47 |
} |
|
48 |
} |
|
49 |
} |
|
50 |
|
84c9d7
|
51 |
export function useFormValues({ |
V |
52 |
defaultValueRef, |
|
53 |
getSchema, |
|
54 |
formModel, |
3ff70b
|
55 |
getProps, |
84c9d7
|
56 |
}: UseFormValuesContext) { |
46e087
|
57 |
// Processing form values |
4ff1c4
|
58 |
function handleFormValues(values: Recordable) { |
2f6253
|
59 |
if (!isObject(values)) { |
陈 |
60 |
return {}; |
|
61 |
} |
4ff1c4
|
62 |
const res: Recordable = {}; |
2f6253
|
63 |
for (const item of Object.entries(values)) { |
陈 |
64 |
let [, value] = item; |
|
65 |
const [key] = item; |
791b32
|
66 |
if (!key || (isArray(value) && value.length === 0) || isFunction(value)) { |
2f6253
|
67 |
continue; |
陈 |
68 |
} |
3ff70b
|
69 |
const transformDateFunc = unref(getProps).transformDateFunc; |
2f6253
|
70 |
if (isObject(value)) { |
3ff70b
|
71 |
value = transformDateFunc?.(value); |
2f6253
|
72 |
} |
3fcfac
|
73 |
|
V |
74 |
if (isArray(value) && value[0]?.format && value[1]?.format) { |
3ff70b
|
75 |
value = value.map((item) => transformDateFunc?.(item)); |
2f6253
|
76 |
} |
46e087
|
77 |
// Remove spaces |
2f6253
|
78 |
if (isString(value)) { |
6fbb57
|
79 |
value = value.trim(); |
2f6253
|
80 |
} |
79eb90
|
81 |
if (!tryDeconstructArray(key, value, res) && !tryDeconstructObject(key, value, res)) { |
L |
82 |
// 没有解构成功的,按原样赋值 |
|
83 |
set(res, key, value); |
|
84 |
} |
2f6253
|
85 |
} |
4ff1c4
|
86 |
return handleRangeTimeValue(res); |
2f6253
|
87 |
} |
84c9d7
|
88 |
|
2f6253
|
89 |
/** |
46e087
|
90 |
* @description: Processing time interval parameters |
2f6253
|
91 |
*/ |
4ff1c4
|
92 |
function handleRangeTimeValue(values: Recordable) { |
3ff70b
|
93 |
const fieldMapToTime = unref(getProps).fieldMapToTime; |
2f6253
|
94 |
|
陈 |
95 |
if (!fieldMapToTime || !Array.isArray(fieldMapToTime)) { |
|
96 |
return values; |
|
97 |
} |
|
98 |
|
612995
|
99 |
for (const [field, [startTimeKey, endTimeKey], format = 'YYYY-MM-DD'] of fieldMapToTime) { |
c8b169
|
100 |
if (!field || !startTimeKey || !endTimeKey) { |
SW |
101 |
continue; |
|
102 |
} |
|
103 |
// If the value to be converted is empty, remove the field |
a3b9ff
|
104 |
if (!get(values, field)) { |
1 |
105 |
unset(values, field); |
2f6253
|
106 |
continue; |
陈 |
107 |
} |
|
108 |
|
a3b9ff
|
109 |
const [startTime, endTime]: string[] = get(values, field); |
2f6253
|
110 |
|
bc4997
|
111 |
const [startTimeFormat, endTimeFormat] = Array.isArray(format) ? format : [format, format]; |
S |
112 |
|
f91d77
|
113 |
if (!isNil(startTime) && !isEmpty(startTime)) { |
a3b9ff
|
114 |
set(values, startTimeKey, formatTime(startTime, startTimeFormat)); |
1 |
115 |
} |
f91d77
|
116 |
if (!isNil(endTime) && !isEmpty(endTime)) { |
a3b9ff
|
117 |
set(values, endTimeKey, formatTime(endTime, endTimeFormat)); |
1 |
118 |
} |
|
119 |
unset(values, field); |
2f6253
|
120 |
} |
陈 |
121 |
|
|
122 |
return values; |
|
123 |
} |
84c9d7
|
124 |
|
89d7a1
|
125 |
function formatTime(time: string, format: string) { |
IW |
126 |
if (format === 'timestamp') { |
|
127 |
return dateUtil(time).unix(); |
|
128 |
} else if (format === 'timestampStartDay') { |
|
129 |
return dateUtil(time).startOf('day').unix(); |
|
130 |
} |
|
131 |
return dateUtil(time).format(format); |
|
132 |
} |
|
133 |
|
84c9d7
|
134 |
function initDefault() { |
V |
135 |
const schemas = unref(getSchema); |
4ff1c4
|
136 |
const obj: Recordable = {}; |
84c9d7
|
137 |
schemas.forEach((item) => { |
a065de
|
138 |
const { defaultValue, defaultValueObj } = item; |
L |
139 |
const fieldKeys = Object.keys(defaultValueObj || {}); |
|
140 |
if (fieldKeys.length) { |
05030e
|
141 |
fieldKeys.forEach((field) => { |
a065de
|
142 |
obj[field] = defaultValueObj![field]; |
L |
143 |
if (formModel[field] === undefined) { |
|
144 |
formModel[field] = defaultValueObj![field]; |
|
145 |
} |
|
146 |
}); |
|
147 |
} |
f91d77
|
148 |
if (!isNil(defaultValue)) { |
ca4f1a
|
149 |
obj[item.field] = defaultValue; |
b41e39
|
150 |
|
HR |
151 |
if (formModel[item.field] === undefined) { |
|
152 |
formModel[item.field] = defaultValue; |
|
153 |
} |
84c9d7
|
154 |
} |
V |
155 |
}); |
d21578
|
156 |
defaultValueRef.value = cloneDeep(obj); |
84c9d7
|
157 |
} |
V |
158 |
|
|
159 |
return { handleFormValues, initDefault }; |
2f6253
|
160 |
} |