@formvk/vue 0.1.0 → 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.
- package/CHANGELOG.md +15 -0
- package/README.md +63 -0
- package/package.json +13 -9
- package/src/__tests__/expression.spec.tsx +0 -79
- package/src/__tests__/form.spec.tsx +0 -185
- package/src/__tests__/hooks.spec.tsx +0 -230
- package/src/__tests__/render.spec.tsx +0 -694
- package/src/__tests__/schema.spec.tsx +0 -587
- package/src/__tests__/shared.tsx +0 -52
- package/src/components/ExpressionScope.tsx +0 -17
- package/src/components/FormConsumer.tsx +0 -17
- package/src/components/FormProvider.tsx +0 -24
- package/src/components/ReactiveField.tsx +0 -196
- package/src/components/RecursionField.tsx +0 -166
- package/src/components/SchemaField.tsx +0 -240
- package/src/components/index.ts +0 -5
- package/src/hooks/index.ts +0 -7
- package/src/hooks/useAttach.ts +0 -24
- package/src/hooks/useField.ts +0 -9
- package/src/hooks/useFieldSchema.ts +0 -9
- package/src/hooks/useForm.ts +0 -9
- package/src/hooks/useFormEffects.ts +0 -23
- package/src/hooks/useInjectionCleaner.ts +0 -6
- package/src/hooks/useParentForm.ts +0 -20
- package/src/index.ts +0 -5
- package/src/shared/connect.tsx +0 -75
- package/src/shared/context.ts +0 -15
- package/src/shared/createForm.ts +0 -7
- package/src/shared/index.ts +0 -2
- package/src/types/index.ts +0 -141
- package/src/utils/getRawComponent.ts +0 -11
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -8
- package/vite.config.ts +0 -23
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
import { isVoidField } from '@formvk/core'
|
|
2
|
-
import type { GeneralField, IFieldFactoryProps } from '@formvk/core'
|
|
3
|
-
import { toJS } from '@formvk/reactive'
|
|
4
|
-
import { each, isFn, isStr } from '@formvk/shared'
|
|
5
|
-
import { Fragment, defineComponent, h, inject, isVNode, provide, ref, shallowRef, watch } from 'vue'
|
|
6
|
-
import type { Ref, VNode } from 'vue'
|
|
7
|
-
|
|
8
|
-
import { useAttach } from '../hooks/useAttach'
|
|
9
|
-
import { useField } from '../hooks/useField'
|
|
10
|
-
import { useForm } from '../hooks/useForm'
|
|
11
|
-
import { FieldSymbol, SchemaOptionsSymbol } from '../shared/context'
|
|
12
|
-
import type { IReactiveFieldProps, ISchemaFieldRuntimeOptions } from '../types'
|
|
13
|
-
import { getRawComponent } from '../utils/getRawComponent'
|
|
14
|
-
|
|
15
|
-
type SlotFn = (props?: any) => any
|
|
16
|
-
|
|
17
|
-
function isVueComponentLike(value: any): boolean {
|
|
18
|
-
if (!value) return false
|
|
19
|
-
if (isFn(value)) return true
|
|
20
|
-
return typeof value === 'object' && (isFn(value.setup) || isFn(value.render) || isStr(value.template))
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function toArray(value: any): any[] {
|
|
24
|
-
if (Array.isArray(value)) return value
|
|
25
|
-
return value === undefined || value === null ? [] : [value]
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function wrapFragment(child: VNode | VNode[] | null): VNode | VNode[] | null {
|
|
29
|
-
if (Array.isArray(child)) {
|
|
30
|
-
if (child.length > 1) return h(Fragment, null, child)
|
|
31
|
-
return child[0] ?? null
|
|
32
|
-
}
|
|
33
|
-
return child
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function renderContent(content: any): any {
|
|
37
|
-
if (isVNode(content) || isStr(content)) return content
|
|
38
|
-
if (isVueComponentLike(content)) return h(content)
|
|
39
|
-
return content
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function mergeSlots(field: GeneralField, slots: Record<string, any>, content: any): Record<string, SlotFn> {
|
|
43
|
-
const result: Record<string, SlotFn> = {}
|
|
44
|
-
Object.keys(slots).forEach(name => {
|
|
45
|
-
result[name] = (slotProps?: any) => slots[name]?.({ field, form: field.form, ...slotProps }) ?? []
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
if (!content) return result
|
|
49
|
-
|
|
50
|
-
function appendToSlot(name: string, value: any) {
|
|
51
|
-
const origin = result[name]
|
|
52
|
-
result[name] = (slotProps?: any) => [...toArray(origin?.(slotProps)), ...toArray(renderContent(value))]
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (isStr(content) || isVNode(content) || isVueComponentLike(content)) {
|
|
56
|
-
appendToSlot('default', content)
|
|
57
|
-
} else if (typeof content === 'object') {
|
|
58
|
-
// 普通对象视为具名插槽内容映射
|
|
59
|
-
each(content, (value, key) => {
|
|
60
|
-
appendToSlot(key, value)
|
|
61
|
-
})
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return result
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function normalizeProps(origin: Record<string, any>) {
|
|
68
|
-
const props: Record<string, any> = {}
|
|
69
|
-
const events: Record<string, any> = {}
|
|
70
|
-
each(origin, (value, key) => {
|
|
71
|
-
if (key.startsWith('on')) {
|
|
72
|
-
const eventName = `${key[2].toLowerCase()}${key.slice(3)}`
|
|
73
|
-
events[eventName] = events[eventName] ?? value
|
|
74
|
-
} else if (key.startsWith('@')) {
|
|
75
|
-
events[key.slice(1)] = value
|
|
76
|
-
} else {
|
|
77
|
-
props[key] = value
|
|
78
|
-
}
|
|
79
|
-
})
|
|
80
|
-
return { props, events }
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function toVueEventProps(events: Record<string, any>) {
|
|
84
|
-
const props: Record<string, any> = {}
|
|
85
|
-
each(events, (handler, name) => {
|
|
86
|
-
props[`on${name.charAt(0).toUpperCase()}${name.slice(1)}`] = handler
|
|
87
|
-
})
|
|
88
|
-
return props
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export const ReactiveField = defineComponent({
|
|
92
|
-
name: 'ReactiveField',
|
|
93
|
-
inheritAttrs: false,
|
|
94
|
-
props: {
|
|
95
|
-
fieldType: {
|
|
96
|
-
type: String,
|
|
97
|
-
default: 'Field',
|
|
98
|
-
},
|
|
99
|
-
fieldProps: {
|
|
100
|
-
type: Object,
|
|
101
|
-
default: () => ({}),
|
|
102
|
-
},
|
|
103
|
-
},
|
|
104
|
-
setup(props, { slots }) {
|
|
105
|
-
const formRef = useForm()
|
|
106
|
-
const parentRef = useField()
|
|
107
|
-
const optionsRef = inject(SchemaOptionsSymbol, ref({})) as Ref<ISchemaFieldRuntimeOptions>
|
|
108
|
-
|
|
109
|
-
function createField(): GeneralField {
|
|
110
|
-
const fieldType = (props as IReactiveFieldProps).fieldType ?? 'Field'
|
|
111
|
-
const fieldProps = ((props as IReactiveFieldProps).fieldProps ?? {}) as IFieldFactoryProps<any, any>
|
|
112
|
-
return (formRef.value as any)?.[`create${fieldType}`]?.({
|
|
113
|
-
...fieldProps,
|
|
114
|
-
basePath: fieldProps.basePath ?? parentRef.value?.address,
|
|
115
|
-
})
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const fieldRef = shallowRef(createField()) as Ref<GeneralField>
|
|
119
|
-
watch(
|
|
120
|
-
() => props.fieldProps,
|
|
121
|
-
() => {
|
|
122
|
-
fieldRef.value = createField()
|
|
123
|
-
}
|
|
124
|
-
)
|
|
125
|
-
useAttach(fieldRef)
|
|
126
|
-
provide(FieldSymbol, fieldRef)
|
|
127
|
-
|
|
128
|
-
return () => {
|
|
129
|
-
const field = fieldRef.value
|
|
130
|
-
const options = optionsRef.value
|
|
131
|
-
if (!field) return slots.default?.()
|
|
132
|
-
if (field.display !== 'visible') return null
|
|
133
|
-
|
|
134
|
-
const mergedSlots = mergeSlots(field, slots, field.content)
|
|
135
|
-
|
|
136
|
-
function renderDecorator(children: VNode | VNode[] | null) {
|
|
137
|
-
if (!field.decoratorType) return wrapFragment(children)
|
|
138
|
-
const Decorator = getRawComponent(options?.components, field.decoratorType)
|
|
139
|
-
const { props: decoratorProps, events } = normalizeProps(toJS((field.decorator as any)[1]) || {})
|
|
140
|
-
return h(
|
|
141
|
-
Decorator,
|
|
142
|
-
{ ...decoratorProps, ...toVueEventProps(events) },
|
|
143
|
-
{
|
|
144
|
-
default: () => children,
|
|
145
|
-
}
|
|
146
|
-
)
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function renderComponent() {
|
|
150
|
-
if (!field.componentType) return mergedSlots.default?.() ?? null
|
|
151
|
-
|
|
152
|
-
const Component = getRawComponent(options?.components, field.componentType)
|
|
153
|
-
const { props: originProps, events } = normalizeProps(toJS((field.component as any)[1]) || {})
|
|
154
|
-
const originChange = events.change
|
|
155
|
-
const originFocus = events.focus
|
|
156
|
-
const originBlur = events.blur
|
|
157
|
-
|
|
158
|
-
events.change = (...args: any[]) => {
|
|
159
|
-
if (!isVoidField(field)) field.onInput(...args)
|
|
160
|
-
originChange?.(...args)
|
|
161
|
-
}
|
|
162
|
-
events.focus = (...args: any[]) => {
|
|
163
|
-
if (!isVoidField(field)) field.onFocus(...args)
|
|
164
|
-
originFocus?.(...args)
|
|
165
|
-
}
|
|
166
|
-
events.blur = (...args: any[]) => {
|
|
167
|
-
if (!isVoidField(field)) field.onBlur(...args)
|
|
168
|
-
originBlur?.(...args)
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const componentProps: Record<string, any> = {
|
|
172
|
-
...(isVoidField(field)
|
|
173
|
-
? {}
|
|
174
|
-
: {
|
|
175
|
-
disabled: field.pattern === 'disabled' || field.pattern === 'readPretty',
|
|
176
|
-
readOnly: field.pattern === 'readOnly',
|
|
177
|
-
}),
|
|
178
|
-
...originProps,
|
|
179
|
-
...(isVoidField(field) ? {} : { value: field.value }),
|
|
180
|
-
...toVueEventProps(events),
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
if (isStr(Component)) {
|
|
184
|
-
return h(
|
|
185
|
-
Component,
|
|
186
|
-
componentProps,
|
|
187
|
-
Object.keys(mergedSlots).map(name => mergedSlots[name]())
|
|
188
|
-
)
|
|
189
|
-
}
|
|
190
|
-
return h(Component, componentProps, mergedSlots)
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
return renderDecorator(renderComponent())
|
|
194
|
-
}
|
|
195
|
-
},
|
|
196
|
-
})
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import type { GeneralField } from '@formvk/core'
|
|
2
|
-
import { Schema } from '@formvk/schema'
|
|
3
|
-
import { isFn, isValid, lazyMerge } from '@formvk/shared'
|
|
4
|
-
import { computed, defineComponent, h, inject, markRaw, provide, ref, shallowRef, watch } from 'vue'
|
|
5
|
-
import type { Ref } from 'vue'
|
|
6
|
-
|
|
7
|
-
import { useField } from '../hooks/useField'
|
|
8
|
-
import { SchemaExpressionScopeSymbol, SchemaOptionsSymbol, SchemaSymbol } from '../shared/context'
|
|
9
|
-
import type { ISchemaFieldRuntimeOptions, ISchemaFilter, ISchemaMapper } from '../types'
|
|
10
|
-
import { ReactiveField } from './ReactiveField'
|
|
11
|
-
|
|
12
|
-
type SlotFn = (props?: any) => any
|
|
13
|
-
|
|
14
|
-
function resolveEmptySlot(slots: Record<string, SlotFn>) {
|
|
15
|
-
const names = Object.keys(slots)
|
|
16
|
-
if (!names.length) return null
|
|
17
|
-
const children = names.reduce<any[]>((result, name) => {
|
|
18
|
-
const value = slots[name]()
|
|
19
|
-
return result.concat(Array.isArray(value) ? value : [value])
|
|
20
|
-
}, [])
|
|
21
|
-
return h('div', { style: { display: 'contents' } }, children)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const RecursionField = defineComponent({
|
|
25
|
-
name: 'RecursionField',
|
|
26
|
-
inheritAttrs: false,
|
|
27
|
-
props: {
|
|
28
|
-
schema: {
|
|
29
|
-
required: true,
|
|
30
|
-
},
|
|
31
|
-
name: [String, Number],
|
|
32
|
-
basePath: {},
|
|
33
|
-
onlyRenderProperties: {
|
|
34
|
-
type: Boolean,
|
|
35
|
-
default: undefined,
|
|
36
|
-
},
|
|
37
|
-
onlyRenderSelf: {
|
|
38
|
-
type: Boolean,
|
|
39
|
-
default: undefined,
|
|
40
|
-
},
|
|
41
|
-
mapProperties: {},
|
|
42
|
-
filterProperties: {},
|
|
43
|
-
},
|
|
44
|
-
setup(props) {
|
|
45
|
-
const parentRef = useField()
|
|
46
|
-
const optionsRef = inject(SchemaOptionsSymbol, ref({})) as Ref<ISchemaFieldRuntimeOptions>
|
|
47
|
-
const scopeRef = inject(SchemaExpressionScopeSymbol, ref({}))
|
|
48
|
-
|
|
49
|
-
const fieldSchemaRef = computed(() => {
|
|
50
|
-
const schema = props.schema
|
|
51
|
-
return markRaw(Schema.isSchemaInstance(schema) ? schema : new Schema(schema as any))
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
function getPropsFromSchema(schema: Schema) {
|
|
55
|
-
const options = optionsRef.value
|
|
56
|
-
return schema.toFieldProps({
|
|
57
|
-
...options,
|
|
58
|
-
get scope() {
|
|
59
|
-
return lazyMerge(options.scope as any, scopeRef.value)
|
|
60
|
-
},
|
|
61
|
-
})
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const fieldPropsRef = shallowRef(getPropsFromSchema(fieldSchemaRef.value))
|
|
65
|
-
watch([fieldSchemaRef, optionsRef], () => {
|
|
66
|
-
fieldPropsRef.value = getPropsFromSchema(fieldSchemaRef.value)
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
function getBasePath() {
|
|
70
|
-
if (props.onlyRenderProperties) {
|
|
71
|
-
return (
|
|
72
|
-
props.basePath ??
|
|
73
|
-
(isValid(props.name) ? (parentRef.value?.address ?? '').concat(props.name) : parentRef.value?.address)
|
|
74
|
-
)
|
|
75
|
-
}
|
|
76
|
-
return props.basePath ?? parentRef.value?.address
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
provide(SchemaSymbol, fieldSchemaRef)
|
|
80
|
-
|
|
81
|
-
return () => {
|
|
82
|
-
const schema = fieldSchemaRef.value
|
|
83
|
-
if (!schema) return null
|
|
84
|
-
|
|
85
|
-
const basePath = getBasePath()
|
|
86
|
-
const fieldProps = fieldPropsRef.value
|
|
87
|
-
|
|
88
|
-
function generateSlotsByProperties(scoped = false) {
|
|
89
|
-
if (props.onlyRenderSelf) return {}
|
|
90
|
-
const properties = Schema.getOrderProperties(schema)
|
|
91
|
-
if (!properties.length) return {}
|
|
92
|
-
|
|
93
|
-
const renderMap: Record<string, ((field?: GeneralField) => any)[]> = {}
|
|
94
|
-
function setRender(key: string, value: (field?: GeneralField) => any) {
|
|
95
|
-
if (!renderMap[key]) renderMap[key] = []
|
|
96
|
-
renderMap[key].push(value)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const mapProperties = props.mapProperties as ISchemaMapper | undefined
|
|
100
|
-
const filterProperties = props.filterProperties as ISchemaFilter | undefined
|
|
101
|
-
|
|
102
|
-
properties.forEach(({ schema: item, key: name }, index) => {
|
|
103
|
-
let itemSchema = item
|
|
104
|
-
if (isFn(mapProperties)) {
|
|
105
|
-
const mapped = mapProperties(item, name)
|
|
106
|
-
if (mapped) itemSchema = mapped
|
|
107
|
-
}
|
|
108
|
-
if (isFn(filterProperties)) {
|
|
109
|
-
if (filterProperties(itemSchema, name) === false) return
|
|
110
|
-
}
|
|
111
|
-
setRender(itemSchema.slot ?? 'default', (field?: GeneralField) =>
|
|
112
|
-
h(RecursionField, {
|
|
113
|
-
key: `${index}-${name}`,
|
|
114
|
-
schema: itemSchema,
|
|
115
|
-
name,
|
|
116
|
-
basePath: field?.address ?? basePath,
|
|
117
|
-
})
|
|
118
|
-
)
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
const slots: Record<string, SlotFn> = {}
|
|
122
|
-
Object.keys(renderMap).forEach(key => {
|
|
123
|
-
const renderFns = renderMap[key]
|
|
124
|
-
slots[key] = scoped
|
|
125
|
-
? ({ field }: { field?: GeneralField } = {}) => renderFns.map(renderFn => renderFn(field))
|
|
126
|
-
: () => renderFns.map(renderFn => renderFn())
|
|
127
|
-
})
|
|
128
|
-
return slots
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (!isValid(props.name)) {
|
|
132
|
-
return resolveEmptySlot(generateSlotsByProperties())
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const commonFieldProps = {
|
|
136
|
-
...fieldProps,
|
|
137
|
-
name: props.name,
|
|
138
|
-
basePath,
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (schema.type === 'object') {
|
|
142
|
-
if (props.onlyRenderProperties) return resolveEmptySlot(generateSlotsByProperties())
|
|
143
|
-
return h(
|
|
144
|
-
ReactiveField,
|
|
145
|
-
{ fieldType: 'ObjectField', fieldProps: commonFieldProps },
|
|
146
|
-
generateSlotsByProperties(true)
|
|
147
|
-
)
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (schema.type === 'array') {
|
|
151
|
-
return h(ReactiveField, { fieldType: 'ArrayField', fieldProps: commonFieldProps })
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (schema.type === 'void') {
|
|
155
|
-
if (props.onlyRenderProperties) return resolveEmptySlot(generateSlotsByProperties())
|
|
156
|
-
return h(
|
|
157
|
-
ReactiveField,
|
|
158
|
-
{ fieldType: 'VoidField', fieldProps: commonFieldProps },
|
|
159
|
-
generateSlotsByProperties(true)
|
|
160
|
-
)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
return h(ReactiveField, { fieldType: 'Field', fieldProps: commonFieldProps })
|
|
164
|
-
}
|
|
165
|
-
},
|
|
166
|
-
})
|
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
import type { Form } from '@formvk/core'
|
|
2
|
-
import { createFragment, Schema } from '@formvk/schema'
|
|
3
|
-
import type { AnyComponent, ISchema, SchemaKey } from '@formvk/schema'
|
|
4
|
-
import { FormPath, isValid } from '@formvk/shared'
|
|
5
|
-
import type { FormPathPattern } from '@formvk/shared'
|
|
6
|
-
import { computed, defineComponent, provide, watch } from 'vue'
|
|
7
|
-
import type { PropType } from 'vue'
|
|
8
|
-
|
|
9
|
-
import { useField } from '../hooks/useField'
|
|
10
|
-
import { useForm } from '../hooks/useForm'
|
|
11
|
-
import { SchemaExpressionScopeSymbol, SchemaOptionsSymbol } from '../shared/context'
|
|
12
|
-
import type {
|
|
13
|
-
InferArrayProps,
|
|
14
|
-
InferFieldProps,
|
|
15
|
-
InferVoidProps,
|
|
16
|
-
ISchemaFieldFactoryOptions,
|
|
17
|
-
ISchemaFieldProps,
|
|
18
|
-
ISchemaFieldRuntimeOptions,
|
|
19
|
-
ISchemaFilter,
|
|
20
|
-
ISchemaMapper,
|
|
21
|
-
SchemaFieldFactoryResult,
|
|
22
|
-
SchemaVueComponents,
|
|
23
|
-
} from '../types'
|
|
24
|
-
import { RecursionField } from './RecursionField'
|
|
25
|
-
|
|
26
|
-
const schemaFieldProps = {
|
|
27
|
-
schema: {
|
|
28
|
-
type: Object as PropType<ISchema | Schema>,
|
|
29
|
-
required: true as const,
|
|
30
|
-
},
|
|
31
|
-
components: {
|
|
32
|
-
type: Object as PropType<SchemaVueComponents>,
|
|
33
|
-
},
|
|
34
|
-
scope: {
|
|
35
|
-
type: Object as PropType<Record<string, any>>,
|
|
36
|
-
},
|
|
37
|
-
name: [String, Number] as PropType<SchemaKey>,
|
|
38
|
-
basePath: [String, Number, Array, Object, Function] as PropType<FormPathPattern>,
|
|
39
|
-
hotReload: {
|
|
40
|
-
type: Boolean,
|
|
41
|
-
default: undefined,
|
|
42
|
-
},
|
|
43
|
-
onlyRenderProperties: {
|
|
44
|
-
type: Boolean,
|
|
45
|
-
default: undefined,
|
|
46
|
-
},
|
|
47
|
-
onlyRenderSelf: {
|
|
48
|
-
type: Boolean,
|
|
49
|
-
default: undefined,
|
|
50
|
-
},
|
|
51
|
-
mapProperties: {
|
|
52
|
-
type: Function as PropType<ISchemaMapper>,
|
|
53
|
-
},
|
|
54
|
-
filterProperties: {
|
|
55
|
-
type: Function as PropType<ISchemaFilter>,
|
|
56
|
-
},
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function joinPath(prefix: string, segment: SchemaKey | '*') {
|
|
60
|
-
return prefix ? `${prefix}.${segment}` : String(segment)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
interface ISchemaPaths {
|
|
64
|
-
/** 字段的绝对路径,数组元素下标使用 * 通配 */
|
|
65
|
-
fields: string[]
|
|
66
|
-
/** 数组子树的清理模式,覆盖任意下标 */
|
|
67
|
-
arrays: string[]
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* 收集 schema 声明的字段路径,用于热更新时定位需要销毁的字段。
|
|
72
|
-
* 数组元素的下标是运行时产物,这里统一用 * 通配。
|
|
73
|
-
*/
|
|
74
|
-
function collectSchemaPaths(schema: Schema, basePath: string, name?: SchemaKey): ISchemaPaths {
|
|
75
|
-
const fields: string[] = []
|
|
76
|
-
const arrays: string[] = []
|
|
77
|
-
|
|
78
|
-
const walkNode = (node: Schema, path: string) => {
|
|
79
|
-
if (node.type === 'array') arrays.push(joinPath(path, '*'))
|
|
80
|
-
walkChildren(node, path)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const walkChildren = (node: Schema, prefix: string) => {
|
|
84
|
-
const properties = node.properties ?? {}
|
|
85
|
-
Object.keys(properties).forEach(key => {
|
|
86
|
-
const path = joinPath(prefix, key)
|
|
87
|
-
fields.push(path)
|
|
88
|
-
walkNode(properties[key], path)
|
|
89
|
-
})
|
|
90
|
-
const items = node.items
|
|
91
|
-
if (items) {
|
|
92
|
-
const itemSchemas = Array.isArray(items) ? items : [items]
|
|
93
|
-
itemSchemas.forEach(item => {
|
|
94
|
-
if (item) walkChildren(item, joinPath(prefix, '*'))
|
|
95
|
-
})
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (isValid(name)) {
|
|
100
|
-
const rootPath = joinPath(basePath, String(name))
|
|
101
|
-
fields.push(rootPath)
|
|
102
|
-
walkNode(schema, rootPath)
|
|
103
|
-
} else {
|
|
104
|
-
walkChildren(schema, basePath)
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return { fields, arrays }
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function toSchema(schema: ISchema | Schema): Schema {
|
|
111
|
-
return Schema.isSchemaInstance(schema) ? schema : new Schema({ type: 'object', ...schema })
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* 热更新:销毁旧 schema 声明的字段(保留值),字段会在 fieldProps 更新后重建。
|
|
116
|
-
*/
|
|
117
|
-
function applyHotReload(form: Form, prev: Schema, next: Schema, basePath: string, name?: SchemaKey) {
|
|
118
|
-
const prevPaths = collectSchemaPaths(prev, basePath, name)
|
|
119
|
-
const nextPaths = collectSchemaPaths(next, basePath, name)
|
|
120
|
-
const nextFields = new Set(nextPaths.fields)
|
|
121
|
-
|
|
122
|
-
prevPaths.fields.forEach(path => {
|
|
123
|
-
if (nextFields.has(path)) return
|
|
124
|
-
form.deleteValuesIn(path)
|
|
125
|
-
form.deleteInitialValuesIn(path)
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
const patterns = new Set<string>([...prevPaths.fields, ...nextPaths.fields, ...prevPaths.arrays, ...nextPaths.arrays])
|
|
129
|
-
patterns.forEach(pattern => {
|
|
130
|
-
form.clearFormGraph(pattern, false)
|
|
131
|
-
})
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function setupHotReload(props: ISchemaFieldProps) {
|
|
135
|
-
if (!props.hotReload) return
|
|
136
|
-
|
|
137
|
-
const formRef = useForm()
|
|
138
|
-
const parentRef = useField()
|
|
139
|
-
|
|
140
|
-
watch(
|
|
141
|
-
() => props.schema,
|
|
142
|
-
(next, prev) => {
|
|
143
|
-
const form = formRef.value
|
|
144
|
-
if (!form || !prev || !next) return
|
|
145
|
-
const basePath = props.basePath
|
|
146
|
-
? FormPath.parse(props.basePath).toString()
|
|
147
|
-
: (parentRef.value?.address?.toString() ?? '')
|
|
148
|
-
applyHotReload(form, toSchema(prev), toSchema(next), basePath, props.name)
|
|
149
|
-
},
|
|
150
|
-
{ flush: 'pre' }
|
|
151
|
-
)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function renderSchemaField(props: ISchemaFieldProps, prebound?: ISchemaFieldRuntimeOptions) {
|
|
155
|
-
const schemaRef = computed(() => toSchema(props.schema))
|
|
156
|
-
|
|
157
|
-
setupHotReload(props)
|
|
158
|
-
|
|
159
|
-
const optionsRef = computed(() => ({
|
|
160
|
-
components: { ...prebound?.components, ...props.components },
|
|
161
|
-
scope: { ...prebound?.scope, ...props.scope },
|
|
162
|
-
}))
|
|
163
|
-
|
|
164
|
-
const scopeRef = computed(() => optionsRef.value.scope)
|
|
165
|
-
|
|
166
|
-
provide(SchemaOptionsSymbol, optionsRef)
|
|
167
|
-
provide(SchemaExpressionScopeSymbol, scopeRef)
|
|
168
|
-
|
|
169
|
-
return () => (
|
|
170
|
-
<RecursionField
|
|
171
|
-
schema={schemaRef.value}
|
|
172
|
-
name={props.name}
|
|
173
|
-
basePath={props.basePath}
|
|
174
|
-
onlyRenderProperties={props.onlyRenderProperties}
|
|
175
|
-
onlyRenderSelf={props.onlyRenderSelf}
|
|
176
|
-
mapProperties={props.mapProperties}
|
|
177
|
-
filterProperties={props.filterProperties}
|
|
178
|
-
/>
|
|
179
|
-
)
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export const SchemaField = defineComponent({
|
|
183
|
-
name: 'SchemaField',
|
|
184
|
-
inheritAttrs: false,
|
|
185
|
-
props: schemaFieldProps,
|
|
186
|
-
setup(props) {
|
|
187
|
-
return renderSchemaField(props)
|
|
188
|
-
},
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* 基于扁平组件映射与分类组件 map 创建 SchemaField + fragment:
|
|
193
|
-
* - voidComponents / arrayComponents 只接收组件 map,分类名由工厂内部提取后传给 fragment
|
|
194
|
-
* - 分类组件会自动合并进运行时组件映射,无需在 components 中重复注册
|
|
195
|
-
* - 返回的 SchemaField 已预绑定拍平后的组件与 scope,props 仍可传扁平 map 覆盖同名组件
|
|
196
|
-
* - fragment 包含 Field / Object / Array / Void / transform 与按组件名生成的动态构造器
|
|
197
|
-
* 组件 props、字段值与 scope 类型全部自动推断,无需手写类型参数。
|
|
198
|
-
*/
|
|
199
|
-
export function createSchemaField<
|
|
200
|
-
F extends SchemaVueComponents = SchemaVueComponents,
|
|
201
|
-
V extends SchemaVueComponents = {},
|
|
202
|
-
A extends SchemaVueComponents = {},
|
|
203
|
-
Scope extends object = Record<string, any>,
|
|
204
|
-
Decorator extends AnyComponent<InferVoidProps<V, A>> = AnyComponent<InferVoidProps<V, A>>,
|
|
205
|
-
>(
|
|
206
|
-
options: ISchemaFieldFactoryOptions<F, V, A, Scope, Decorator> = {}
|
|
207
|
-
): SchemaFieldFactoryResult<F, V, A, Scope, Decorator> {
|
|
208
|
-
const voidComponents = options.voidComponents ?? {}
|
|
209
|
-
const arrayComponents = options.arrayComponents ?? {}
|
|
210
|
-
const flatComponents: SchemaVueComponents = {
|
|
211
|
-
...options.components,
|
|
212
|
-
...voidComponents,
|
|
213
|
-
...arrayComponents,
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const fragment = createFragment<InferFieldProps<F, V, A>, InferVoidProps<V, A>, InferArrayProps<A>, Scope, Decorator>(
|
|
217
|
-
{
|
|
218
|
-
patches: options.patches,
|
|
219
|
-
voidComponentNames: Object.keys(voidComponents),
|
|
220
|
-
arrayComponentNames: Object.keys(arrayComponents),
|
|
221
|
-
defaultDecorator: options.defaultDecorator,
|
|
222
|
-
}
|
|
223
|
-
)
|
|
224
|
-
|
|
225
|
-
const prebound: ISchemaFieldRuntimeOptions = {
|
|
226
|
-
components: flatComponents,
|
|
227
|
-
scope: options.scope,
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
const BoundSchemaField = defineComponent({
|
|
231
|
-
name: 'SchemaField',
|
|
232
|
-
inheritAttrs: false,
|
|
233
|
-
props: schemaFieldProps,
|
|
234
|
-
setup(props) {
|
|
235
|
-
return renderSchemaField(props, prebound)
|
|
236
|
-
},
|
|
237
|
-
})
|
|
238
|
-
|
|
239
|
-
return { SchemaField: BoundSchemaField, fragment }
|
|
240
|
-
}
|
package/src/components/index.ts
DELETED
package/src/hooks/index.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { useAttach } from './useAttach'
|
|
2
|
-
export { useField } from './useField'
|
|
3
|
-
export { useFieldSchema } from './useFieldSchema'
|
|
4
|
-
export { useForm } from './useForm'
|
|
5
|
-
export { useFormEffects } from './useFormEffects'
|
|
6
|
-
export { useInjectionCleaner } from './useInjectionCleaner'
|
|
7
|
-
export { useParentForm } from './useParentForm'
|
package/src/hooks/useAttach.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { nextTick, onMounted, onUnmounted, watch } from 'vue'
|
|
2
|
-
import type { Ref } from 'vue'
|
|
3
|
-
|
|
4
|
-
interface IRecycleTarget {
|
|
5
|
-
onMount: () => void
|
|
6
|
-
onUnmount: () => void
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function useAttach<T extends IRecycleTarget>(target: Ref<T>): Ref<T> {
|
|
10
|
-
watch(target, (value, oldValue, onInvalidate) => {
|
|
11
|
-
if (value && value !== oldValue) {
|
|
12
|
-
oldValue?.onUnmount()
|
|
13
|
-
nextTick(() => value.onMount())
|
|
14
|
-
onInvalidate(() => value.onUnmount())
|
|
15
|
-
}
|
|
16
|
-
})
|
|
17
|
-
onMounted(() => {
|
|
18
|
-
target.value?.onMount()
|
|
19
|
-
})
|
|
20
|
-
onUnmounted(() => {
|
|
21
|
-
target.value?.onUnmount()
|
|
22
|
-
})
|
|
23
|
-
return target
|
|
24
|
-
}
|
package/src/hooks/useField.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { GeneralField } from '@formvk/core'
|
|
2
|
-
import { inject, ref } from 'vue'
|
|
3
|
-
import type { Ref } from 'vue'
|
|
4
|
-
|
|
5
|
-
import { FieldSymbol } from '../shared/context'
|
|
6
|
-
|
|
7
|
-
export function useField<T = GeneralField>(): Ref<T> {
|
|
8
|
-
return inject(FieldSymbol, ref()) as Ref<T>
|
|
9
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Schema } from '@formvk/schema'
|
|
2
|
-
import { inject, ref } from 'vue'
|
|
3
|
-
import type { Ref } from 'vue'
|
|
4
|
-
|
|
5
|
-
import { SchemaSymbol } from '../shared/context'
|
|
6
|
-
|
|
7
|
-
export function useFieldSchema(): Ref<Schema> {
|
|
8
|
-
return inject(SchemaSymbol, ref()) as Ref<Schema>
|
|
9
|
-
}
|
package/src/hooks/useForm.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { Form } from '@formvk/core'
|
|
2
|
-
import { uid } from '@formvk/shared'
|
|
3
|
-
import { onBeforeUnmount, watchEffect } from 'vue'
|
|
4
|
-
|
|
5
|
-
import { useForm } from './useForm'
|
|
6
|
-
|
|
7
|
-
export function useFormEffects(effects?: (form: Form) => void): void {
|
|
8
|
-
const formRef = useForm()
|
|
9
|
-
|
|
10
|
-
const stop = watchEffect(onCleanup => {
|
|
11
|
-
const form = formRef.value
|
|
12
|
-
if (!form) return
|
|
13
|
-
const id = uid()
|
|
14
|
-
form.addEffects(id, effects)
|
|
15
|
-
|
|
16
|
-
onCleanup(() => {
|
|
17
|
-
// 捕获注册时的 form,避免 form 替换后清理到了新实例上
|
|
18
|
-
form.removeEffects(id)
|
|
19
|
-
})
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
onBeforeUnmount(() => stop())
|
|
23
|
-
}
|