I'm writing a React application for a small business selling furniture on the measure (among other services) as a side project. Its main feature is a page with a dynamic form component (i.e. by clicking on buttons a new set of inputs will appear, representing one of the products sold. Here is what I have so far for the Quote parent component:
const Quote = () => {
const [formFields, setFormFields] = useState ([])
const onClick = (event) => {
let newProduct;
switch (event.target.name) {
case "arm_std":
newProduct = <StandardWardrobe/>
break;
case "curtain":
newProduct = <Curtain />
break;
// more products...
}
setFormFields([...formFields, newProduct])
}
return (
<div className='quote'>
<Sidebar />
<div className='formContainer'>
<form onSubmit={onSubmit}>
<Adress_Form/>
<div id = 'products' key ={unique_key()}>
{formFields.map(item => (
<div key ={unique_key()}>{item}</div>
))}
</div>
<input type="submit"></input>
</form>
<button
onClick = {onClick}
className='button'
name="arm_std">Armadio Standard</button>
<button
onClick = {onClick}
className='button'
name="curtain">Tenda</button>
<button
onClick = {onClick}
className='button'>Armadio Mansardato</button>
<button
onClick = {onClick}
className='button'>Armadio Scorrevole</button>
</div>
</div>
)
}
Here is the code for one of the products:
const StandardWardrobe = () => {
return (
<div className="add-form" id="arm_std">
<div className = "product-header" >
<label>Armadio standard giessegi</label>
</div>
<div className="form-control">
<label>Posizione Armadio</label>
<input type = 'text' placeholder = "Posizione Armadio" id="arm_std_position" />
</div>
<div className="form-control">
<label>Modello Armadio</label>
<input type = 'text' placeholder = "Modello" id ="arm_std_model"/>
</div>
<div className="form-control">
<label>Finitura Fianchi/Ante</label>
<input type = 'text' placeholder = "Finitura" id ="arm_std_finish"/>
</div>
<div className="form-control">
<label>Larghezza in millimetri</label>
<input type = 'number' placeholder = "L. in mm" id ="arm_std_width"/>
</div>
<div className="form-control">
<label>Altezza minima in millimetri</label>
<input type = 'number' placeholder = "H. min in mm" id ="arm_std_height"/>
</div>
<div className="form-control">
<label>Accessori</label>
<textarea className="form-control textarea"placeholder="Accessori" id="arm_std_accessories" />
</div>
<div className="form-control">
<label>Note</label>
<textarea className="form-control textarea"placeholder="Note" id ="arm_std_notes" />
</div>
<div className="form-control">
<label>Prezzo</label>
<input type = 'number' placeholder = "Prezzo" id="price" />
</div>
</div>
)
}
What I'm having trouble with is how do I construct an onChangeHandler capable of storing the inputs of all these different products?
Of course, I'm open to changing the current structure, since this is my first real project using the MERN stack.