Skip to content

Instantly share code, notes, and snippets.

@agoiabel
Last active September 6, 2018 16:52
Show Gist options
  • Select an option

  • Save agoiabel/f4a415e49354343ef0539fa2739b1ca0 to your computer and use it in GitHub Desktop.

Select an option

Save agoiabel/f4a415e49354343ef0539fa2739b1ca0 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import TextInput from './TextInput';
import validate from './validator';
class FormComponent extends Component {
constructor() {
super();
this.state = {
formControls: {
name: {
value: ''.
placeholder: 'What is your name',
valid: false,
touched: false,
validationRules: {
minLength: 3,
isRequired: true
}
}
}
}
}
changeHandler = event => {
const name = event.target.name;
const value = event.target.value;
const updatedControls = {
...this.state.formControls
};
const updatedFormElement = {
...updatedControls[name]
};
updatedFormElement.value = value;
updatedFormElement.touched = true;
updatedFormElement.valid = validate(value, updatedFormElement.validationRules);
updatedControls[name] = updatedFormElement;
this.setState({
formControls: updatedControls
});
}
formSubmitHandler = () => {
console.dir(this.state.formControls);
}
render() {
return (
<div>
<TextInput name="name"
placeholder={this.state.formControls.name.placeholder}
value={this.state.formControls.name.value}
onChange={this.changeHandler}
/>
<button onClick={this.formSubmitHandler}> Submit </button>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment