0

I am creating dynamic checboxes based on an array of objects but I am unable to set change handlers for the same . I am using semantic-ui-react Checkboxes. How do I handle change events for the corresponding change checkboxes.

Also There is a submit button , I need to get items that are checked .Can someone help me with this too

Help would be appreciated

Checkbox render code


    let arr = [  {key: "Pending", text: "Pending", checked: false}
         {key: "Approved", text: "Approved", checked: false}
         {key: "Cancelled", text: "Cancelled", checked: false}
    ];

    this.state ={ optionsArr: [] }

    {this.state.arr.map((item: any, i: number) => (
                      <div className="menu-item" key={i}>
                        <Checkbox
                          name={item.text}
                          onChange={this.handleItemClick}
                          checked={item.checked}
                          label={item.text}
                        />
                      </div>
      ))}

Change Handler

     handleItemClick = (event: React.FormEvent<HTMLInputElement>, data: any) => {
        const i = this.state.optionsArr.findIndex(
          (item: any) => item.text === data.name
        );
        const optionsArr = this.state.optionsArr.map((prevState: any, si: any) =>
          si === i ? !prevState : prevState
        );
        this.setState({ optionsArr });
      };

2
  • Can you add a whole file? Commented Jun 27, 2019 at 12:31
  • The first time you loop through the optionsArr, you use item.text on each item. The second time you loop through, if si === i you try to return !item. I'm not sure that makes sense, think you want to return {text: prevState.text, checked: !prevState.checked} Commented Jun 27, 2019 at 12:32

1 Answer 1

2

Think you are returning the wrong thing when trying to update the state of the options array.

prevState is an object, like {key: 'string', text: 'string', checked: boolean}, so doing !prevState doesn't make sense. Try:

     handleItemClick = (event: React.FormEvent<HTMLInputElement>, data: any) => {
        const i = this.state.optionsArr.findIndex(
          (item: any) => item.text === data.name
        );
        const optionsArr = this.state.optionsArr.map((prevState: any, si: any) =>
          si === i ? {...prevState, checked: !prevstate.checked} : prevState
        );
        this.setState({ optionsArr });
      };

You can get the checked items by using filter:


this.state.optionsArr.filter((item: any) => item.checked)
Sign up to request clarification or add additional context in comments.

4 Comments

Use filter. Updated my answer
store the original state in the state object also. Then you can call this.setState({optionsArr: this.state.original})
That would give me all checkoxes unchecked as I am setting it to false in the begining. I want the previous state
What have you tried? I'm sure you can come up with something to store the previous state...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.