@qin-ui/vant-pro 1.0.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.
@@ -0,0 +1,236 @@
1
+ import { camelize, inject, reactive, provide, ref } from "vue";
2
+ import { i as isPlainObject, s as set, g as get, t as toPath } from "../vendor/utils/lodash-es-DN4QDiDm.js";
3
+ const InjectionFormKey = Symbol("form");
4
+ const InjectionPathKey = Symbol("path");
5
+ function getObject(val) {
6
+ return isPlainObject(val) ? val : {};
7
+ }
8
+ function camelizeProperties(obj) {
9
+ return Object.fromEntries(
10
+ Object.entries(obj).map(([key, value]) => [camelize(key), value])
11
+ );
12
+ }
13
+ const useFields = (initFields) => {
14
+ const fields = ref([]);
15
+ fields.value = initFields || [];
16
+ const _map = /* @__PURE__ */ new Map();
17
+ const cacheMatch = (pathStr, updater) => {
18
+ var _a;
19
+ const fieldPath = _map.get(pathStr) || [];
20
+ if (fieldPath.length === 0) return false;
21
+ const fieldIndex = fieldPath[fieldPath.length - 1];
22
+ const parentField = fieldPath.length === 1 ? fields.value : get(fields.value, fieldPath.slice(0, -2));
23
+ const field = (_a = parentField == null ? void 0 : parentField.fields) == null ? void 0 : _a[fieldIndex];
24
+ const _path = toPath((field == null ? void 0 : field.name) ?? (field == null ? void 0 : field.path)).join(".");
25
+ if (_path === pathStr) {
26
+ updater({ field, fieldIndex, parentField });
27
+ return true;
28
+ }
29
+ return false;
30
+ };
31
+ const updaterMatch = (path, updater, options = {}) => {
32
+ if (!path) return;
33
+ const pathStr = typeof path === "function" ? "" : toPath(path).join(".");
34
+ const { all = typeof path === "function" } = options;
35
+ if (pathStr && _map.has(pathStr) && !all) {
36
+ const bool = cacheMatch(pathStr, updater);
37
+ if (bool) return;
38
+ }
39
+ const queue = fields.value.map((field, i) => ({
40
+ field,
41
+ fieldPath: [i],
42
+ parentField: { fields: fields.value }
43
+ }));
44
+ while (queue.length) {
45
+ const { field, fieldPath, parentField } = queue.shift();
46
+ let matched = false;
47
+ const _path = toPath(field.name ?? field.path).join(".");
48
+ if (_path) _map.set(_path, fieldPath);
49
+ if (typeof path === "function") {
50
+ matched = path(field);
51
+ } else if (_path) {
52
+ matched = pathStr === _path;
53
+ }
54
+ if (matched) {
55
+ const fieldIndex = fieldPath[fieldPath.length - 1];
56
+ updater({ field, fieldIndex, parentField });
57
+ if (!all) return;
58
+ }
59
+ if (Array.isArray(field.fields)) {
60
+ field.fields.forEach((f, i) => {
61
+ queue.push({
62
+ field: f,
63
+ fieldPath: [...fieldPath, "fields", i],
64
+ parentField: field
65
+ });
66
+ });
67
+ }
68
+ }
69
+ };
70
+ function getField(path, options) {
71
+ if (!path) return void 0;
72
+ const res = [];
73
+ updaterMatch(
74
+ path,
75
+ ({ field }) => {
76
+ res.push(field);
77
+ },
78
+ options
79
+ );
80
+ return (options == null ? void 0 : options.all) ? res : res[0];
81
+ }
82
+ function setField(path, field, options) {
83
+ if (!path) return;
84
+ const { updateType = "merge", ...rest } = options || {};
85
+ updaterMatch(
86
+ path,
87
+ ({ field: preField, fieldIndex, parentField }) => {
88
+ let value = field;
89
+ if (typeof field === "function") {
90
+ value = field(preField);
91
+ }
92
+ if (!value) return;
93
+ if (updateType === "rewrite") {
94
+ parentField.fields[fieldIndex] = value;
95
+ } else {
96
+ Object.assign(preField, value);
97
+ }
98
+ },
99
+ rest
100
+ );
101
+ }
102
+ function deleteField(path, options) {
103
+ if (!path) return;
104
+ updaterMatch(
105
+ path,
106
+ ({ fieldIndex, parentField }) => {
107
+ parentField.fields.splice(fieldIndex, 1);
108
+ },
109
+ options
110
+ );
111
+ }
112
+ function addFields(path, newFields, options, placement) {
113
+ if (newFields.length === 0) return;
114
+ if (path) {
115
+ updaterMatch(
116
+ path,
117
+ ({ fieldIndex, parentField }) => {
118
+ const index = placement === "after" ? fieldIndex + 1 : fieldIndex;
119
+ parentField.fields.splice(index, 0, ...newFields);
120
+ },
121
+ options
122
+ );
123
+ } else if (placement === "after") {
124
+ fields.value.push(...newFields);
125
+ } else {
126
+ fields.value.unshift(...newFields);
127
+ }
128
+ }
129
+ function appendField(path, field, options) {
130
+ const newFields = Array.isArray(field) ? field : [field];
131
+ addFields(path, newFields, options, "after");
132
+ }
133
+ function prependField(path, field, options) {
134
+ const newFields = Array.isArray(field) ? field : [field];
135
+ addFields(path, newFields, options, "before");
136
+ }
137
+ function getParentField(path, options) {
138
+ if (!path) return void 0;
139
+ const res = [];
140
+ updaterMatch(
141
+ path,
142
+ ({ parentField }) => {
143
+ res.push(parentField);
144
+ },
145
+ options
146
+ );
147
+ return (options == null ? void 0 : options.all) ? res : res[0];
148
+ }
149
+ return {
150
+ fields,
151
+ getField,
152
+ setField,
153
+ deleteField,
154
+ appendField,
155
+ prependField,
156
+ getParentField
157
+ };
158
+ };
159
+ const InjectionFormDataKey = Symbol("form-data");
160
+ const useFormData = (initFormData) => {
161
+ if (!initFormData) {
162
+ const injectFormDataStore = inject(InjectionFormDataKey, void 0);
163
+ if (injectFormDataStore) return injectFormDataStore;
164
+ }
165
+ const formData = reactive(initFormData ?? {});
166
+ function getFormData(path) {
167
+ if (!path) return void 0;
168
+ return get(formData, path);
169
+ }
170
+ function setFormData(...args) {
171
+ let path;
172
+ let value;
173
+ if (args.length >= 2) {
174
+ [path, value] = args;
175
+ if (!path) return;
176
+ } else {
177
+ [value] = args;
178
+ }
179
+ if (path) {
180
+ if (typeof value === "function") {
181
+ const preValue = getFormData(path);
182
+ value = value(preValue);
183
+ }
184
+ set(formData, path, value);
185
+ } else {
186
+ if (typeof value === "function") {
187
+ const preValue = formData;
188
+ value = value(preValue);
189
+ }
190
+ if (!isPlainObject(value)) return;
191
+ Object.keys(formData).forEach((key) => {
192
+ delete formData[key];
193
+ });
194
+ Object.assign(formData, value);
195
+ }
196
+ }
197
+ const formDataStore = { formData, getFormData, setFormData };
198
+ provide(InjectionFormDataKey, formDataStore);
199
+ return formDataStore;
200
+ };
201
+ const useFormRef = () => {
202
+ const formRef = ref();
203
+ const setFormRef = (inst) => {
204
+ formRef.value = inst;
205
+ };
206
+ return { formRef, setFormRef };
207
+ };
208
+ function useForm(...args) {
209
+ let initFormData = {}, initFields = [], root = true;
210
+ if (args.length === 1) {
211
+ root = args[0];
212
+ } else if (args.length >= 2) {
213
+ initFormData = args[0] ?? {};
214
+ initFields = args[1];
215
+ root = args[2] ?? root;
216
+ }
217
+ if (!root) {
218
+ const injectForm = inject(InjectionFormKey);
219
+ if (injectForm) return injectForm;
220
+ }
221
+ return {
222
+ ...useFormData(initFormData),
223
+ ...useFields(initFields),
224
+ ...useFormRef()
225
+ };
226
+ }
227
+ export {
228
+ InjectionFormKey as I,
229
+ InjectionPathKey as a,
230
+ useFields as b,
231
+ camelizeProperties as c,
232
+ useFormRef as d,
233
+ useFormData as e,
234
+ getObject as g,
235
+ useForm as u
236
+ };