Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
Hold error message in state
Browse files Browse the repository at this point in the history
  • Loading branch information
Liooo committed Jun 18, 2017
1 parent 1984d52 commit 63814ff
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export class Component extends React.Component {
this.typeInfo = getTypeInfo(props.type)
this.state = {
isPristine: true,
hasError: false,
hasError: props.options ? !!props.options.hasError : false,
error: this.getError(),
value: this.getTransformer().format(props.value)
}
}
Expand All @@ -124,6 +125,7 @@ export class Component extends React.Component {
const should = (
nextState.value !== state.value ||
nextState.hasError !== state.hasError ||
nextState.error !== state.error ||
nextProps.options !== props.options ||
nextProps.type !== props.type ||
isArraysShallowDiffers(nextPath, curPath)
Expand Down Expand Up @@ -155,20 +157,21 @@ export class Component extends React.Component {
}

getValue() {
return this.getTransformer().parse(this.state.value)
const value = this.state ? this.state.value : this.getTransformer().format(this.props.value)
return this.getTransformer().parse(value)
}

isValueNully() {
return Nil.is(this.getValue())
}

removeErrors() {
this.setState({ hasError: false })
this.setState({ hasError: false, error: '' })
}

validate() {
const result = t.validate(this.getValue(), this.props.type, this.getValidationOptions())
this.setState({ hasError: !result.isValid() })
this.setState({ hasError: !result.isValid(), error: this.getError() })
return result
}

Expand Down Expand Up @@ -208,7 +211,7 @@ export class Component extends React.Component {
}

hasError() {
return this.props.options.hasError || this.state.hasError
return this.props.options.hasError || (this.state && this.state.hasError)
}

getConfig() {
Expand Down Expand Up @@ -237,8 +240,8 @@ export class Component extends React.Component {
typeInfo: this.typeInfo,
path: this.props.ctx.path,
isPristine: this.state.isPristine,
error: this.getError(),
hasError: this.hasError(),
error: this.state.error,
hasError: this.state.hasError,
label: this.getLabel(),
onChange: this.onChange,
config: this.getConfig(),
Expand Down Expand Up @@ -438,6 +441,7 @@ export class Datetime extends Component {
return defaultDatetimeValue
},
parse: (value) => {
if (Nil.is(value)) { return defaultDatetimeValue }
const numbers = value.map(parseNumber)
if (numbers.every(t.Number.is)) {
return new Date(numbers[0], numbers[1], numbers[2])
Expand Down Expand Up @@ -474,7 +478,7 @@ export class Struct extends Component {
}

removeErrors() {
this.setState({ hasError: false })
this.setState({ hasError: false, error: '' })
Object.keys(this.refs).forEach((ref) => this.refs[ref].removeErrors())
}

Expand Down Expand Up @@ -507,7 +511,7 @@ export class Struct extends Component {
}
}

this.setState({ hasError: errors.length > 0 })
this.setState({ hasError: errors.length > 0, error: this.getError() })
return new t.ValidationResult({errors, value})
}

Expand Down Expand Up @@ -623,7 +627,7 @@ export class List extends Component {
}

removeErrors() {
this.setState({ hasError: false })
this.setState({ hasError: false, error: '' })
Object.keys(this.refs).forEach((ref) => this.refs[ref].removeErrors())
}

Expand All @@ -650,7 +654,7 @@ export class List extends Component {
errors = result.errors
}

this.setState({ hasError: errors.length > 0 })
this.setState({ hasError: errors.length > 0, error: this.getError() })
return new t.ValidationResult({errors: errors, value: value})
}

Expand Down

0 comments on commit 63814ff

Please sign in to comment.