index.vue
3.83 KB
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
<template>
<z-form ref="form" v-model="model" v-bind="schema.props" v-on="schema.props">
<template v-for="(item, index) in schema.items">
<template v-if="item.is">
<z-form-item v-bind="keywordFilter(item)" :key="index">
<slot
v-if="$scopedSlots[item.prop]"
:name="`${item.prop}`"
:value="get(model, item.prop)"
:onInput="value => onComponentInput({ value, item })"
:props="{ value: get(model, item.prop) }"
:listeners="{ input: value => onComponentInput({ value, item }) }"
>
</slot>
<component v-else :is="item.is" :value="get(model, item.prop)" @input="value => onComponentInput({ value, item })" v-bind="item.props">
<template v-if="item.render">
<dynamic-render
v-if="typeof item.render === 'function'"
:render="
item.render($createElement, {
model,
value: get(model, item.prop),
onInput: value => onComponentInput({ value, item }),
})
"
></dynamic-render>
<static-render v-else :render="item.render"></static-render>
</template>
<template v-if="item.children">
<childrens-render :value="item.children"></childrens-render>
</template>
</component>
<slot :name="`label-${item.prop}`" slot="label"></slot>
<slot :name="`error-${item.prop}`" slot="error"></slot>
</z-form-item>
</template>
<template v-else>
<z-form-item v-bind="keywordFilter(item)" :key="index">
<slot :name="item.prop"></slot>
</z-form-item>
</template>
</template>
<slot v-if="schema.footer !== false" name="footer">
<z-form-item>
<el-button type="primary" @click="onSubmit">确定</el-button>
<el-button plain @click="onCancel">取消</el-button>
</z-form-item>
</slot>
</z-form>
</template>
<script>
import { cloneDeep, get, set } from '../utils';
export default {
name: 'SchemaForm',
components: {
DynamicRender: {
functional: true,
render(h, context) {
return context.props.render;
},
},
StaticRender: {
functional: true,
render(h, context) {
return h('span', [context.props.render]);
},
},
ChildrensRender: {
functional: true,
render(h, context) {
return context.props.value.map(item => {
if (Array.isArray(item.children) && item.children.length > 0) {
return h('childrens-render', { props: { value: item.children } });
} else {
return h(item.is, { ...item });
}
});
},
},
},
props: {
value: {
type: Object,
default() {
return {};
},
},
schema: {
type: Object,
required: true,
},
},
data() {
return {
model: this.value,
originData: {},
};
},
computed: {
schemaProps() {
return this.schema.props;
},
},
created() {
const { originData, ...other } = this._data;
this.originData = cloneDeep(other);
},
watch: {
value(val = {}) {
this.model = val;
},
model: {
handler(val) {
this.$emit('input', val);
},
deep: true,
},
},
methods: {
get,
keywordFilter(val = {}) {
const { children, is, ...other } = val;
return other;
},
onComponentInput({ value, item }) {
set(this.model, item.prop, value);
},
onSubmit() {
this.$refs.form.validate(valid => {
if (valid) {
this.$emit('submit', this.model);
}
});
},
onCancel() {
this.model = cloneDeep(this.originData).model;
this.$emit('cancel');
},
},
};
</script>