-2

When I press the delete icon, I want that item to be deleted. but the number at the beginning of the ordered list is not broken. It is class component. what should be in removeItem method ?

No code currently because I try so much things of that and nothing working so I end up on console.logs watch what happened so I deleted it

const ProfileSettings = observer(
class ProfileSettings extends React.Component {
    constructor(props) { 
        super(props)
        this.state = {
            expandIconPosition: 'left',
            buttonDisable: false,
            fetchedNotificationData: [],
            spinning: true,
            checkedA: true,
            checkedB: true,
            checkedC: true,
            list: [],
        };
    }



addListItem = () => {
        var test = this.state.list;
        
        test.push({ text: "",selected:false });            

        this.setState({
            list: test
        });

    }
removeItem = () => {
        ???????????
    }

<div style={{ backgroundColor: "#ffffff", borderRadius: 16, padding: 10, minHeight: 140, width: 406, borderRadius: 8, marginTop: 10, boxShadow: "0px 1px 3px 0px rgba(0, 0, 0, 0.2), 0px 2px 1px -1px rgba(0, 0, 0, 0.12), 0 1px 0 rgba(0, 0, 0, 0.14)" }}>
                                    {(this.state.list.length !== 0) ? this.state.list.map((row) => (
                                        <div className="row" style={{borderRadius:4,marginLeft:5,marginRight:5,paddingLeft : 15, backgroundColor : (row.selected) ? "#1A32FF" : "#FFF"     }}>
                                            <span style={{ marginTop: 2,color:(row.selected) ? "#FFF" : "black" }}>{this.state.list.indexOf(row) + 1}.</span>
                                            <TextField InputProps={{
                                                style:{
                                                    color: row.selected ? 'white' : 'black'
                                                  },
                                                onClick:()=>this.changeRowSelectedOption(row)
                                            }}id="standard-basic" style={{ width: "50%",marginLeft:3 }}></TextField>

                                            <IconButton className={(row.selected) ? "selectedDeleteIconclass" : "deleteIconclass" }  style={{ float: "right" , marginLeft : 115,marginTop:-8}} onClick={() => this.removeItem()}>
                                                <Delete />
                                            </IconButton>
                                        </div>
                                    )) : null}
                                </div>
                            </div>
j08691
  • 204,283
  • 31
  • 260
  • 272

1 Answers1

0

You need to use an array method like filter which is going to return a new array based on a callback.

Doing something like this should work:

removeItem = (item) => {
   var test = [...this.state.list];
   const newArr = test.filter(el => el.text !== item.text);

   this.setState({
       list: newArr
   })

}

The item parameter that removeItem receives is the element you're trying to remove and the filter method is going to return you a new array with all the elements that don't have the text of the element you passed to removeItem function

Brian Polanco
  • 48
  • 1
  • 8