This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
SchemaForm.vue
152 lines (129 loc) · 3.16 KB
/
SchemaForm.vue
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
<template>
<component
:is="!hasParentSchema ? 'form' : 'div'"
v-bind="formBinds"
>
<slot
v-if="!hasParentSchema"
name="beforeForm"
/>
<div
class="schema-row"
v-for="(fields, index) in parsedSchema"
:key="index"
>
<component
v-for="field in fields"
v-bind="binds(field)"
:key="field.model"
:is="field.component"
:modelValue="val(field)"
@update:modelValue="update(field.model, $event)"
@update-batch="updateBatch(field.model, $event)"
class="schema-col"
/>
</div>
<slot
v-if="!hasParentSchema"
name="afterForm"
/>
</component>
</template>
<script>
import useParsedSchema from './features/ParsedSchema'
import { computed, watch, provide, inject } from 'vue'
export default {
props: {
schema: {
type: [Object, Array],
required: true,
validator (schema) {
if (!Array.isArray(schema)) return true
return schema.filter(field => !Array.isArray(field) && (!field.model && !field.schema)).length === 0
}
},
modelValue: {
type: Object,
required: true
},
sharedConfig: {
type: Object,
default: () => ({})
},
preventModelCleanupOnSchemaChange: {
type: Boolean,
default: false
}
},
emits: ['submit', 'update:modelValue'],
setup (props, { emit }) {
const hasParentSchema = inject('parentSchemaExists', false)
if (!hasParentSchema) {
provide('parentSchemaExists', true)
}
const { parsedSchema } = useParsedSchema(props)
const cleanupModelChanges = (schema, oldSchema) => {
if (props.preventModelCleanupOnSchemaChange) return
const reducer = (acc, val) => {
return acc.concat(val.map(i => i.model))
}
const newKeys = schema.reduce(reducer, [])
const oldKeys = oldSchema.reduce(reducer, [])
const diff = oldKeys.filter(i => !newKeys.includes(i))
if (!diff.length) return
const val = { ...props.modelValue }
for (const key of diff) {
delete val[key]
}
emit('update:modelValue', val)
}
watch(parsedSchema, cleanupModelChanges)
const update = (property, value) => {
emit('update:modelValue', {
...props.modelValue,
[property]: value
})
}
const updateBatch = (property, values) => {
emit('update:modelValue', {
...props.modelValue,
...values
})
}
const binds = (field) => {
return field.schema
? { schema: field.schema }
: { ...props.sharedConfig, ...field }
}
const val = (field) => {
if (field.schema && !props.modelValue[field.model]) {
return {}
}
return props.modelValue[field.model]
}
const formBinds = computed(() => {
if (hasParentSchema) return {}
return {
onSubmit: event => {
event.preventDefault()
emit('submit', event)
}
}
})
return {
parsedSchema,
val,
binds,
update,
updateBatch,
hasParentSchema,
formBinds
}
}
}
</script>
<style>
.schema-row {
display: flex;
}
</style>