Skip to content

Instantly share code, notes, and snippets.

View agoiabel's full-sized avatar

Agoi Abel Adeyemi agoiabel

View GitHub Profile
<button onClick={this.formSubmitHandler}
disabled={!this.state.formControls.name.valid}
>
Submit
</button>
.form-control {
width: 50%;
border: 1px solid #ccc;
padding: 10px;
}
.control-error {
border: 1px solid red;
}
import React from 'react';
const TextInput = props => {
let formControl = "form-control";
if (props.touched && !props.valid) {
formControl = 'form-control control-error';
}
render() {
return (
<TextInput name="name"
placeholder={this.state.formControls.name.placeholder}
value={this.state.formControls.name.value}
onChange={this.changeHandler}
touched={this.state.formControls.name.touched}
valid={this.state.formControls.name.valid}
/>
import React, { Component } from 'react';
import TextInput from './TextInput';
import validate from './validator';
class FormComponent extends Component {
constructor() {
super();
this.state = {
const validate = (value, rules) => {
let isValid = true;
for (let rule in rules) {
switch (rule) {
case 'minLength': isValid = isValid && minLengthValidator(value, rules[rule]); break;
case 'isRequired': isValid = isValid && requiredValidator(value); break;
constructor() {
super();
this.state = {
formControls: {
name: {
value: ''.
placeholder: 'What is your name',
valid: false,
const validate = (value, rules) => {
let isValid = true;
for (let rule in rules) {
switch (rule) {
case 'minLength': isValid = isValid && minLengthValidator(value, rules[rule]); break;
default: isValid = true;
}
changeHandler = event => {
const name = event.target.name;
const value = event.target.value;
const updatedControls = {
...this.state.formControls
};
const updatedFormElement = {
...updatedControls[name]
changeHandler = event => {
const name = event.target.name;
const value = event.target.value;
this.setState(prevState => {
return {
formControls: {
...prevState.formControls,