-
-
Save viraj124/7a1ccc3658ecc21302008ad14c41351d to your computer and use it in GitHub Desktop.
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"; | |
import ReactDOM from "react-dom"; | |
import "./style.css"; | |
class IncorporationForm extends React.Component { | |
constructor() { | |
super(); | |
this.state = { | |
name: "", | |
shareholders: [{ name: "" }] | |
}; | |
} | |
handleNameChange = evt => { | |
this.setState({ name: evt.target.value }); | |
}; | |
handleShareholderNameChange = idx => evt => { | |
const newShareholders = this.state.shareholders.map((shareholder, sidx) => { | |
if (idx !== sidx) return shareholder; | |
return { ...shareholder, name: evt.target.value }; | |
}); | |
this.setState({ shareholders: newShareholders }); | |
}; | |
handleSubmit = evt => { | |
const { name, shareholders } = this.state; | |
alert(`Incorporated: ${name} with ${shareholders.length} shareholders`); | |
}; | |
handleAddShareholder = () => { | |
this.setState({ | |
shareholders: this.state.shareholders.concat([{ name: "" }]) | |
}); | |
}; | |
handleRemoveShareholder = idx => () => { | |
this.setState({ | |
shareholders: this.state.shareholders.filter((s, sidx) => idx !== sidx) | |
}); | |
}; | |
render() { | |
return ( | |
<form onSubmit={this.handleSubmit}> | |
<input | |
type="text" | |
placeholder="Company name, e.g. Magic Everywhere LLC" | |
value={this.state.name} | |
onChange={this.handleNameChange} | |
/> | |
<h4>Shareholders</h4> | |
{this.state.shareholders.map((shareholder, idx) => ( | |
<div className="shareholder"> | |
<select name="cars" id="cars"> | |
<option value="volvo">Volvo</option> | |
<option value="saab">Saab</option> | |
<option value="mercedes">Mercedes</option> | |
<option value="audi">Audi</option> | |
</select> | |
<button | |
type="button" | |
onClick={this.handleRemoveShareholder(idx)} | |
className="small" | |
> | |
- | |
</button> | |
</div> | |
))} | |
<button | |
type="button" | |
onClick={this.handleAddShareholder} | |
className="small" | |
> | |
Add Shareholder | |
</button> | |
<button>Incorporate</button> | |
</form> | |
); | |
} | |
} | |
const rootElement = document.getElementById("root"); | |
ReactDOM.render(<IncorporationForm />, rootElement); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment