Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checking CheckBoxes created using map method #169

Open
munsim opened this issue May 15, 2022 · 4 comments
Open

checking CheckBoxes created using map method #169

munsim opened this issue May 15, 2022 · 4 comments

Comments

@munsim
Copy link

munsim commented May 15, 2022

Hi

My react native app needs to create list of checkBoxes using map method. User presses button - new checkbox appears (with some text next to it). You can call it a todo list.

I used example from docs:
` const [toggleCheckBox, setToggleCheckBox] = useState(false)

<CheckBox
disabled={false}
value={toggleCheckBox}
onValueChange={(newValue) => setToggleCheckBox(newValue)}
/>`

So lets say i have 3 checkboxes, all with the same value. When i tick on 1 checkbox - all checkboxes are ticked.

So how can I tick only 1 checkbox? Please, note user still should be able to remove and add checkboxes.

@ManiAzza
Copy link

Same problem here !!

@JonasJs
Copy link

JonasJs commented Jun 12, 2022

  1. Same problem here !!

@JonasJs
Copy link

JonasJs commented Jun 12, 2022

In the native functions of the library I'm didn't find, so I ended up creating my own example.
The code and concept may not be the best, but that's what I found and I believe we can improve.

1. State with dynamic options

const [options, setOptions] = useState([
    {
      label: "item 1",
      actived: false
    },
    {
      label: "item 2",
      actived: false
    },
    {
      label: "item 3",
      actived: false
    }
  ]);

2. Method to select one option.

  function handleSelectOption(label){
    const newItems = [...options];
    const newOptions = newItems.map(item => {
      return {
        ...item,
        actived: item.label === label,
      }
    });
    
    setOptions(newOptions);
  }

3.Rendered code.

The disabled parameter is true to remove the toggle function.

{options.map((item, index) => (
   <TouchableOpacity key={index} onPress={() => handleSelectItem(item.label)}>
    <CheckBox
      disabled={true}
      value={item.actived}
    />
  </TouchableOpacity>
))}

@andreas-bergstrom
Copy link

You need to put the checkbox in it's own component and put the state inside that component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants