@formvk/vue 1.0.0 → 1.1.1
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/CHANGELOG.md +16 -0
- package/README.md +2 -0
- package/README.zh-CN.md +65 -0
- package/dist/index.js +451 -322
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,122 +1,159 @@
|
|
|
1
|
-
import { Schema
|
|
2
|
-
import { FormPath
|
|
3
|
-
import { Fragment
|
|
4
|
-
import { createForm as
|
|
5
|
-
import { toJS
|
|
1
|
+
import { Schema, createFragment } from "@formvk/schema";
|
|
2
|
+
import { FormPath, each, isFn, isStr, isValid, lazyMerge, uid } from "@formvk/shared";
|
|
3
|
+
import { Fragment, computed, createVNode, defineComponent, h, inject, isVNode, markRaw, nextTick, onBeforeUnmount, onMounted, onUnmounted, provide, ref, shallowRef, toRaw, toRef, watch, watchEffect } from "vue";
|
|
4
|
+
import { createForm as createForm$1, isObjectField, isVoidField } from "@formvk/core";
|
|
5
|
+
import { toJS } from "@formvk/reactive";
|
|
6
6
|
export * from "@formvk/schema";
|
|
7
7
|
// src/shared/context.ts
|
|
8
|
-
var
|
|
8
|
+
var FormSymbol = Symbol("form");
|
|
9
|
+
var FieldSymbol = Symbol("field");
|
|
10
|
+
var SchemaSymbol = Symbol("schema");
|
|
11
|
+
var SchemaExpressionScopeSymbol = Symbol("schemaExpression");
|
|
12
|
+
var SchemaOptionsSymbol = Symbol("schemaOptions");
|
|
13
|
+
// src/components/ExpressionScope.tsx
|
|
14
|
+
var ExpressionScope = /* @__PURE__ */ defineComponent({
|
|
9
15
|
name: "ExpressionScope",
|
|
10
|
-
inheritAttrs:
|
|
16
|
+
inheritAttrs: false,
|
|
11
17
|
props: ["value"],
|
|
12
|
-
setup(
|
|
13
|
-
|
|
14
|
-
|
|
18
|
+
setup(props, { slots }) {
|
|
19
|
+
const scopeRef = inject(SchemaExpressionScopeSymbol, ref({}));
|
|
20
|
+
const expressionScopeRef = computed(() => lazyMerge(scopeRef.value, props.value));
|
|
21
|
+
provide(SchemaExpressionScopeSymbol, expressionScopeRef);
|
|
22
|
+
return () => slots.default?.();
|
|
15
23
|
}
|
|
16
24
|
});
|
|
17
25
|
// src/hooks/useForm.ts
|
|
18
|
-
function
|
|
19
|
-
return
|
|
26
|
+
function useForm() {
|
|
27
|
+
return inject(FormSymbol, ref());
|
|
20
28
|
}
|
|
21
29
|
// src/components/FormConsumer.tsx
|
|
22
|
-
var
|
|
30
|
+
var FormConsumer = /* @__PURE__ */ defineComponent({
|
|
23
31
|
name: "FormConsumer",
|
|
24
|
-
inheritAttrs:
|
|
32
|
+
inheritAttrs: false,
|
|
25
33
|
slots: Object,
|
|
26
|
-
setup(
|
|
27
|
-
|
|
28
|
-
return () =>
|
|
34
|
+
setup(_, { slots }) {
|
|
35
|
+
const formRef = useForm();
|
|
36
|
+
return () => slots.default?.({ form: formRef.value }) ?? null;
|
|
29
37
|
}
|
|
30
38
|
});
|
|
31
39
|
// src/hooks/useAttach.ts
|
|
32
|
-
function
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
})
|
|
40
|
+
function useAttach(target) {
|
|
41
|
+
watch(target, (value, oldValue, onInvalidate) => {
|
|
42
|
+
if (value && value !== oldValue) {
|
|
43
|
+
oldValue?.onUnmount();
|
|
44
|
+
nextTick(() => value.onMount());
|
|
45
|
+
onInvalidate(() => value.onUnmount());
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
onMounted(() => {
|
|
49
|
+
target.value?.onMount();
|
|
50
|
+
});
|
|
51
|
+
onUnmounted(() => {
|
|
52
|
+
target.value?.onUnmount();
|
|
53
|
+
});
|
|
54
|
+
return target;
|
|
40
55
|
}
|
|
41
56
|
// src/hooks/useInjectionCleaner.ts
|
|
42
|
-
function
|
|
43
|
-
|
|
57
|
+
function useInjectionCleaner(injectionKeys) {
|
|
58
|
+
injectionKeys.forEach((key) => provide(key, ref()));
|
|
44
59
|
}
|
|
45
60
|
// src/components/FormProvider.tsx
|
|
46
|
-
var
|
|
61
|
+
var FormProvider = /* @__PURE__ */ defineComponent({
|
|
47
62
|
name: "FormProvider",
|
|
48
|
-
inheritAttrs:
|
|
63
|
+
inheritAttrs: false,
|
|
49
64
|
props: ["form"],
|
|
50
|
-
setup(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
65
|
+
setup(props, { slots }) {
|
|
66
|
+
const formRef = useAttach(toRef(props, "form"));
|
|
67
|
+
provide(FormSymbol, formRef);
|
|
68
|
+
useInjectionCleaner([
|
|
69
|
+
FieldSymbol,
|
|
70
|
+
SchemaSymbol,
|
|
71
|
+
SchemaExpressionScopeSymbol,
|
|
72
|
+
SchemaOptionsSymbol
|
|
73
|
+
]);
|
|
74
|
+
return () => slots.default?.();
|
|
58
75
|
}
|
|
59
76
|
});
|
|
60
77
|
// src/hooks/useField.ts
|
|
61
|
-
function
|
|
62
|
-
return
|
|
78
|
+
function useField() {
|
|
79
|
+
return inject(FieldSymbol, ref());
|
|
63
80
|
}
|
|
64
81
|
// src/utils/getRawComponent.ts
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
82
|
+
/**
|
|
83
|
+
* 解析 schema 中的组件描述:字符串按路径从 components 中查找,组件对象直接使用。
|
|
84
|
+
* 返回前统一 toRaw,避免组件定义被响应式代理包裹。
|
|
85
|
+
*/
|
|
86
|
+
function getRawComponent(components, component) {
|
|
87
|
+
const resolved = isStr(component) ? FormPath.getIn(components, component) : void 0;
|
|
88
|
+
return toRaw(resolved ?? component);
|
|
68
89
|
}
|
|
69
90
|
// src/components/ReactiveField.tsx
|
|
70
|
-
function
|
|
71
|
-
|
|
91
|
+
function isVueComponentLike(value) {
|
|
92
|
+
if (!value) return false;
|
|
93
|
+
if (isFn(value)) return true;
|
|
94
|
+
return typeof value === "object" && (isFn(value.setup) || isFn(value.render) || isStr(value.template));
|
|
72
95
|
}
|
|
73
|
-
function
|
|
74
|
-
|
|
96
|
+
function toArray(value) {
|
|
97
|
+
if (Array.isArray(value)) return value;
|
|
98
|
+
return value === void 0 || value === null ? [] : [value];
|
|
75
99
|
}
|
|
76
|
-
function
|
|
77
|
-
|
|
100
|
+
function wrapFragment(child) {
|
|
101
|
+
if (Array.isArray(child)) {
|
|
102
|
+
if (child.length > 1) return h(Fragment, null, child);
|
|
103
|
+
return child[0] ?? null;
|
|
104
|
+
}
|
|
105
|
+
return child;
|
|
78
106
|
}
|
|
79
|
-
function
|
|
80
|
-
|
|
107
|
+
function renderContent(content) {
|
|
108
|
+
if (isVNode(content) || isStr(content)) return content;
|
|
109
|
+
if (isVueComponentLike(content)) return h(content);
|
|
110
|
+
return content;
|
|
81
111
|
}
|
|
82
|
-
function
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
field
|
|
87
|
-
form:
|
|
88
|
-
...
|
|
112
|
+
function mergeSlots(field, slots, content) {
|
|
113
|
+
const result = {};
|
|
114
|
+
Object.keys(slots).forEach((name) => {
|
|
115
|
+
result[name] = (slotProps) => slots[name]?.({
|
|
116
|
+
field,
|
|
117
|
+
form: field.form,
|
|
118
|
+
...slotProps
|
|
89
119
|
}) ?? [];
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
120
|
+
});
|
|
121
|
+
if (!content) return result;
|
|
122
|
+
function appendToSlot(name, value) {
|
|
123
|
+
const origin = result[name];
|
|
124
|
+
result[name] = (slotProps) => [...toArray(origin?.(slotProps)), ...toArray(renderContent(value))];
|
|
94
125
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
126
|
+
if (isStr(content) || isVNode(content) || isVueComponentLike(content)) appendToSlot("default", content);
|
|
127
|
+
else if (typeof content === "object") each(content, (value, key) => {
|
|
128
|
+
appendToSlot(key, value);
|
|
129
|
+
});
|
|
130
|
+
return result;
|
|
98
131
|
}
|
|
99
|
-
function
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
props
|
|
108
|
-
|
|
132
|
+
function normalizeProps(origin) {
|
|
133
|
+
const props = {};
|
|
134
|
+
const events = {};
|
|
135
|
+
each(origin, (value, key) => {
|
|
136
|
+
if (key.startsWith("on")) {
|
|
137
|
+
const eventName = `${key[2].toLowerCase()}${key.slice(3)}`;
|
|
138
|
+
events[eventName] = events[eventName] ?? value;
|
|
139
|
+
} else if (key.startsWith("@")) events[key.slice(1)] = value;
|
|
140
|
+
else props[key] = value;
|
|
141
|
+
});
|
|
142
|
+
return {
|
|
143
|
+
props,
|
|
144
|
+
events
|
|
109
145
|
};
|
|
110
146
|
}
|
|
111
|
-
function
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
})
|
|
147
|
+
function toVueEventProps(events) {
|
|
148
|
+
const props = {};
|
|
149
|
+
each(events, (handler, name) => {
|
|
150
|
+
props[`on${name.charAt(0).toUpperCase()}${name.slice(1)}`] = handler;
|
|
151
|
+
});
|
|
152
|
+
return props;
|
|
116
153
|
}
|
|
117
|
-
var
|
|
154
|
+
var ReactiveField = /* @__PURE__ */ defineComponent({
|
|
118
155
|
name: "ReactiveField",
|
|
119
|
-
inheritAttrs:
|
|
156
|
+
inheritAttrs: false,
|
|
120
157
|
props: {
|
|
121
158
|
fieldType: {
|
|
122
159
|
type: String,
|
|
@@ -127,71 +164,89 @@ var K = /* @__PURE__ */ f({
|
|
|
127
164
|
default: () => ({})
|
|
128
165
|
}
|
|
129
166
|
},
|
|
130
|
-
setup(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
167
|
+
setup(props, { slots }) {
|
|
168
|
+
const formRef = useForm();
|
|
169
|
+
const parentRef = useField();
|
|
170
|
+
const optionsRef = inject(SchemaOptionsSymbol, ref({}));
|
|
171
|
+
function createField() {
|
|
172
|
+
const fieldType = props.fieldType ?? "Field";
|
|
173
|
+
const fieldProps = props.fieldProps ?? {};
|
|
174
|
+
return formRef.value?.[`create${fieldType}`]?.({
|
|
175
|
+
...fieldProps,
|
|
176
|
+
basePath: fieldProps.basePath ?? parentRef.value?.address
|
|
137
177
|
});
|
|
138
178
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
179
|
+
const fieldRef = shallowRef(createField());
|
|
180
|
+
watch(() => props.fieldProps, () => {
|
|
181
|
+
fieldRef.value = createField();
|
|
182
|
+
});
|
|
183
|
+
useAttach(fieldRef);
|
|
184
|
+
provide(FieldSymbol, fieldRef);
|
|
185
|
+
return () => {
|
|
186
|
+
const field = fieldRef.value;
|
|
187
|
+
const options = optionsRef.value;
|
|
188
|
+
if (!field) return slots.default?.();
|
|
189
|
+
if (field.display !== "visible") return null;
|
|
190
|
+
const mergedSlots = mergeSlots(field, slots, field.content);
|
|
191
|
+
function renderDecorator(children) {
|
|
192
|
+
if (!field.decoratorType) return wrapFragment(children);
|
|
193
|
+
const Decorator = getRawComponent(options?.components, field.decoratorType);
|
|
194
|
+
const { props: decoratorProps, events } = normalizeProps(toJS(field.decorator[1]) || {});
|
|
195
|
+
return h(Decorator, {
|
|
196
|
+
...decoratorProps,
|
|
197
|
+
...toVueEventProps(events)
|
|
198
|
+
}, { default: () => children });
|
|
154
199
|
}
|
|
155
|
-
function
|
|
156
|
-
if (!
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
200
|
+
function renderComponent() {
|
|
201
|
+
if (!field.componentType) return mergedSlots.default?.() ?? null;
|
|
202
|
+
const Component = getRawComponent(options?.components, field.componentType);
|
|
203
|
+
const { props: originProps, events } = normalizeProps(toJS(field.component[1]) || {});
|
|
204
|
+
const originChange = events.change;
|
|
205
|
+
const originFocus = events.focus;
|
|
206
|
+
const originBlur = events.blur;
|
|
207
|
+
events.change = (...args) => {
|
|
208
|
+
if (!isVoidField(field)) field.onInput(...args);
|
|
209
|
+
originChange?.(...args);
|
|
210
|
+
};
|
|
211
|
+
events.focus = (...args) => {
|
|
212
|
+
if (!isVoidField(field)) field.onFocus(...args);
|
|
213
|
+
originFocus?.(...args);
|
|
214
|
+
};
|
|
215
|
+
events.blur = (...args) => {
|
|
216
|
+
if (!isVoidField(field)) field.onBlur(...args);
|
|
217
|
+
originBlur?.(...args);
|
|
164
218
|
};
|
|
165
|
-
|
|
166
|
-
...
|
|
167
|
-
disabled:
|
|
168
|
-
readOnly:
|
|
219
|
+
const componentProps = {
|
|
220
|
+
...isVoidField(field) ? {} : {
|
|
221
|
+
disabled: field.pattern === "disabled" || field.pattern === "readPretty",
|
|
222
|
+
readOnly: field.pattern === "readOnly"
|
|
169
223
|
},
|
|
170
|
-
...
|
|
171
|
-
...
|
|
172
|
-
...
|
|
224
|
+
...originProps,
|
|
225
|
+
...isVoidField(field) ? {} : { value: field.value },
|
|
226
|
+
...toVueEventProps(events)
|
|
173
227
|
};
|
|
174
|
-
|
|
228
|
+
if (isStr(Component)) return h(Component, componentProps, Object.keys(mergedSlots).map((name) => mergedSlots[name]()));
|
|
229
|
+
return h(Component, componentProps, mergedSlots);
|
|
175
230
|
}
|
|
176
|
-
return
|
|
231
|
+
return renderDecorator(renderComponent());
|
|
177
232
|
};
|
|
178
233
|
}
|
|
179
234
|
});
|
|
180
235
|
// src/components/RecursionField.tsx
|
|
181
|
-
function
|
|
182
|
-
|
|
183
|
-
if (!
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
return
|
|
236
|
+
function resolveEmptySlot(slots) {
|
|
237
|
+
const names = Object.keys(slots);
|
|
238
|
+
if (!names.length) return null;
|
|
239
|
+
const children = names.reduce((result, name) => {
|
|
240
|
+
const value = slots[name]();
|
|
241
|
+
return result.concat(Array.isArray(value) ? value : [value]);
|
|
187
242
|
}, []);
|
|
188
|
-
return
|
|
243
|
+
return h("div", { style: { display: "contents" } }, children);
|
|
189
244
|
}
|
|
190
|
-
var
|
|
245
|
+
var RecursionField = /* @__PURE__ */ defineComponent({
|
|
191
246
|
name: "RecursionField",
|
|
192
|
-
inheritAttrs:
|
|
247
|
+
inheritAttrs: false,
|
|
193
248
|
props: {
|
|
194
|
-
schema: { required:
|
|
249
|
+
schema: { required: true },
|
|
195
250
|
name: [String, Number],
|
|
196
251
|
basePath: {},
|
|
197
252
|
onlyRenderProperties: {
|
|
@@ -205,84 +260,108 @@ var J = /* @__PURE__ */ f({
|
|
|
205
260
|
mapProperties: {},
|
|
206
261
|
filterProperties: {}
|
|
207
262
|
},
|
|
208
|
-
setup(
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
263
|
+
setup(props) {
|
|
264
|
+
const parentRef = useField();
|
|
265
|
+
const optionsRef = inject(SchemaOptionsSymbol, ref({}));
|
|
266
|
+
const scopeRef = inject(SchemaExpressionScopeSymbol, ref({}));
|
|
267
|
+
const fieldSchemaRef = computed(() => {
|
|
268
|
+
const schema = props.schema;
|
|
269
|
+
return markRaw(Schema.isSchemaInstance(schema) ? schema : new Schema(schema));
|
|
212
270
|
});
|
|
213
|
-
function
|
|
214
|
-
|
|
215
|
-
return
|
|
216
|
-
...
|
|
271
|
+
function getPropsFromSchema(schema) {
|
|
272
|
+
const options = optionsRef.value;
|
|
273
|
+
return schema.toFieldProps({
|
|
274
|
+
...options,
|
|
217
275
|
get scope() {
|
|
218
|
-
return
|
|
276
|
+
return lazyMerge(options.scope, scopeRef.value);
|
|
219
277
|
}
|
|
220
278
|
});
|
|
221
279
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
280
|
+
const fieldPropsRef = shallowRef(getPropsFromSchema(fieldSchemaRef.value));
|
|
281
|
+
watch([fieldSchemaRef, optionsRef], () => {
|
|
282
|
+
fieldPropsRef.value = getPropsFromSchema(fieldSchemaRef.value);
|
|
225
283
|
});
|
|
226
|
-
function
|
|
227
|
-
|
|
284
|
+
function getBasePath() {
|
|
285
|
+
if (props.onlyRenderProperties) return props.basePath ?? (isValid(props.name) ? (parentRef.value?.address ?? "").concat(props.name) : parentRef.value?.address);
|
|
286
|
+
return props.basePath ?? parentRef.value?.address;
|
|
228
287
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
if (
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
288
|
+
provide(SchemaSymbol, fieldSchemaRef);
|
|
289
|
+
return () => {
|
|
290
|
+
const schema = fieldSchemaRef.value;
|
|
291
|
+
if (!schema) return null;
|
|
292
|
+
const basePath = getBasePath();
|
|
293
|
+
const fieldProps = fieldPropsRef.value;
|
|
294
|
+
function generateSlotsByProperties(scoped = false) {
|
|
295
|
+
if (props.onlyRenderSelf) return {};
|
|
296
|
+
const properties = Schema.getOrderProperties(schema);
|
|
297
|
+
if (!properties.length) return {};
|
|
298
|
+
const renderMap = {};
|
|
299
|
+
function setRender(key, value) {
|
|
300
|
+
if (!renderMap[key]) renderMap[key] = [];
|
|
301
|
+
renderMap[key].push(value);
|
|
240
302
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
303
|
+
const mapProperties = props.mapProperties;
|
|
304
|
+
const filterProperties = props.filterProperties;
|
|
305
|
+
properties.forEach(({ schema: item, key: name }, index) => {
|
|
306
|
+
let itemSchema = item;
|
|
307
|
+
if (isFn(mapProperties)) {
|
|
308
|
+
const mapped = mapProperties(item, name);
|
|
309
|
+
if (mapped) itemSchema = mapped;
|
|
247
310
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
311
|
+
if (isFn(filterProperties)) {
|
|
312
|
+
if (filterProperties(itemSchema, name) === false) return;
|
|
313
|
+
}
|
|
314
|
+
setRender(itemSchema.slot ?? "default", (field) => h(RecursionField, {
|
|
315
|
+
key: `${index}-${name}`,
|
|
316
|
+
schema: itemSchema,
|
|
317
|
+
name,
|
|
318
|
+
basePath: field?.address ?? basePath
|
|
253
319
|
}));
|
|
254
320
|
});
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
})
|
|
321
|
+
const slots = {};
|
|
322
|
+
Object.keys(renderMap).forEach((key) => {
|
|
323
|
+
const renderFns = renderMap[key];
|
|
324
|
+
slots[key] = scoped ? ({ field } = {}) => renderFns.map((renderFn) => renderFn(field)) : () => renderFns.map((renderFn) => renderFn());
|
|
325
|
+
});
|
|
326
|
+
return slots;
|
|
260
327
|
}
|
|
261
|
-
if (!
|
|
262
|
-
|
|
263
|
-
...
|
|
264
|
-
name:
|
|
265
|
-
basePath
|
|
328
|
+
if (!isValid(props.name)) return resolveEmptySlot(generateSlotsByProperties());
|
|
329
|
+
const commonFieldProps = {
|
|
330
|
+
...fieldProps,
|
|
331
|
+
name: props.name,
|
|
332
|
+
basePath
|
|
266
333
|
};
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
334
|
+
if (schema.type === "object") {
|
|
335
|
+
if (props.onlyRenderProperties) return resolveEmptySlot(generateSlotsByProperties());
|
|
336
|
+
return h(ReactiveField, {
|
|
337
|
+
fieldType: "ObjectField",
|
|
338
|
+
fieldProps: commonFieldProps
|
|
339
|
+
}, generateSlotsByProperties(true));
|
|
340
|
+
}
|
|
341
|
+
if (schema.type === "array") return h(ReactiveField, {
|
|
271
342
|
fieldType: "ArrayField",
|
|
272
|
-
fieldProps:
|
|
273
|
-
})
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
343
|
+
fieldProps: commonFieldProps
|
|
344
|
+
});
|
|
345
|
+
if (schema.type === "void") {
|
|
346
|
+
if (props.onlyRenderProperties) return resolveEmptySlot(generateSlotsByProperties());
|
|
347
|
+
return h(ReactiveField, {
|
|
348
|
+
fieldType: "VoidField",
|
|
349
|
+
fieldProps: commonFieldProps
|
|
350
|
+
}, generateSlotsByProperties(true));
|
|
351
|
+
}
|
|
352
|
+
return h(ReactiveField, {
|
|
277
353
|
fieldType: "Field",
|
|
278
|
-
fieldProps:
|
|
354
|
+
fieldProps: commonFieldProps
|
|
279
355
|
});
|
|
280
356
|
};
|
|
281
357
|
}
|
|
282
|
-
})
|
|
358
|
+
});
|
|
359
|
+
// src/components/SchemaField.tsx
|
|
360
|
+
/** @jsxImportSource vue */
|
|
361
|
+
var schemaFieldProps = {
|
|
283
362
|
schema: {
|
|
284
363
|
type: Object,
|
|
285
|
-
required:
|
|
364
|
+
required: true
|
|
286
365
|
},
|
|
287
366
|
components: { type: Object },
|
|
288
367
|
scope: { type: Object },
|
|
@@ -309,196 +388,246 @@ var J = /* @__PURE__ */ f({
|
|
|
309
388
|
mapProperties: { type: Function },
|
|
310
389
|
filterProperties: { type: Function }
|
|
311
390
|
};
|
|
312
|
-
function
|
|
313
|
-
return
|
|
391
|
+
function joinPath(prefix, segment) {
|
|
392
|
+
return prefix ? `${prefix}.${segment}` : String(segment);
|
|
314
393
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
394
|
+
/**
|
|
395
|
+
* 收集 schema 声明的字段路径,用于热更新时定位需要销毁的字段。
|
|
396
|
+
* 数组元素的下标是运行时产物,这里统一用 * 通配。
|
|
397
|
+
*/
|
|
398
|
+
function collectSchemaPaths(schema, basePath, name) {
|
|
399
|
+
const fields = [];
|
|
400
|
+
const arrays = [];
|
|
401
|
+
const walkNode = (node, path) => {
|
|
402
|
+
if (node.type === "array") arrays.push(joinPath(path, "*"));
|
|
403
|
+
walkChildren(node, path);
|
|
404
|
+
};
|
|
405
|
+
const walkChildren = (node, prefix) => {
|
|
406
|
+
const properties = node.properties ?? {};
|
|
407
|
+
Object.keys(properties).forEach((key) => {
|
|
408
|
+
const path = joinPath(prefix, key);
|
|
409
|
+
fields.push(path);
|
|
410
|
+
walkNode(properties[key], path);
|
|
323
411
|
});
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
412
|
+
const items = node.items;
|
|
413
|
+
if (items) (Array.isArray(items) ? items : [items]).forEach((item) => {
|
|
414
|
+
if (item) walkChildren(item, joinPath(prefix, "*"));
|
|
327
415
|
});
|
|
328
416
|
};
|
|
329
|
-
if (
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
417
|
+
if (isValid(name)) {
|
|
418
|
+
const rootPath = joinPath(basePath, String(name));
|
|
419
|
+
fields.push(rootPath);
|
|
420
|
+
walkNode(schema, rootPath);
|
|
421
|
+
} else walkChildren(schema, basePath);
|
|
333
422
|
return {
|
|
334
|
-
fields
|
|
335
|
-
arrays
|
|
423
|
+
fields,
|
|
424
|
+
arrays
|
|
336
425
|
};
|
|
337
426
|
}
|
|
338
|
-
function
|
|
339
|
-
return
|
|
427
|
+
function toSchema(schema) {
|
|
428
|
+
return Schema.isSchemaInstance(schema) ? schema : new Schema({
|
|
340
429
|
type: "object",
|
|
341
|
-
...
|
|
430
|
+
...schema
|
|
342
431
|
});
|
|
343
432
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
433
|
+
/**
|
|
434
|
+
* 热更新:销毁旧 schema 声明的字段(保留值),字段会在 fieldProps 更新后重建。
|
|
435
|
+
*/
|
|
436
|
+
function applyHotReload(form, prev, next, basePath, name) {
|
|
437
|
+
const prevPaths = collectSchemaPaths(prev, basePath, name);
|
|
438
|
+
const nextPaths = collectSchemaPaths(next, basePath, name);
|
|
439
|
+
const nextFields = new Set(nextPaths.fields);
|
|
440
|
+
prevPaths.fields.forEach((path) => {
|
|
441
|
+
if (nextFields.has(path)) return;
|
|
442
|
+
form.deleteValuesIn(path);
|
|
443
|
+
form.deleteInitialValuesIn(path);
|
|
444
|
+
});
|
|
445
|
+
(/* @__PURE__ */ new Set([
|
|
446
|
+
...prevPaths.fields,
|
|
447
|
+
...nextPaths.fields,
|
|
448
|
+
...prevPaths.arrays,
|
|
449
|
+
...nextPaths.arrays
|
|
450
|
+
])).forEach((pattern) => {
|
|
451
|
+
form.clearFormGraph(pattern, false);
|
|
355
452
|
});
|
|
356
453
|
}
|
|
357
|
-
function
|
|
358
|
-
if (!
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
454
|
+
function setupHotReload(props) {
|
|
455
|
+
if (!props.hotReload) return;
|
|
456
|
+
const formRef = useForm();
|
|
457
|
+
const parentRef = useField();
|
|
458
|
+
watch(() => props.schema, (next, prev) => {
|
|
459
|
+
const form = formRef.value;
|
|
460
|
+
if (!form || !prev || !next) return;
|
|
461
|
+
const basePath = props.basePath ? FormPath.parse(props.basePath).toString() : parentRef.value?.address?.toString() ?? "";
|
|
462
|
+
applyHotReload(form, toSchema(prev), toSchema(next), basePath, props.name);
|
|
365
463
|
}, { flush: "pre" });
|
|
366
464
|
}
|
|
367
|
-
function
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
465
|
+
function renderSchemaField(props, prebound) {
|
|
466
|
+
const schemaRef = computed(() => toSchema(props.schema));
|
|
467
|
+
setupHotReload(props);
|
|
468
|
+
const optionsRef = computed(() => ({
|
|
371
469
|
components: {
|
|
372
|
-
...
|
|
373
|
-
...
|
|
470
|
+
...prebound?.components,
|
|
471
|
+
...props.components
|
|
374
472
|
},
|
|
375
473
|
scope: {
|
|
376
|
-
...
|
|
377
|
-
...
|
|
474
|
+
...prebound?.scope,
|
|
475
|
+
...props.scope
|
|
378
476
|
}
|
|
379
|
-
}))
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
477
|
+
}));
|
|
478
|
+
const scopeRef = computed(() => optionsRef.value.scope);
|
|
479
|
+
provide(SchemaOptionsSymbol, optionsRef);
|
|
480
|
+
provide(SchemaExpressionScopeSymbol, scopeRef);
|
|
481
|
+
return () => createVNode(RecursionField, {
|
|
482
|
+
"schema": schemaRef.value,
|
|
483
|
+
"name": props.name,
|
|
484
|
+
"basePath": props.basePath,
|
|
485
|
+
"onlyRenderProperties": props.onlyRenderProperties,
|
|
486
|
+
"onlyRenderSelf": props.onlyRenderSelf,
|
|
487
|
+
"mapProperties": props.mapProperties,
|
|
488
|
+
"filterProperties": props.filterProperties
|
|
388
489
|
}, null);
|
|
389
490
|
}
|
|
390
|
-
var
|
|
491
|
+
var SchemaField = /* @__PURE__ */ defineComponent({
|
|
391
492
|
name: "SchemaField",
|
|
392
|
-
inheritAttrs:
|
|
393
|
-
props:
|
|
394
|
-
setup(
|
|
395
|
-
return
|
|
493
|
+
inheritAttrs: false,
|
|
494
|
+
props: schemaFieldProps,
|
|
495
|
+
setup(props) {
|
|
496
|
+
return renderSchemaField(props);
|
|
396
497
|
}
|
|
397
498
|
});
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
499
|
+
/**
|
|
500
|
+
* 基于扁平组件映射与分类组件 map 创建 SchemaField + fragment:
|
|
501
|
+
* - voidComponents / arrayComponents 只接收组件 map,分类名由工厂内部提取后传给 fragment
|
|
502
|
+
* - 分类组件会自动合并进运行时组件映射,无需在 components 中重复注册
|
|
503
|
+
* - 返回的 SchemaField 已预绑定拍平后的组件与 scope,props 仍可传扁平 map 覆盖同名组件
|
|
504
|
+
* - fragment 包含 Field / Object / Array / Void / transform 与按组件名生成的动态构造器
|
|
505
|
+
* 组件 props、字段值与 scope 类型全部自动推断,无需手写类型参数。
|
|
506
|
+
*/
|
|
507
|
+
function createSchemaField(options = {}) {
|
|
508
|
+
const voidComponents = options.voidComponents ?? {};
|
|
509
|
+
const arrayComponents = options.arrayComponents ?? {};
|
|
510
|
+
const flatComponents = {
|
|
511
|
+
...options.components,
|
|
512
|
+
...voidComponents,
|
|
513
|
+
...arrayComponents
|
|
514
|
+
};
|
|
515
|
+
const fragment = createFragment({
|
|
516
|
+
patches: options.patches,
|
|
517
|
+
voidComponentNames: Object.keys(voidComponents),
|
|
518
|
+
arrayComponentNames: Object.keys(arrayComponents),
|
|
519
|
+
defaultDecorator: options.defaultDecorator
|
|
520
|
+
});
|
|
521
|
+
const prebound = {
|
|
522
|
+
components: flatComponents,
|
|
523
|
+
scope: options.scope
|
|
411
524
|
};
|
|
412
525
|
return {
|
|
413
|
-
SchemaField: /* @__PURE__ */
|
|
526
|
+
SchemaField: /* @__PURE__ */ defineComponent({
|
|
414
527
|
name: "SchemaField",
|
|
415
|
-
inheritAttrs:
|
|
416
|
-
props:
|
|
417
|
-
setup(
|
|
418
|
-
return
|
|
528
|
+
inheritAttrs: false,
|
|
529
|
+
props: schemaFieldProps,
|
|
530
|
+
setup(props) {
|
|
531
|
+
return renderSchemaField(props, prebound);
|
|
419
532
|
}
|
|
420
533
|
}),
|
|
421
|
-
fragment
|
|
534
|
+
fragment
|
|
422
535
|
};
|
|
423
536
|
}
|
|
424
537
|
// src/hooks/useFieldSchema.ts
|
|
425
|
-
function
|
|
426
|
-
return
|
|
538
|
+
function useFieldSchema() {
|
|
539
|
+
return inject(SchemaSymbol, ref());
|
|
427
540
|
}
|
|
428
541
|
// src/hooks/useFormEffects.ts
|
|
429
|
-
function
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
542
|
+
function useFormEffects(effects) {
|
|
543
|
+
const formRef = useForm();
|
|
544
|
+
const stop = watchEffect((onCleanup) => {
|
|
545
|
+
const form = formRef.value;
|
|
546
|
+
if (!form) return;
|
|
547
|
+
const id = uid();
|
|
548
|
+
form.addEffects(id, effects);
|
|
549
|
+
onCleanup(() => {
|
|
550
|
+
form.removeEffects(id);
|
|
436
551
|
});
|
|
437
552
|
});
|
|
438
|
-
|
|
553
|
+
onBeforeUnmount(() => stop());
|
|
439
554
|
}
|
|
440
555
|
// src/hooks/useParentForm.ts
|
|
441
|
-
function
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
556
|
+
function useParentForm() {
|
|
557
|
+
const field = useField();
|
|
558
|
+
const form = useForm();
|
|
559
|
+
function findObjectParent(target) {
|
|
560
|
+
if (!target) return form.value;
|
|
561
|
+
if (isObjectField(target)) return target;
|
|
562
|
+
return findObjectParent(target.parent);
|
|
445
563
|
}
|
|
446
|
-
return
|
|
564
|
+
return computed(() => findObjectParent(field.value));
|
|
447
565
|
}
|
|
448
566
|
// src/shared/connect.tsx
|
|
449
|
-
function
|
|
450
|
-
function
|
|
451
|
-
return
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
567
|
+
function mapProps(...args) {
|
|
568
|
+
function transform(input, field) {
|
|
569
|
+
return args.reduce((props, mapper) => {
|
|
570
|
+
if (isFn(mapper)) props = Object.assign(props, mapper(props, field));
|
|
571
|
+
else each(mapper, (to, extract) => {
|
|
572
|
+
const extractValue = FormPath.getIn(field, extract);
|
|
573
|
+
const targetValue = isStr(to) ? to : extract;
|
|
574
|
+
const originalValue = FormPath.getIn(props, targetValue);
|
|
575
|
+
if (extract === "value") {
|
|
576
|
+
if (to !== extract) delete props.value;
|
|
577
|
+
}
|
|
578
|
+
if (isValid(originalValue) && !isValid(extractValue)) return;
|
|
579
|
+
FormPath.setIn(props, targetValue, extractValue);
|
|
580
|
+
});
|
|
581
|
+
return props;
|
|
582
|
+
}, input);
|
|
455
583
|
}
|
|
456
|
-
return function(
|
|
457
|
-
return /* @__PURE__ */
|
|
458
|
-
name:
|
|
459
|
-
inheritAttrs:
|
|
460
|
-
setup(
|
|
461
|
-
|
|
584
|
+
return function(target) {
|
|
585
|
+
return /* @__PURE__ */ defineComponent({
|
|
586
|
+
name: target.name ? `Connected${target.name}` : "ConnectedComponent",
|
|
587
|
+
inheritAttrs: false,
|
|
588
|
+
setup(_, { attrs, slots }) {
|
|
589
|
+
const fieldRef = useField();
|
|
462
590
|
return () => {
|
|
463
|
-
|
|
464
|
-
return
|
|
591
|
+
const newAttrs = fieldRef.value ? transform({ ...attrs }, fieldRef.value) : { ...attrs };
|
|
592
|
+
return h(target, newAttrs, slots);
|
|
465
593
|
};
|
|
466
594
|
}
|
|
467
595
|
});
|
|
468
596
|
};
|
|
469
597
|
}
|
|
470
|
-
function
|
|
471
|
-
return function(
|
|
472
|
-
return /* @__PURE__ */
|
|
473
|
-
name:
|
|
474
|
-
inheritAttrs:
|
|
475
|
-
setup(
|
|
476
|
-
|
|
598
|
+
function mapReadPretty(component, readPrettyProps) {
|
|
599
|
+
return function(target) {
|
|
600
|
+
return /* @__PURE__ */ defineComponent({
|
|
601
|
+
name: target.name ? `Read${target.name}` : "ReadComponent",
|
|
602
|
+
inheritAttrs: false,
|
|
603
|
+
setup(_, { attrs, slots }) {
|
|
604
|
+
const fieldRef = useField();
|
|
477
605
|
return () => {
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
...
|
|
482
|
-
|
|
606
|
+
const field = fieldRef.value;
|
|
607
|
+
const Current = field && !isVoidField(field) && field.pattern === "readPretty" ? component : target;
|
|
608
|
+
return h(Current, {
|
|
609
|
+
...readPrettyProps,
|
|
610
|
+
...attrs
|
|
611
|
+
}, slots);
|
|
483
612
|
};
|
|
484
613
|
}
|
|
485
614
|
});
|
|
486
615
|
};
|
|
487
616
|
}
|
|
488
|
-
function
|
|
489
|
-
|
|
490
|
-
return
|
|
491
|
-
name:
|
|
492
|
-
inheritAttrs:
|
|
493
|
-
setup(
|
|
494
|
-
return () =>
|
|
617
|
+
function connect(target, ...args) {
|
|
618
|
+
const Component = args.reduce((current, mapper) => mapper(current), target);
|
|
619
|
+
return markRaw(/* @__PURE__ */ defineComponent({
|
|
620
|
+
name: target.name,
|
|
621
|
+
inheritAttrs: false,
|
|
622
|
+
setup(_, { attrs, slots }) {
|
|
623
|
+
return () => h(Component, attrs, slots);
|
|
495
624
|
}
|
|
496
625
|
}));
|
|
497
626
|
}
|
|
498
627
|
// src/shared/createForm.ts
|
|
499
|
-
function
|
|
500
|
-
return
|
|
628
|
+
function createForm(options) {
|
|
629
|
+
return markRaw(createForm$1(options));
|
|
501
630
|
}
|
|
502
|
-
export {
|
|
631
|
+
export { ExpressionScope, FormConsumer, FormProvider, RecursionField, SchemaField, connect, createForm, createSchemaField, mapProps, mapReadPretty, useAttach, useField, useFieldSchema, useForm, useFormEffects, useInjectionCleaner, useParentForm };
|
|
503
632
|
|
|
504
633
|
//# sourceMappingURL=index.js.map
|