-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathRecursionField.ts
239 lines (219 loc) · 7.25 KB
/
RecursionField.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { inject, provide, watch, defineComponent, shallowRef } from 'vue-demi'
import { GeneralField } from '@formily/core'
import { isFn, isValid } from '@formily/shared'
import { Schema } from '@formily/json-schema'
import { observer } from '@formily/reactive-vue'
import {
SchemaSymbol,
SchemaOptionsSymbol,
SchemaExpressionScopeSymbol,
} from '../shared'
import { useField } from '../hooks'
import ObjectField from './ObjectField'
import ArrayField from './ArrayField'
import Field from './Field'
import VoidField from './VoidField'
import { h } from '../shared/h'
import { Fragment } from '../shared/fragment'
import type { IRecursionFieldProps, DefineComponent } from '../types'
function isVueOptions(options: any) {
if (!options) {
return false
}
return (
typeof options.template === 'string' ||
typeof options.render === 'function' ||
typeof options.setup === 'function'
)
}
const RecursionField = observer(
defineComponent<IRecursionFieldProps>({
name: 'RecursionField',
inheritAttrs: false,
props: {
schema: {
required: true,
},
name: [String, Number],
basePath: {},
onlyRenderProperties: {
type: Boolean,
default: undefined,
},
onlyRenderSelf: {
type: Boolean,
default: undefined,
},
mapProperties: {},
filterProperties: {},
},
setup(props: IRecursionFieldProps) {
const parentRef = useField()
const optionsRef = inject(SchemaOptionsSymbol)
const scopeRef = inject(SchemaExpressionScopeSymbol)
const createSchema = (schemaProp: IRecursionFieldProps['schema']) =>
new Schema(schemaProp)
const createFieldSchema = (schema: Schema) =>
schema.compile?.(scopeRef.value)
const schemaRef = shallowRef(createSchema(props.schema))
const fieldSchemaRef = shallowRef(createFieldSchema(schemaRef.value))
watch([() => props.schema, scopeRef, optionsRef], () => {
schemaRef.value = createSchema(props.schema)
fieldSchemaRef.value = createFieldSchema(schemaRef.value)
})
const getPropsFromSchema = (schema: Schema) =>
schema?.toFieldProps?.({ ...optionsRef.value, scope: scopeRef.value })
const fieldPropsRef = shallowRef(getPropsFromSchema(fieldSchemaRef.value))
watch([fieldSchemaRef, optionsRef], () => {
fieldPropsRef.value = getPropsFromSchema(fieldSchemaRef.value)
})
const getBasePath = () => {
if (props.onlyRenderProperties) {
return props.basePath || parentRef?.value?.address.concat(props.name)
}
return props.basePath || parentRef?.value?.address
}
provide(SchemaSymbol, fieldSchemaRef)
return () => {
const basePath = getBasePath()
const fieldProps = fieldPropsRef.value
const xContent = fieldSchemaRef.value['x-content']
const xContentMap: Record<string, any[]> = {}
if (typeof xContent === 'string') {
xContentMap['default'] = [xContent]
} else if (isVueOptions(xContent) || typeof xContent === 'function') {
// is vue component or functional component
xContentMap['default'] = [h(xContent, {}, {})]
} else if (xContent && typeof xContent === 'object') {
// for named slots
Object.keys(xContent).forEach((key) => {
const child = xContent[key]
if (typeof child === 'string') {
xContentMap[key] = [child]
} else if (isVueOptions(child) || typeof child === 'function') {
xContentMap[key] = [h(child, {}, {})]
}
})
}
const getSlots = (children = []) => {
const slots: Record<string, () => any> = {}
if (children.length > 0) {
slots.default = () => [...children]
}
Object.keys(xContentMap).forEach((key) => {
if (key === 'default') {
slots[key] = () => [...children, ...xContentMap[key]]
} else {
slots[key] = () => [...xContentMap[key]]
}
})
return slots
}
const renderProperties = (field?: GeneralField) => {
if (props.onlyRenderSelf) return
const children = fieldSchemaRef.value.mapProperties(
(item, name, index) => {
const base = field?.address || basePath
let schema: Schema = item
if (isFn(props.mapProperties)) {
const mapped = props.mapProperties(item, name)
if (mapped) {
schema = mapped
}
}
if (isFn(props.filterProperties)) {
if (props.filterProperties(schema, name) === false) {
return null
}
}
return h(
RecursionField,
{
key: index,
attrs: {
schema,
name,
basePath: base,
},
},
{}
)
}
)
const slots: Record<string, () => any> = {}
const allSlots = getSlots(children)
if (allSlots.default) {
slots.default = allSlots.default
}
return h(Fragment, {}, slots)
}
const render = () => {
if (!isValid(props.name)) return renderProperties()
if (fieldSchemaRef.value.type === 'object') {
if (props.onlyRenderProperties) return renderProperties()
const slots = getSlots()
return h(
ObjectField,
{
attrs: {
...fieldProps,
name: props.name,
basePath: basePath,
},
},
{
...slots,
default: ({ field }) => [renderProperties(field)],
}
)
} else if (fieldSchemaRef.value.type === 'array') {
const slots = getSlots()
return h(
ArrayField,
{
attrs: {
...fieldProps,
name: props.name,
basePath: basePath,
},
},
slots
)
} else if (fieldSchemaRef.value.type === 'void') {
if (props.onlyRenderProperties) return renderProperties()
const slots = getSlots()
return h(
VoidField,
{
attrs: {
...fieldProps,
name: props.name,
basePath: basePath,
},
},
{
...slots,
default: ({ field }) => [renderProperties(field)],
}
)
}
const slots = getSlots()
return h(
Field,
{
attrs: {
...fieldProps,
name: props.name,
basePath: basePath,
},
},
slots
)
}
if (!fieldSchemaRef.value) return
return render()
}
},
}) as unknown as DefineComponent<IRecursionFieldProps>
)
export default RecursionField