This repository has been archived by the owner on Jul 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
311 lines (286 loc) · 8.76 KB
/
index.js
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import {capitalize, setProp, getProp, getDefaultValue} from './src/util'
import CustomColorPicker from './src/customColorPicker.js'
export default {
props: {
schema: {
type: Object,
required: true,
default: () => ({
properties: {}
})
},
formProps: Object,
value: {
type: Object,
default: () => ({})
},
showHeading: {
type: Boolean,
default: true
}
},
components: {
CustomColorPicker
},
watch: {
schema: {
immediate: true,
handler: function () {
let value = this._isMounted ? {} : this.value
const props = this.schema.properties || {}
Object.keys(props).forEach(name => {
const schema = props[name]
const type = schema.type || 'string'
if (!(name in value)) {
let val = 'default' in schema ? schema.default : getDefaultValue(type)
if (type === 'number' || type === 'integer') {
if ('minimum' in schema) val = Math.max(val, schema.minimum)
if ('maximum' in schema) val = Math.min(val, schema.maximum)
}
this.$set(value, name, val)
}
})
if (this._isMounted) this.$refs.form.resetValidity()
this.$emit('change', value)
}
}
},
render(h) {
const {schema} = this
const children = []
// input fields
const fields = this.renderSchema(h, schema, '$')
children.push(...fields)
// submit button
const submit = h('c-form-item', {
props: {label: ' '}
}, [
h('c-button', {
props: {primary: true}
}, '提交'),
h('c-button', {
props: {
type: 'reset'
},
on: {
click: (e) => {
e.preventDefault()
this.$refs.form.reset()
}
}
}, '重置')
])
if (this.$slots.submitBtnBox) {
children.push(this.$slots.submitBtnBox)
} else
if (fields.length) {
if (this.$slots.submitBtnBox) {
children.push(this.$slots.submitBtnBox)
} else {
children.push(submit)
}
}
return h('c-form', {
props: this.formProps,
on: {
submit: this.onSubmit.bind(this)
},
ref: 'form'
}, children)
},
methods: {
/**
* render a a json schema to form items
* @param h {Function} Vue `createElement` function
* @param schema {Object} the json schema Object
* @param path {String} JSON path of the schema
*/
renderSchema(h, schema, path) {
const props = schema.properties || {}
const level = path.split('.').length
const heading = h('div', {class: 'c-form__heading'}, [
schema.title ? h('h' + level, schema.title) : null,
schema.description ? h('p', schema.description) : null
])
const fields = Object.keys(props).map(propName => {
return this.renderField(h, propName, props[propName], `${path}.${propName}`)
})
if (this.showHeading) fields.unshift(heading)
return fields
},
/**
* render a prop to form field
* @param h {Function} Vue `createElement` function
* @param propName {String} property name
* @param schema {Object} JSON schema of the field
*/
renderField(h, propName, schema, path) {
const {type, title, description} = schema
if (type === 'object') return this.renderSchema(h, schema, path)
if (type === 'array') return this.renderArray(h, schema, path)
const label = title || capitalize(propName)
const requiredFields = this.schema.required || []
const isRequired = requiredFields.includes(propName)
const tipsElement = description ? h('span', {
class: 'has-margin-left-md is-text-gray-6'
}, description) : undefined
return h('c-form-item', {
props: {
label: `${label}:`,
required: isRequired
}
}, [
this.renderPrimitive(h, schema, path),
tipsElement
])
},
/**
* render string/number/boolean
*/
renderPrimitive(h, schema, path) {
const {type, format} = schema
const enums = schema.enum // enum is reserved keyword
const options = schema.options
const {value} = this
const rules = {}
const propValue = getProp(value, path)
if (propValue === undefined) {
const defaultValue = 'default' in schema ? schema.default : getDefaultValue(type)
setProp(value, path, defaultValue, this)
}
// common data object for all type of fields
const dataObject = {
props: {
// value: getProp(value, path) || ('default' in schema ? schema.default : getDefaultValue(type)),
value: getProp(value, path),
rules
},
on: {
change: val => {
setProp(value, path, val, this)
this.$emit('change', value)
}
}
}
let tagName = 'c-input'
// radio group or select for enumable
if (Array.isArray(enums)) {
const options = enums.map(value => ({label: value, value}))
if (enums.length <= 5) {
tagName = 'c-radio-group'
Object.assign(dataObject.props, {options, button: true})
} else {
tagName = 'c-select'
Object.assign(dataObject.props, {options})
}
}
// select for options
if (Array.isArray(options)) {
tagName = 'c-select'
Object.assign(dataObject.props, {options})
}
// toggle TODO: replace with toggle component
if (type === 'boolean') {
Object.assign(dataObject.props, {
options: [
{value: 1, label: '是'},
{value: 0, label: '否'}
],
button: true
})
tagName = 'c-radio-group'
}
// date
if (format === 'date') {
tagName = 'c-datepicker'
}
// email
if (format === 'email') {
dataObject.props.rules.type = 'email'
}
// color
if (format === 'color') {
// dataObject.props.type = 'color'
tagName = 'c-color-picker'
// tagName = 'CustomColorPicker'
}
// pic upload
if (format === 'image') {
tagName = 'c-uploader'
}
// number
if (type === 'number' || type === 'integer') {
const useSlider = typeof schema.minimum === 'number'
&& typeof schema.maximum === 'number'
if (useSlider) {
tagName = 'c-slider'
dataObject.props.min = schema.minimum
dataObject.props.max = schema.maximum
dataObject.props.value = Math.max(Math.min(schema.maximum, dataObject.props.value), schema.minimum)
if ('multipleOf' in schema) dataObject.props.step = schema.multipleOf
} else {
dataObject.props.type = 'number'
}
dataObject.props.rules.type = 'number'
}
// min / max rules
if ('minLength' in schema) rules.minlength = schema.minLength
if ('maxLength' in schema) rules.maxlength = schema.maxLength
if ('minimum' in schema) rules.min = schema.minimum
if ('maximum' in schema) rules.max = schema.maximum
// pattern
if (typeof schema.pattern === 'string') {
rules.pattern = new RegExp(schema.pattern)
}
// default input
return h(tagName, dataObject)
},
renderArray(h, schema, path) {
const title = h('h' + path.split('.').length, schema.title)
const initialValue = getProp(this.value, path)
if (!Array.isArray(initialValue)) setProp(this.value, path, [], this)
const arrayValue = initialValue || []
const items = arrayValue.map((value, index) => {
const input = this.renderField(h, String(index + 1), schema.items, `${path}[${index}]`)
const arrayItemBody = h('div', {
staticClass: 'array-item__body'
}, [input])
const removeButton = h('c-button', {
props: {
danger: true,
icon: 'trash',
type: 'button'
},
staticClass: 'has-margin-left-sm',
on: {
click: e => getProp(this.value, path).splice(index, 1)
}
})
return h('div', {
staticClass: 'array-item'
}, [arrayItemBody, removeButton])
})
const addButton = h('c-button', {
props: {
primary: true,
icon: 'plus',
type: 'button'
},
staticClass: 'has-margin-left-sm',
on: {
click: e => {
getProp(this.value, path).push(getDefaultValue(schema.items.type))
}
}
})
const heading = h('div', {
staticClass: 'is-flex is-align-center'
}, [title, addButton])
return h('div', [heading, ...items])
},
onSubmit(e) {
e.preventDefault()
this.$refs.form.isValid()
}
}
}