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

Commit

Permalink
👕 refactor(progress): change data prop to computed prop [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Dec 7, 2016
1 parent 0b152bc commit 1304256
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 67 deletions.
67 changes: 48 additions & 19 deletions src/components/validity/computed.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
/* @flow */

export default function (Vue: GlobalAPI): Object {
const { isPlainObject } = Vue.util
const { isPlainObject } = Vue.util

function invalid (): boolean {
return !this.valid
}

function pristine (): boolean {
return !this.dirty
}

function untouched (): boolean {
return !this.touched
}

function _setError (
function setError (
result: ValidationResult,
field: string,
validator: string,
Expand All @@ -33,6 +21,37 @@ export default function (Vue: GlobalAPI): Object {
result.errors.push(error)
}

function walkProgresses (keys: Array<string>, target: any): string {
let progress = ''
for (let i = 0; i < keys.length; i++) {
const result = target[keys[i]]
if (typeof result === 'string' && result) {
progress = result
break
}
if (isPlainObject(result)) {
const nestedKeys = Object.keys(result)
progress = walkProgresses(nestedKeys, result)
if (!progress) {
break
}
}
}
return progress
}

function invalid (): boolean {
return !this.valid
}

function pristine (): boolean {
return !this.dirty
}

function untouched (): boolean {
return !this.touched
}

function result (): ValidationResult {
const ret: ValidationResult = {
valid: this.valid,
Expand All @@ -51,11 +70,11 @@ export default function (Vue: GlobalAPI): Object {
if (result) {
ret[validator] = false
} else {
_setError(ret, this.field, validator)
setError(ret, this.field, validator)
ret[validator] = !result
}
} else if (typeof result === 'string') {
_setError(ret, this.field, validator, result)
setError(ret, this.field, validator, result)
ret[validator] = result
} else if (isPlainObject(result)) { // object
const props: Array<string> = Object.keys(result)
Expand All @@ -66,11 +85,11 @@ export default function (Vue: GlobalAPI): Object {
if (propRet) {
ret[prop][validator] = false
} else {
_setError(ret, this.field, validator, undefined, prop)
setError(ret, this.field, validator, undefined, prop)
ret[prop][validator] = !propRet
}
} else if (typeof propRet === 'string') {
_setError(ret, this.field, validator, propRet, prop)
setError(ret, this.field, validator, propRet, prop)
ret[prop][validator] = propRet
} else {
ret[prop][validator] = false
Expand All @@ -84,10 +103,20 @@ export default function (Vue: GlobalAPI): Object {
return ret
}

function progress (): string {
let ret = ''
ret = walkProgresses(
this._keysCached(this._uid.toString(), this.results),
this.progresses
)
return ret
}

return {
invalid,
pristine,
untouched,
result
result,
progress
}
}
6 changes: 1 addition & 5 deletions src/components/validity/lifecycles.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ export default function (Vue: GlobalAPI): Object {
// watch validation raw results
this._watchValidationRawResults()

// watch validation raw progress
this._watchValidationRawProgresses()

const validation = this.$options.propsData ? this.$options.propsData.validation : null
if (validation) {
const { instance, name } = validation
Expand All @@ -72,7 +69,6 @@ export default function (Vue: GlobalAPI): Object {
instance.unregister(this.field, this, { named: name, group })
}

this._unwatchValidationRawProgresses()
this._unwatchValidationRawResults()

this._elementable.unlistenInputableEvent()
Expand All @@ -88,7 +84,7 @@ export default function (Vue: GlobalAPI): Object {
} else {
// TODO: should be warn
}

toggleClasses(this.$el, this.classes.untouched, addClass)
toggleClasses(this.$el, this.classes.pristine, addClass)
}
Expand Down
1 change: 0 additions & 1 deletion src/components/validity/methods-event.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* @flow */
import { MODEL_NOTIFY_EVENT } from '../../util'

export default function (Vue: GlobalAPI): Object {

Expand Down
41 changes: 1 addition & 40 deletions src/components/validity/methods-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export default function (Vue: GlobalAPI): Object {
}

function reset (): void {
this._unwatchValidationRawProgresses()
this._unwatchValidationRawResults()
const keys: Array<string> = this._keysCached(this._uid.toString(), this.results)
_initStates(keys, this.results, undefined)
Expand All @@ -79,9 +78,7 @@ export default function (Vue: GlobalAPI): Object {
this.touched = false
this.modified = false
this._modified = false
this.progress = ''
this._watchValidationRawResults()
this._watchValidationRawProgresses()
}

function _walkValid (keys: Array<string>, target: any): boolean {
Expand Down Expand Up @@ -131,40 +128,6 @@ export default function (Vue: GlobalAPI): Object {
delete this._unwatchResults
}

function _walkProgresses (keys: Array<string>, target: any): string {
let progress = ''
for (let i = 0; i < keys.length; i++) {
const result = target[keys[i]]
if (typeof result === 'string' && result) {
progress = result
break
}
if (isPlainObject(result)) {
const nestedKeys = Object.keys(result)
progress = _walkProgresses(nestedKeys, result)
if (!progress) {
break
}
}
}
return progress
}

function _watchValidationRawProgresses (): void {
this._unwatchProgresses = this.$watch('progresses', (val: any) => {
this.progress = _walkProgresses(
this._keysCached(this._uid.toString(), this.results),
this.progresses
)
}, { deep: true })
}

function _unwatchValidationRawProgresses (): void {
this._unwatchProgresses()
this._unwatchProgresses = undefined
delete this._unwatchProgresses
}

return {
getValue,
checkModified,
Expand All @@ -176,8 +139,6 @@ export default function (Vue: GlobalAPI): Object {
reset,
_walkValid,
_watchValidationRawResults,
_unwatchValidationRawResults,
_watchValidationRawProgresses,
_unwatchValidationRawProgresses
_unwatchValidationRawResults
}
}
3 changes: 1 addition & 2 deletions src/components/validity/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ export default function (Vue: GlobalAPI): Object {
dirty: false,
touched: false,
modified: false,
progresses: getInitialProgresses(validators),
progress: ''
progresses: getInitialProgresses(validators)
}
}

Expand Down

0 comments on commit 1304256

Please sign in to comment.