Here's my current code:
/* ************************************* */
/* ******** IMPORTS ******** */
/* ************************************* */
import React, { Component } from 'react';
import UUID from 'node-uuid';
import { Card, CardBlock, Button, InputGroup, Input } from 'reactstrap';
import ProviderInfos from '../ProviderInfos/ProviderInfos';
/* ************************************* */
/* ******** VARIABLES ******** */
/* ************************************* */
/* ************************************* */
/* ******** COMPONENT ******** */
/* ************************************* */
export default class PretzelStandComponent extends Component {
constructor(props) {
super(props);
this.state = {
inputPretzel: [],
inputCurry: [],
inputWurst: []
};
this.incrementPretzel = this.incrementPretzel.bind(this);
this.incrementCurry = this.incrementCurry.bind(this);
this.incrementWurst = this.incrementWurst.bind(this);
this.decrementPretzel = this.decrementPretzel.bind(this);
this.decrementCurry = this.decrementCurry.bind(this);
this.decrementWurst = this.decrementWurst.bind(this);
}
componentDidMount() {
this.incrementPretzel();
this.incrementCurry();
this.incrementWurst();
}
incrementPretzel() {
const uuid = require('uuid/v1');
uuid();
const inputPretzel = this.state.inputPretzel;
this.setState({
inputPretzel: inputPretzel.concat(<InputGroup>
<Input placeholder="Pretzel" key={uuid} /><ProviderInfos /></InputGroup>),
});
}
incrementCurry() {
const uuid = require('uuid/v1');
uuid();
const inputCurry = this.state.inputCurry;
this.setState({
inputCurry: inputCurry.concat(<InputGroup>
<Input placeholder="Curry" key={uuid} /><ProviderInfos /></InputGroup>),
});
}
incrementWurst() {
const uuid = require('uuid/v1');
uuid();
const inputWurst = this.state.inputWurst;
this.setState({
inputWurst: inputWurst.concat(<InputGroup>
<Input placeholder="Wurst" key={uuid} /><ProviderInfos /></InputGroup>),
});
}
decrementPretzel() {
this.setState({
inputPretzel: this.state.inputPretzel.splice(this.state.inputPretzel, this.state.inputPretzel.length - 1),
});
}
decrementCurry() {
this.setState({
inputCurry: this.state.inputCurry.splice(this.state.inputCurry, this.state.inputCurry.length - 1),
});
}
decrementWurst() {
this.setState({
inputWurst: this.state.inputWurst.splice(this.state.inputWurst, this.state.inputWurst.length - 1),
});
}
render() {
return (
<Card>
<CardBlock className="main-table">
<fieldset>
<legend>Pretzels</legend>
{this.state.inputPretzel}
<button onClick={this.incrementPretzel}>Add a Pretzel</button>
<button onClick={this.decrementPretzel}>Remove a Pretzel</button>
</fieldset>
<fieldset>
<legend>Curry</legend>
{this.state.inputCurry}
<button onClick={this.incrementCurry}>Add Curry</button>
<button onClick={this.decrementCurry}>Remove Curry</button>
</fieldset>
<fieldset>
<legend>Wurst</legend>
{this.state.inputPretzel}
<button onClick={this.incrementPretzel}>Add Wurst</button>
<button onClick={this.decrementPretzel}>Remove Wurst</button>
</fieldset>
<Button color="secondary">Options</Button>{' '}
<Button id="btn">Exécuter</Button>
</CardBlock>
</Card>
);
}
}
As you can see I have three different elements and collections of these elements:
- Pretzels
- Currys
- and Wursts
I can add them and remove the last one. but I'd like to remove each one.
In the Html code that I'm placing in the setState and adding to each collection I want to append a delete button or somehow have a delete button next to each line which deletes the right line.