@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/dist/index.js CHANGED
@@ -1,122 +1,159 @@
1
- import { Schema as e, createFragment as t } from "@formvk/schema";
2
- import { FormPath as n, each as r, isFn as i, isStr as a, isValid as o, lazyMerge as s, uid as c } from "@formvk/shared";
3
- import { Fragment as l, computed as u, createVNode as d, defineComponent as f, h as p, inject as m, isVNode as h, markRaw as g, nextTick as _, onBeforeUnmount as v, onMounted as ee, onUnmounted as te, provide as y, ref as b, shallowRef as x, toRaw as S, toRef as ne, watch as C, watchEffect as re } from "vue";
4
- import { createForm as w, isObjectField as T, isVoidField as E } from "@formvk/core";
5
- import { toJS as D } from "@formvk/reactive";
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 O = Symbol("form"), k = Symbol("field"), A = Symbol("schema"), j = Symbol("schemaExpression"), M = Symbol("schemaOptions"), ie = /* @__PURE__ */ f({
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: !1,
16
+ inheritAttrs: false,
11
17
  props: ["value"],
12
- setup(e, { slots: t }) {
13
- let n = m(j, b({})), r = u(() => s(n.value, e.value));
14
- return y(j, r), () => t.default?.();
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 N() {
19
- return m(O, b());
26
+ function useForm() {
27
+ return inject(FormSymbol, ref());
20
28
  }
21
29
  // src/components/FormConsumer.tsx
22
- var P = /* @__PURE__ */ f({
30
+ var FormConsumer = /* @__PURE__ */ defineComponent({
23
31
  name: "FormConsumer",
24
- inheritAttrs: !1,
32
+ inheritAttrs: false,
25
33
  slots: Object,
26
- setup(e, { slots: t }) {
27
- let n = N();
28
- return () => t.default?.({ form: n.value }) ?? null;
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 F(e) {
33
- return C(e, (e, t, n) => {
34
- e && e !== t && (t?.onUnmount(), _(() => e.onMount()), n(() => e.onUnmount()));
35
- }), ee(() => {
36
- e.value?.onMount();
37
- }), te(() => {
38
- e.value?.onUnmount();
39
- }), e;
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 I(e) {
43
- e.forEach((e) => y(e, b()));
57
+ function useInjectionCleaner(injectionKeys) {
58
+ injectionKeys.forEach((key) => provide(key, ref()));
44
59
  }
45
60
  // src/components/FormProvider.tsx
46
- var ae = /* @__PURE__ */ f({
61
+ var FormProvider = /* @__PURE__ */ defineComponent({
47
62
  name: "FormProvider",
48
- inheritAttrs: !1,
63
+ inheritAttrs: false,
49
64
  props: ["form"],
50
- setup(e, { slots: t }) {
51
- let n = F(ne(e, "form"));
52
- return y(O, n), I([
53
- k,
54
- A,
55
- j,
56
- M
57
- ]), () => t.default?.();
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 L() {
62
- return m(k, b());
78
+ function useField() {
79
+ return inject(FieldSymbol, ref());
63
80
  }
64
81
  // src/utils/getRawComponent.ts
65
- function R(e, t) {
66
- let r = a(t) ? n.getIn(e, t) : void 0;
67
- return S(r ?? t);
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 z(e) {
71
- return e ? i(e) ? !0 : typeof e == "object" && (i(e.setup) || i(e.render) || a(e.template)) : !1;
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 B(e) {
74
- return Array.isArray(e) ? e : e == null ? [] : [e];
96
+ function toArray(value) {
97
+ if (Array.isArray(value)) return value;
98
+ return value === void 0 || value === null ? [] : [value];
75
99
  }
76
- function V(e) {
77
- return Array.isArray(e) ? e.length > 1 ? p(l, null, e) : e[0] ?? null : e;
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 H(e) {
80
- return h(e) || a(e) ? e : z(e) ? p(e) : e;
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 U(e, t, n) {
83
- let i = {};
84
- if (Object.keys(t).forEach((n) => {
85
- i[n] = (r) => t[n]?.({
86
- field: e,
87
- form: e.form,
88
- ...r
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
- }), !n) return i;
91
- function o(e, t) {
92
- let n = i[e];
93
- i[e] = (e) => [...B(n?.(e)), ...B(H(t))];
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
- return a(n) || h(n) || z(n) ? o("default", n) : typeof n == "object" && r(n, (e, t) => {
96
- o(t, e);
97
- }), i;
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 W(e) {
100
- let t = {}, n = {};
101
- return r(e, (e, r) => {
102
- if (r.startsWith("on")) {
103
- let t = `${r[2].toLowerCase()}${r.slice(3)}`;
104
- n[t] = n[t] ?? e;
105
- } else r.startsWith("@") ? n[r.slice(1)] = e : t[r] = e;
106
- }), {
107
- props: t,
108
- events: n
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 G(e) {
112
- let t = {};
113
- return r(e, (e, n) => {
114
- t[`on${n.charAt(0).toUpperCase()}${n.slice(1)}`] = e;
115
- }), t;
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 K = /* @__PURE__ */ f({
154
+ var ReactiveField = /* @__PURE__ */ defineComponent({
118
155
  name: "ReactiveField",
119
- inheritAttrs: !1,
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(e, { slots: t }) {
131
- let n = N(), r = L(), i = m(M, b({}));
132
- function o() {
133
- let t = e.fieldType ?? "Field", i = e.fieldProps ?? {};
134
- return n.value?.[`create${t}`]?.({
135
- ...i,
136
- basePath: i.basePath ?? r.value?.address
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
- let s = x(o());
140
- return C(() => e.fieldProps, () => {
141
- s.value = o();
142
- }), F(s), y(k, s), () => {
143
- let e = s.value, n = i.value;
144
- if (!e) return t.default?.();
145
- if (e.display !== "visible") return null;
146
- let r = U(e, t, e.content);
147
- function o(t) {
148
- if (!e.decoratorType) return V(t);
149
- let r = R(n?.components, e.decoratorType), { props: i, events: a } = W(D(e.decorator[1]) || {});
150
- return p(r, {
151
- ...i,
152
- ...G(a)
153
- }, { default: () => t });
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 c() {
156
- if (!e.componentType) return r.default?.() ?? null;
157
- let t = R(n?.components, e.componentType), { props: i, events: o } = W(D(e.component[1]) || {}), s = o.change, c = o.focus, l = o.blur;
158
- o.change = (...t) => {
159
- E(e) || e.onInput(...t), s?.(...t);
160
- }, o.focus = (...t) => {
161
- E(e) || e.onFocus(...t), c?.(...t);
162
- }, o.blur = (...t) => {
163
- E(e) || e.onBlur(...t), l?.(...t);
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
- let u = {
166
- ...E(e) ? {} : {
167
- disabled: e.pattern === "disabled" || e.pattern === "readPretty",
168
- readOnly: e.pattern === "readOnly"
219
+ const componentProps = {
220
+ ...isVoidField(field) ? {} : {
221
+ disabled: field.pattern === "disabled" || field.pattern === "readPretty",
222
+ readOnly: field.pattern === "readOnly"
169
223
  },
170
- ...i,
171
- ...E(e) ? {} : { value: e.value },
172
- ...G(o)
224
+ ...originProps,
225
+ ...isVoidField(field) ? {} : { value: field.value },
226
+ ...toVueEventProps(events)
173
227
  };
174
- return a(t) ? p(t, u, Object.keys(r).map((e) => r[e]())) : p(t, u, r);
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 o(c());
231
+ return renderDecorator(renderComponent());
177
232
  };
178
233
  }
179
234
  });
180
235
  // src/components/RecursionField.tsx
181
- function q(e) {
182
- let t = Object.keys(e);
183
- if (!t.length) return null;
184
- let n = t.reduce((t, n) => {
185
- let r = e[n]();
186
- return t.concat(Array.isArray(r) ? r : [r]);
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 p("div", { style: { display: "contents" } }, n);
243
+ return h("div", { style: { display: "contents" } }, children);
189
244
  }
190
- var J = /* @__PURE__ */ f({
245
+ var RecursionField = /* @__PURE__ */ defineComponent({
191
246
  name: "RecursionField",
192
- inheritAttrs: !1,
247
+ inheritAttrs: false,
193
248
  props: {
194
- schema: { required: !0 },
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(t) {
209
- let n = L(), r = m(M, b({})), a = m(j, b({})), c = u(() => {
210
- let n = t.schema;
211
- return g(e.isSchemaInstance(n) ? n : new e(n));
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 l(e) {
214
- let t = r.value;
215
- return e.toFieldProps({
216
- ...t,
271
+ function getPropsFromSchema(schema) {
272
+ const options = optionsRef.value;
273
+ return schema.toFieldProps({
274
+ ...options,
217
275
  get scope() {
218
- return s(t.scope, a.value);
276
+ return lazyMerge(options.scope, scopeRef.value);
219
277
  }
220
278
  });
221
279
  }
222
- let d = x(l(c.value));
223
- C([c, r], () => {
224
- d.value = l(c.value);
280
+ const fieldPropsRef = shallowRef(getPropsFromSchema(fieldSchemaRef.value));
281
+ watch([fieldSchemaRef, optionsRef], () => {
282
+ fieldPropsRef.value = getPropsFromSchema(fieldSchemaRef.value);
225
283
  });
226
- function f() {
227
- return t.onlyRenderProperties ? t.basePath ?? (o(t.name) ? (n.value?.address ?? "").concat(t.name) : n.value?.address) : t.basePath ?? n.value?.address;
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
- return y(A, c), () => {
230
- let n = c.value;
231
- if (!n) return null;
232
- let r = f(), a = d.value;
233
- function s(a = !1) {
234
- if (t.onlyRenderSelf) return {};
235
- let o = e.getOrderProperties(n);
236
- if (!o.length) return {};
237
- let s = {};
238
- function c(e, t) {
239
- s[e] || (s[e] = []), s[e].push(t);
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
- let l = t.mapProperties, u = t.filterProperties;
242
- o.forEach(({ schema: e, key: t }, n) => {
243
- let a = e;
244
- if (i(l)) {
245
- let n = l(e, t);
246
- n && (a = n);
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
- i(u) && u(a, t) === !1 || c(a.slot ?? "default", (e) => p(J, {
249
- key: `${n}-${t}`,
250
- schema: a,
251
- name: t,
252
- basePath: e?.address ?? r
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
- let d = {};
256
- return Object.keys(s).forEach((e) => {
257
- let t = s[e];
258
- d[e] = a ? ({ field: e } = {}) => t.map((t) => t(e)) : () => t.map((e) => e());
259
- }), d;
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 (!o(t.name)) return q(s());
262
- let l = {
263
- ...a,
264
- name: t.name,
265
- basePath: r
328
+ if (!isValid(props.name)) return resolveEmptySlot(generateSlotsByProperties());
329
+ const commonFieldProps = {
330
+ ...fieldProps,
331
+ name: props.name,
332
+ basePath
266
333
  };
267
- return n.type === "object" ? t.onlyRenderProperties ? q(s()) : p(K, {
268
- fieldType: "ObjectField",
269
- fieldProps: l
270
- }, s(!0)) : n.type === "array" ? p(K, {
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: l
273
- }) : n.type === "void" ? t.onlyRenderProperties ? q(s()) : p(K, {
274
- fieldType: "VoidField",
275
- fieldProps: l
276
- }, s(!0)) : p(K, {
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: l
354
+ fieldProps: commonFieldProps
279
355
  });
280
356
  };
281
357
  }
282
- }), Y = {
358
+ });
359
+ // src/components/SchemaField.tsx
360
+ /** @jsxImportSource vue */
361
+ var schemaFieldProps = {
283
362
  schema: {
284
363
  type: Object,
285
- required: !0
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 X(e, t) {
313
- return e ? `${e}.${t}` : String(t);
391
+ function joinPath(prefix, segment) {
392
+ return prefix ? `${prefix}.${segment}` : String(segment);
314
393
  }
315
- function Z(e, t, n) {
316
- let r = [], i = [], a = (e, t) => {
317
- e.type === "array" && i.push(X(t, "*")), s(e, t);
318
- }, s = (e, t) => {
319
- let n = e.properties ?? {};
320
- Object.keys(n).forEach((e) => {
321
- let i = X(t, e);
322
- r.push(i), a(n[e], i);
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
- let i = e.items;
325
- i && (Array.isArray(i) ? i : [i]).forEach((e) => {
326
- e && s(e, X(t, "*"));
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 (o(n)) {
330
- let i = X(t, String(n));
331
- r.push(i), a(e, i);
332
- } else s(e, t);
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: r,
335
- arrays: i
423
+ fields,
424
+ arrays
336
425
  };
337
426
  }
338
- function Q(t) {
339
- return e.isSchemaInstance(t) ? t : new e({
427
+ function toSchema(schema) {
428
+ return Schema.isSchemaInstance(schema) ? schema : new Schema({
340
429
  type: "object",
341
- ...t
430
+ ...schema
342
431
  });
343
432
  }
344
- function oe(e, t, n, r, i) {
345
- let a = Z(t, r, i), o = Z(n, r, i), s = new Set(o.fields);
346
- a.fields.forEach((t) => {
347
- s.has(t) || (e.deleteValuesIn(t), e.deleteInitialValuesIn(t));
348
- }), (/* @__PURE__ */ new Set([
349
- ...a.fields,
350
- ...o.fields,
351
- ...a.arrays,
352
- ...o.arrays
353
- ])).forEach((t) => {
354
- e.clearFormGraph(t, !1);
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 se(e) {
358
- if (!e.hotReload) return;
359
- let t = N(), r = L();
360
- C(() => e.schema, (i, a) => {
361
- let o = t.value;
362
- if (!o || !a || !i) return;
363
- let s = e.basePath ? n.parse(e.basePath).toString() : r.value?.address?.toString() ?? "";
364
- oe(o, Q(a), Q(i), s, e.name);
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 $(e, t) {
368
- let n = u(() => Q(e.schema));
369
- se(e);
370
- let r = u(() => ({
465
+ function renderSchemaField(props, prebound) {
466
+ const schemaRef = computed(() => toSchema(props.schema));
467
+ setupHotReload(props);
468
+ const optionsRef = computed(() => ({
371
469
  components: {
372
- ...t?.components,
373
- ...e.components
470
+ ...prebound?.components,
471
+ ...props.components
374
472
  },
375
473
  scope: {
376
- ...t?.scope,
377
- ...e.scope
474
+ ...prebound?.scope,
475
+ ...props.scope
378
476
  }
379
- })), i = u(() => r.value.scope);
380
- return y(M, r), y(j, i), () => d(J, {
381
- schema: n.value,
382
- name: e.name,
383
- basePath: e.basePath,
384
- onlyRenderProperties: e.onlyRenderProperties,
385
- onlyRenderSelf: e.onlyRenderSelf,
386
- mapProperties: e.mapProperties,
387
- filterProperties: e.filterProperties
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 ce = /* @__PURE__ */ f({
491
+ var SchemaField = /* @__PURE__ */ defineComponent({
391
492
  name: "SchemaField",
392
- inheritAttrs: !1,
393
- props: Y,
394
- setup(e) {
395
- return $(e);
493
+ inheritAttrs: false,
494
+ props: schemaFieldProps,
495
+ setup(props) {
496
+ return renderSchemaField(props);
396
497
  }
397
498
  });
398
- function le(e = {}) {
399
- let n = e.voidComponents ?? {}, r = e.arrayComponents ?? {}, i = {
400
- ...e.components,
401
- ...n,
402
- ...r
403
- }, a = t({
404
- patches: e.patches,
405
- voidComponentNames: Object.keys(n),
406
- arrayComponentNames: Object.keys(r),
407
- defaultDecorator: e.defaultDecorator
408
- }), o = {
409
- components: i,
410
- scope: e.scope
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__ */ f({
526
+ SchemaField: /* @__PURE__ */ defineComponent({
414
527
  name: "SchemaField",
415
- inheritAttrs: !1,
416
- props: Y,
417
- setup(e) {
418
- return $(e, o);
528
+ inheritAttrs: false,
529
+ props: schemaFieldProps,
530
+ setup(props) {
531
+ return renderSchemaField(props, prebound);
419
532
  }
420
533
  }),
421
- fragment: a
534
+ fragment
422
535
  };
423
536
  }
424
537
  // src/hooks/useFieldSchema.ts
425
- function ue() {
426
- return m(A, b());
538
+ function useFieldSchema() {
539
+ return inject(SchemaSymbol, ref());
427
540
  }
428
541
  // src/hooks/useFormEffects.ts
429
- function de(e) {
430
- let t = N(), n = re((n) => {
431
- let r = t.value;
432
- if (!r) return;
433
- let i = c();
434
- r.addEffects(i, e), n(() => {
435
- r.removeEffects(i);
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
- v(() => n());
553
+ onBeforeUnmount(() => stop());
439
554
  }
440
555
  // src/hooks/useParentForm.ts
441
- function fe() {
442
- let e = L(), t = N();
443
- function n(e) {
444
- return e ? T(e) ? e : n(e.parent) : t.value;
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 u(() => n(e.value));
564
+ return computed(() => findObjectParent(field.value));
447
565
  }
448
566
  // src/shared/connect.tsx
449
- function pe(...e) {
450
- function t(t, s) {
451
- return e.reduce((e, t) => (i(t) ? e = Object.assign(e, t(e, s)) : r(t, (t, r) => {
452
- let i = n.getIn(s, r), c = a(t) ? t : r, l = n.getIn(e, c);
453
- r === "value" && t !== r && delete e.value, (!o(l) || o(i)) && n.setIn(e, c, i);
454
- }), e), t);
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(e) {
457
- return /* @__PURE__ */ f({
458
- name: e.name ? `Connected${e.name}` : "ConnectedComponent",
459
- inheritAttrs: !1,
460
- setup(n, { attrs: r, slots: i }) {
461
- let a = L();
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
- let n = a.value ? t({ ...r }, a.value) : { ...r };
464
- return p(e, n, i);
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 me(e, t) {
471
- return function(n) {
472
- return /* @__PURE__ */ f({
473
- name: n.name ? `Read${n.name}` : "ReadComponent",
474
- inheritAttrs: !1,
475
- setup(r, { attrs: i, slots: a }) {
476
- let o = L();
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
- let r = o.value, s = r && !E(r) && r.pattern === "readPretty" ? e : n;
479
- return p(s, {
480
- ...t,
481
- ...i
482
- }, a);
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 he(e, ...t) {
489
- let n = t.reduce((e, t) => t(e), e);
490
- return g(/* @__PURE__ */ f({
491
- name: e.name,
492
- inheritAttrs: !1,
493
- setup(e, { attrs: t, slots: r }) {
494
- return () => p(n, t, r);
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 ge(e) {
500
- return g(w(e));
628
+ function createForm(options) {
629
+ return markRaw(createForm$1(options));
501
630
  }
502
- export { ie as ExpressionScope, P as FormConsumer, ae as FormProvider, J as RecursionField, ce as SchemaField, he as connect, ge as createForm, le as createSchemaField, pe as mapProps, me as mapReadPretty, F as useAttach, L as useField, ue as useFieldSchema, N as useForm, de as useFormEffects, I as useInjectionCleaner, fe as useParentForm };
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