Skip to content

Instantly share code, notes, and snippets.

@acomito
Created August 12, 2020 22:15
Show Gist options
  • Save acomito/ac7d148fb458fb354811ce6daf0ba19c to your computer and use it in GitHub Desktop.
Save acomito/ac7d148fb458fb354811ce6daf0ba19c to your computer and use it in GitHub Desktop.
const File = () => {
return (
<FormItem label="Year Built" error={this.state.yearError}>
<NumberInput
min={0}
onChange={(yearBuilt) => {
// *********** clear your error message here
this.setState({ yearError: null })
if (yearBuilt > currentYear || isNaN(yearBuilt)) { // checking for isNaN here may not make sense, since it's a number input it'll always be a number or null
return (this.setState({ yearError: 'Fix year' })); // probably could use a more descriptive error like year built can't be greater than this year
}
if (this.state.yearError==null){ // I typically use !this.state.yearError rather than this.state.yearError==null.. also you want to use === triple equals rather than double equals
yearBuilt = yearBuilt.toString() // if year is null, you can't call toString().... toString() is a method on a number... also you should call this once during on submit rather than every single onChange
}
return this.setState({ yearBuilt })}
}}
/>
</FormItem>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment