I would like to do a form that could add/remove row by clicking add/remove buttons. My problems now is that, the remove button can always just remove the last row, instead of the row that the remove button located. When I used the array delete operator, it can delete the desired row. Yet, this method did not really delete the object in the array and probably hard for me to do the checking in the for loop. I wonder where did I do wrong?
Updated: 1. Added jsfiddle https://jsfiddle.net/69z2wepo/84246/ 2. Save the dropdown value to parent's state
constructor(props){
super(props)
this.state = {
dropdown: [
{first: "true", hideAdd: "false", result: ""}
]
}
this.addRow = this.addRow.bind(this);
this.removeRow = this.removeRow.bind(this);
this.onChange = this.onChange.bind(this);
}
onChange = (key, value) => {
let oldArray = JSON.parse(JSON.stringify(this.state.dropdown));
oldArray[key]['result'] = value;
this.setState({dropdown:oldArray}, function(){
console.log(this.state.dropdown);
//console.log(this.state.dropdown.length)
})
}
removeRow = (e, key) => {
e.preventDefault();
let oldArray = JSON.parse(JSON.stringify(this.state.dropdown));
oldArray.slice();
oldArray.splice(key, 1);
for ( let i=0; i<oldArray.length; i++){
if (i==0){
oldArray[i]['first'] = 'true';
}
if (oldArray.length==1){
oldArray[i]['hideAdd'] = 'false';
}else{
oldArray[i]['hideAdd'] = 'true';
if ( i == oldArray.length-1){
oldArray[i]['hideAdd'] = 'false';
}
}
}
this.setState({dropdown:oldArray}, function(){
console.log(this.state.dropdown);
//console.log(oldArray);
//console.log(oldArray.length);
})
}
render() {
return (
<Grid.Column>
<Grid style={comStyles().gridWidth}>
<Grid.Row>
<Grid.Column width={4} textAlign='left' style={comStyles().lineHeight}>Then</Grid.Column>
<Grid.Column width={12}>
{
this.state.dropdown.map((item, index) => (
<RulesThenDropdown default={item.result} onChange={this.onChange} add={this.addRow} remove={this.removeRow} id={index} key={index} hideAdd={item.hideAdd} first={item.first} />
))
}
</Grid.Column>
</Grid.Row>
</Grid>
</Grid.Column>
)
}
And the followings are the code from the child component
render() {
const Options = thenOptions;
let extra = "";
const removeBtn = <button onClick={(e,m)=>this.props.remove(e,this.props.id)} className="circular ui icon button"><i className="icon minus"></i></button>
const addBtn = <button onClick={(e,m)=>this.props.add(e,this.props.id)} className="circular ui icon button"><i className="icon plus"></i></button>
if(this.props.first==="false"){
if(this.props.hideAdd=="true"){
extra = removeBtn;
}else{
extra = <div>{addBtn}{removeBtn}</div>;
}
}else if(this.props.first==="true"){
if(this.props.hideAdd!="true"){
extra = addBtn;
}else{
extra = removeBtn;
}
}
return (
<div style={comStyles().buttonsMainWrapper}>
<Form.Dropdown
placeholder='Then'
fluid
selection
options={Options}
defaultValue = {this.props.result}
onChange={(e,{ value })=>this.props.onChange(this.props.id, value)}
/>
<div style={comStyles().buttonsGroup}>
{
extra
}
</div>
</div>
)
}