Here's my current code : (2 Files & Classes : "FoodStandComponent.jsx")
/* ************************************* */
/* ******** IMPORTS ******** */
/* ************************************* */
import uuid from 'uuid/v4';
import { Card, CardBlock, Button, InputGroup, Input } from 'reactstrap';
import React, { Component } from 'react';
import ProviderInfos from '../ProviderInfos/ProviderInfos';
import InputType from './InputType/InputType';
/* ************************************* */
/* ******** VARIABLES ******** */
/* ************************************* */
const propTypes = {
newInput: React.PropTypes.array,
exportInput: React.PropTypes.func,
};
/* ************************************* */
/* ******** COMPONENT ******** */
/* ************************************* */
class FoodStandComponent extends Component {
constructor(props) {
super(props);
this.state = {
newInput: [
{ name: 'Pretzel', id: uuid() },
{ name: 'Curry', id: uuid() },
{ name: 'Wurst', id: uuid() },
],
InValue: '',
};
this.add = this.add.bind(this);
this.remove = this.remove.bind(this);
}
add = (name) => {
const ninput = this.state.newInput.concat({ name, id: uuid(), value: this.state.InValue });
this.setState({
newInput: ninput,
InValue: '',
});
};
remove = (id, name) => {
const toBeRemoved = this.state.newInput.filter(x => x.name === name).pop();
if (toBeRemoved) {
this.setState({
newInput: this.state.newInput.filter(x => x.name !== name).concat(
this.state.newInput.filter(x => x.name === name && x.id !== toBeRemoved.id),
),
});
}
};
render() {
console.log();
const cubeFifteenOrUnder = this.state.newInput.filter(x => x.name === 'Pretzel')
&& this.state.newInput.filter(x => x.name === 'Pretzel').length <= 13;
const dsoFifteenOrUnder = this.state.newInput.filter(x => x.name === 'Curry')
&& this.state.newInput.filter(x => x.name === 'Curry').length <= 13;
const multiFifteenOrUnder = this.state.newInput.filter(name => name === 'Wurst')
&& this.state.newInput.filter(x => x.name === 'Wurst').length <= 13;
return (
<Card>
<CardBlock className="main-table">
<fieldset>
<legend>Pretzels</legend>
<InputGroup>
<Input placeholder="Pretzel" />
<ProviderInfos type="Pretzel" />
{ cubeFifteenOrUnder && (
<div className="plus" onClick={() => this.add('Pretzel')}>
<i className="fa fa-plus" aria-hidden="true" />
</div>
) }
{ !cubeFifteenOrUnder && (
<div className="plus-off">
<i className="fa fa-plus" aria-hidden="true" />
</div>
) }
</InputGroup>
{this.state.newInput.map((mapStorageVariable) => {
if (mapStorageVariable.name === 'Pretzel') {
return (<InputType
id={mapStorageVariable.id}
placeholder={mapStorageVariable.name}
value={mapStorageVariable.value}
onRemove={() => this.remove(mapStorageVariable.id, mapStorageVariable.name)}
/>);
}
return null;
})}
</fieldset>
<fieldset>
<legend>Curries</legend>
<InputGroup>
<Input placeholder="Curry" />
<ProviderInfos type="Curry" />
{ dsoFifteenOrUnder && (
<div className="plus" onClick={() => this.add('Curry')}>
<i className="fa fa-plus" aria-hidden="true" />
</div>
) }
{ !dsoFifteenOrUnder && (
<div className="plus-off">
<i className="fa fa-plus" aria-hidden="true" />
</div>
) }
</InputGroup>
{this.state.newInput.map((mapStorageVariable) => {
if (mapStorageVariable.name === 'Curry') {
return (<InputType
id={mapStorageVariable.id}
placeholder={mapStorageVariable.name}
value={mapStorageVariable.value}
onRemove={() => this.remove(mapStorageVariable.id, mapStorageVariable.name)}
/>);
}
return null;
})}
</fieldset>
<fieldset>
<legend>Wursts</legend>
<InputGroup>
<Input placeholder="Wurst" />
<ProviderInfos type="Wurst" />
{ multiFifteenOrUnder && (
<div className="plus" onClick={() => this.add('Wurst')}>
<i className="fa fa-plus" aria-hidden="true" />
</div>
) }
{ !multiFifteenOrUnder && (
<div className="plus-off">
<i className="fa fa-plus" aria-hidden="true" />
</div>
) }
</InputGroup>
{this.state.newInput.map((mapStorageVariable) => {
if (mapStorageVariable.name === 'Wurst') {
return (<InputType
id={mapStorageVariable.id}
placeholder={mapStorageVariable.name}
value={mapStorageVariable.value}
onRemove={() => this.remove(mapStorageVariable.id, mapStorageVariable.name)}
/>);
}
return null;
})}
</fieldset>
<Button color="secondary">Options</Button>{' '}
<Button id="btn">Exécuter</Button>
</CardBlock>
</Card>
);
}
}
SearchExtendedComponent.propTypes = propTypes;
export default SearchExtendedComponent;
(and InputTypeComponent.jsx )
/* ************************************* */
/* ******** IMPORTS ******** */
/* ************************************* */
import ProviderInfos from '../../ProviderInfos/ProviderInfos';
import React, { Component } from 'react';
import { Card, CardBlock, Button, InputGroup, Input } from 'reactstrap';
/* ************************************* */
/* ******** VARIABLES ******** */
/* ************************************* */
/* ************************************* */
/* ******** COMPONENT ******** */
/* ************************************* */
export default class InputTypeComponent extends Component {
constructor(props) {
super(props);
this.state = {
type: '',
};
}
onRemove = () => {
this.props.onRemove(this.props.id);
}
onChange = () => {
this.props.onChange(this.props.id);
}
render() {
const { placeholder, id, value } = this.props;
const { type } = this.state;
this.type = placeholder;
return (
<InputGroup key={id}>
<Input placeholder={placeholder} />{value}
<ProviderInfos type={this.type} />
<div className="minus" onClick={this.onRemove}>
<i className="fa fa-minus" aria-hidden="true" />
</div>
</InputGroup>
);
}
}
I'm trying to get a list with "add" and "remove" buttons refactored into one function.
as you can see above thanks to @Jacky Choo's answer & code I'm almost there but the issue is that I've lost the functionality I previously had of having the line I want deleted removed when I click on it's own remove button.
When I click on this minus sign the line with the text and changed checkboxes stays. and the very last line dissapears.
UPDATE :
Fixed it! By changing the remove to this I get my intended result. yes the lines below the deleted one are reset but that is for Redux to handle. A big shoutout to @Jacky Choo who basically figured it out for me!
remove = (id, name) => {
this.setState({
newInput: this.state.newInput.filter(x => x.name === name && x.id !== id),
});
};