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

Commit

Permalink
⭐ new: manually touch with API [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Dec 9, 2016
1 parent 5a682cb commit 484d360
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/components/validity/lifecycles.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,18 @@ export default function (Vue: GlobalAPI): Object {
this._unwatchValidationRawResults()

this._elementable.unlistenInputableEvent()
this._elementable.unlistenToucheableEvent()
if (this.autotouch === 'on') {
this._elementable.unlistenToucheableEvent()
}
this._elementable = null
}

function mounted (): void {
this._elementable = createValidityElement(this, this._vnode)
if (this._elementable) {
this._elementable.listenToucheableEvent()
if (this.autotouch === 'on') {
this._elementable.listenToucheableEvent()
}
this._elementable.listenInputableEvent()
} else {
// TODO: should be warn
Expand Down
7 changes: 6 additions & 1 deletion src/components/validity/methods-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export default function (Vue: GlobalAPI): Object {
delete this._unwatchResults
}

function touch (): void {
this.willUpdateTouched()
}

return {
getValue,
checkModified,
Expand All @@ -139,6 +143,7 @@ export default function (Vue: GlobalAPI): Object {
reset,
_walkValid,
_watchValidationRawResults,
_unwatchValidationRawResults
_unwatchValidationRawResults,
touch
}
}
6 changes: 6 additions & 0 deletions src/components/validity/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export default {
multiple: {
type: Boolean
},
autotouch: {
type: String,
default: () => {
return 'on'
}
},
classes: {
type: Object,
default: () => {
Expand Down
37 changes: 37 additions & 0 deletions test/unit/components/validity-functional.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,4 +1063,41 @@ describe('validity functional component', () => {
}).then(done)
})
})

describe('manually touch', () => {
it('should be work', done => {
const vm = new Vue({
components,
render (h) {
return h('div', [
h('validity', {
ref: 'validity',
props: {
field: 'field1',
autotouch: 'off',
validators: { required: true }
}
}, [
h('input', { ref: 'textbox', attrs: { type: 'text' }})
])
])
}
}).$mount(el)
const { validity, textbox } = vm.$refs
let result
waitForUpdate(() => {
triggerEvent(textbox, 'focusout')
}).thenWaitFor(1).then(() => {
result = validity.result
assert(result.touched === false)
assert(result.untouched === true)
// manually touch with API
validity.touch()
}).thenWaitFor(1).then(() => {
result = validity.result
assert(result.touched === true)
assert(result.untouched === false)
}).then(done)
})
})
})

0 comments on commit 484d360

Please sign in to comment.