Created
July 5, 2018 01:35
-
-
Save MostlyFocusedMike/3b222d34148c1535bf4594056a0e8719 to your computer and use it in GitHub Desktop.
This file contains 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" | |
import CatInputs from "./CatInputs" | |
class Form extends React.Component { | |
state = { | |
cats: [{name:"", age:""}], | |
owner: "", | |
description: "" | |
} | |
handleChange = (e) => { | |
if (["name", "age"].includes(e.target.className) ) { | |
let cats = [...this.state.cats] | |
cats[e.target.dataset.id][e.target.className] = e.target.value.toUpperCase() | |
this.setState({ cats }, () => console.log(this.state.cats)) | |
} else { | |
this.setState({ [e.target.name]: e.target.value.toUpperCase() }) | |
} | |
} | |
addCat = (e) => { | |
this.setState((prevState) => ({ | |
cats: [...prevState.cats, {name:"", age:""}], | |
})); | |
} | |
handleSubmit = (e) => { e.preventDefault() } | |
render() { | |
let {owner, description, cats} = this.state | |
return ( | |
<form onSubmit={this.handleSubmit} onChange={this.handleChange} > | |
<label htmlFor="name">Owner</label> | |
<input type="text" name="owner" id="owner" value={owner} /> | |
<label htmlFor="description">Description</label> | |
<input type="text" name="description" id="description" value={description} /> | |
<button onClick={this.addCat}>Add new cat</button> | |
<CatInputs cats={cats} /> | |
<input type="submit" value="Submit" /> | |
</form> | |
) | |
} | |
} | |
export default Form |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment