This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .form-control { | |
| width: 50%; | |
| border: 1px solid #ccc; | |
| padding: 10px; | |
| } | |
| .control-error { | |
| border: 1px solid red; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| const TextInput = props => { | |
| let formControl = "form-control"; | |
| if (props.touched && !props.valid) { | |
| formControl = 'form-control control-error'; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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} | |
| /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component } from 'react'; | |
| import TextInput from './TextInput'; | |
| import validate from './validator'; | |
| class FormComponent extends Component { | |
| constructor() { | |
| super(); | |
| this.state = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| constructor() { | |
| super(); | |
| this.state = { | |
| formControls: { | |
| name: { | |
| value: ''. | |
| placeholder: 'What is your name', | |
| valid: false, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| changeHandler = event => { | |
| const name = event.target.name; | |
| const value = event.target.value; | |
| const updatedControls = { | |
| ...this.state.formControls | |
| }; | |
| const updatedFormElement = { | |
| ...updatedControls[name] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| changeHandler = event => { | |
| const name = event.target.name; | |
| const value = event.target.value; | |
| this.setState(prevState => { | |
| return { | |
| formControls: { | |
| ...prevState.formControls, |