Skip to content

Commit 4501642

Browse files
Merge pull request #16 from antonreshetov/13-form-builder
Form builder component
2 parents 33df308 + b509857 commit 4501642

File tree

16 files changed

+610
-237
lines changed

16 files changed

+610
-237
lines changed

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module.exports = {
1010
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
1111
'^.+\\.jsx?$': 'babel-jest'
1212
},
13+
transformIgnorePatterns: [
14+
'/node_modules/'
15+
],
1316
moduleNameMapper: {
1417
'^@/(.*)$': '<rootDir>/src/$1'
1518
},

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
"test:unit": "vue-cli-service test:unit"
3333
},
3434
"dependencies": {
35-
"async-validator": "^1.10.0",
3635
"popper.js": "^1.14.4",
36+
"vee-validate": "^2.2.0",
3737
"vue": "^2.6.0",
3838
"vue-router": "^3.0.1"
3939
},
@@ -45,10 +45,12 @@
4545
"@vue/eslint-config-standard": "^4.0.0",
4646
"@vue/test-utils": "^1.0.0-beta.20",
4747
"axios": "^0.18.0",
48+
"babel-core": "^7.0.0-bridge.0",
4849
"babel-eslint": "^10.0.1",
4950
"babel-jest": "^23.0.1",
5051
"eslint": "^5.16.0",
5152
"eslint-plugin-vue": "^5.2.2",
53+
"flush-promises": "^1.0.2",
5254
"highlight.js": "^9.12.0",
5355
"lint-staged": "^8.1.5",
5456
"marked": "^0.5.1",

src/components/checkbox/Checkbox.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@
3535
export default {
3636
name: 'VueCheckbox',
3737
38+
$_veeValidate: {
39+
name () {
40+
return this.name
41+
},
42+
value () {
43+
return this.checked
44+
}
45+
},
46+
3847
model: {
3948
prop: 'checked',
4049
event: 'change'
@@ -43,7 +52,7 @@ export default {
4352
props: {
4453
checked: Boolean,
4554
value: {
46-
type: [String, Number, Object, Boolean],
55+
type: [String, Number, Boolean],
4756
default: ''
4857
},
4958
name: {

src/components/checkbox/CheckboxGroup.vue

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<template>
2-
<div class="vue-checkbox-group">
2+
<div
3+
class="vue-checkbox-group"
4+
:name="name"
5+
>
36
<slot />
47
</div>
58
</template>
@@ -8,6 +11,15 @@
811
export default {
912
name: 'VueCheckboxGroup',
1013
14+
$_veeValidate: {
15+
name () {
16+
return this.name
17+
},
18+
value () {
19+
return this.modelValue
20+
}
21+
},
22+
1123
model: {
1224
prop: 'modelValue',
1325
event: 'change'
@@ -17,6 +29,10 @@ export default {
1729
modelValue: {
1830
type: Array,
1931
default: () => []
32+
},
33+
name: {
34+
type: String,
35+
default: ''
2036
}
2137
},
2238

src/components/form/Form.vue

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
</template>
1313

1414
<script>
15-
import AsyncValidator from 'async-validator'
1615
1716
export default {
1817
name: 'VueForm',
@@ -28,10 +27,6 @@ export default {
2827
type: Object,
2928
default: () => {}
3029
},
31-
rules: {
32-
type: Object,
33-
default: () => {}
34-
},
3530
labelPosition: {
3631
type: String,
3732
default: 'right'
@@ -40,68 +35,6 @@ export default {
4035
type: String,
4136
default: '100px'
4237
}
43-
},
44-
45-
data () {
46-
return {
47-
validator: this.rules ? new AsyncValidator(this.rules) : undefined
48-
}
49-
},
50-
51-
methods: {
52-
validate () {
53-
return new Promise((resolve, reject) => {
54-
console.warn(this.validator)
55-
this.validator.validate(this.model, (err, f) => {
56-
this.$children.forEach(child => {
57-
if (!this.isChildFormItem(child)) return
58-
59-
this.setChildOptions(child, '', true, true)
60-
})
61-
62-
if (!err) return resolve('Form is valid')
63-
64-
Object.keys(this.model).forEach(item => {
65-
err.forEach(err => {
66-
if (err.field === item) {
67-
this.$children.forEach(child => {
68-
if (!this.isChildFormItem(child)) return
69-
70-
if (child.field === item) {
71-
this.setChildOptions(child, err.message, false, true)
72-
}
73-
})
74-
}
75-
})
76-
return reject(new Error('Form is not valid'))
77-
})
78-
})
79-
})
80-
},
81-
resetValidation () {
82-
this.$children.forEach(child => {
83-
if (!this.isChildFormItem(child)) return
84-
85-
this.setChildOptions(child, '', true, false)
86-
})
87-
},
88-
resetFieldValidation (field) {
89-
this.$children.forEach(child => {
90-
if (!this.isChildFormItem(child)) return
91-
92-
if (child.field === field) {
93-
this.setChildOptions(child, '', true, false)
94-
}
95-
})
96-
},
97-
isChildFormItem (child) {
98-
return child.$options.name === 'VueFormItem'
99-
},
100-
setChildOptions (child, message, valid, state) {
101-
child.validateMessage = message
102-
child.isValid = valid
103-
child.validateState = state
104-
}
10538
}
10639
}
10740
</script>

src/components/form/FormItem.vue

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
<slot />
1414
<transition name="form-slide-fade">
1515
<div
16-
v-if="!isValid"
16+
v-if="showErrorMsg"
1717
class="vue-form__item-error"
1818
>
19-
{{ validateMessage }}
19+
{{ showErrorMsg }}
2020
</div>
2121
</transition>
2222
</div>
@@ -34,24 +34,24 @@ export default {
3434
type: String,
3535
default: ''
3636
},
37-
rules: {
38-
type: [Object, Array],
39-
default: () => {}
40-
},
4137
field: {
4238
type: String,
4339
default: ''
4440
}
4541
},
4642
47-
data () {
48-
return {
49-
isValid: true,
50-
validateMessage: '',
51-
validateState: false
43+
computed: {
44+
showErrorMsg () {
45+
let msg
46+
if (this.errors.items.length) {
47+
const item = this.errors.items.find(i => i.field === this.field)
48+
if (item) msg = item.msg
49+
}
50+
return msg
5251
}
5352
}
5453
}
54+
5555
</script>
5656

5757
<style lang="scss">

0 commit comments

Comments
 (0)