Skip to content
This repository has been archived by the owner on Dec 25, 2017. It is now read-only.

Commit

Permalink
⭐ new: support v-model validation result [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Dec 7, 2016
1 parent 1304256 commit bfe63e2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/components/validity/lifecycles.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ export default function (Vue: GlobalAPI): Object {
return targets
}

function watchModelable (val: any): void {
this.$emit('input', {
result: this.result,
progress: this.progress,
progresses: this.progresses
})
}

function created (): void {
this._elementable = null

Expand Down Expand Up @@ -69,6 +77,16 @@ export default function (Vue: GlobalAPI): Object {
instance.unregister(this.field, this, { named: name, group })
}

if (this._unwatchResultProp) {
this._unwatchResultProp()
this._unwatchResultProp = null
}

if (this._unwatchProgressProp) {
this._unwatchProgressProp()
this._unwatchProgressProp = null
}

this._unwatchValidationRawResults()

this._elementable.unlistenInputableEvent()
Expand All @@ -85,6 +103,11 @@ export default function (Vue: GlobalAPI): Object {
// TODO: should be warn
}

if (hasModelDirective(this.$vnode)) {
this._unwatchResultProp = this.$watch('result', watchModelable)
this._unwatchProgressProp = this.$watch('progress', watchModelable)
}

toggleClasses(this.$el, this.classes.untouched, addClass)
toggleClasses(this.$el, this.classes.pristine, addClass)
}
Expand Down Expand Up @@ -116,3 +139,8 @@ function checkBuiltInElement (vnode: VNode): any {
!vnode.componentOptions &&
vnode.tag
}

function hasModelDirective (vnode: VNode): boolean {
return ((vnode && vnode.data && vnode.data.directives) || []).find(dir => { return dir.name === 'model' }) ? true : false
}

3 changes: 3 additions & 0 deletions src/components/validity/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export default function (Vue: GlobalAPI): Object {
child: {
type: Object,
required: true
},
value: {
type: Object
}
}, baseProps)

Expand Down
60 changes: 60 additions & 0 deletions test/unit/components/validity-functional.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1003,4 +1003,64 @@ describe('validity functional component', () => {
})
})
})

describe('v-model', () => {
it('should be work', done => {
function createModelDirective (key, value, modifier) {
const modifiers = modifier ? { validity: true } : {}
return [{
expression: key,
modifiers,
name: 'model',
rawName: 'v-model',
value: value
}]
}
const vm = new Vue({
data: { validation: {}},
components,
validators: {
exist (val) {
return new Promise((resolve, reject) => {
setTimeout(() => {
val === 'dio' ? resolve() : reject()
}, 5)
})
}
},
render (h) {
const input = (function ($event) { this.validation = $event }).bind(this)
return h('div', [
h('validity', {
props: {
field: 'field1',
validators: ['required', 'numeric', 'exist']
},
directives: createModelDirective('validation', this.validation),
on: { input },
ref: 'validity',
}, [
h('input', { ref: 'textbox', attrs: { type: 'text' }})
])
])
}
}).$mount(el)
const { validity, textbox } = vm.$refs
waitForUpdate(() => {
validity.validate() // validate !!
}).thenWaitFor(1).then(() => {
assert.deepEqual(vm.validation, {
result: validity.result,
progress: validity.progress,
progresses: validity.progresses
})
}).thenWaitFor(6).then(() => {
assert.deepEqual(vm.validation, {
result: validity.result,
progress: validity.progress,
progresses: validity.progresses
})
}).then(done)
})
})
})

0 comments on commit bfe63e2

Please sign in to comment.