ThakurBallary / react-native-radio-buttons-group

Simple, best and easy to use radio buttons for react native apps.
MIT License
263 stars 71 forks source link

How to get the selected button value? #30

Closed AleenWael closed 3 years ago

ThakurBallary commented 3 years ago

There are multiple ways to get the selected value. One of the way is, using find method on array. arr.find(e => e.selected)?.value

RowlandOti commented 2 years ago

The action/function is never called on pressing a RadioButton i.e onPress: toggleRadio - toggleRadio(id: string) is never invoked. Why? What is the behaviour of onPress()

ThakurBallary commented 2 years ago

onPress function is invoked and updated state of radio buttons is passed to caller function. Please find the link to working example in https://github.com/ThakurBallary/react-native-radio-buttons-group/issues/25#issuecomment-846493544

RowlandOti commented 2 years ago

there are two onPress, one for RadioGroup and another for RadioButton. I am speaking about one for RadioButton. It's never invoked. Can you give sample usage in the example shared?

ThakurBallary commented 2 years ago

RadioButton onPress is invoked on a touch of the item. I'm not sure what made you think that RadioButton onPress is not invoked. Can you please share the code snippet?

victor47n commented 2 years ago

there are two onPress, one for RadioGroup and another for RadioButton. I am speaking about one for RadioButton. It's never invoked. Can you give sample usage in the example shared?

I suffer from the same problem, when using the RadioButton's OnPress(), it is never invoked.

An example: { id: '1', label: 'Test', value: '', borderColor: theme.colors.primary, color: theme.colors.primary, onPress: () => { console.log('Press?') }

RowlandOti commented 2 years ago

@ThakurBallary your example uses the onPress passed to RadioGroup. We are referring to the one passed to a single RadioButton. Can you kindly adjust your example with that uses case to show how to use the RadioButtons onPress(), or otherwise let us know if is faulty, so we may know how to help if possible.

ThakurBallary commented 2 years ago

@RowlandOti @victor47n Thanks for writing. Sorry, it took some time for me to understand. @victor47n example has helped me to understand the issue. Release v2.2.7 with the fix. Working example on Snack

RowlandOti commented 2 years ago

Thanks for the quick fix, I can confirm this works now.

victor47n commented 2 years ago

@ThakurBallary Thanks for the quick fix!

brunozn commented 8 months ago

Para pegar o value fiz assim:

                 onPress={(selectedId) => {
                      const selectedItem = radioButtons.find(item => item.id === selectedId);
                      console.log('selectedItem', selectedItem);

                      if (selectedItem) {
                        setSelectedId(selectedItem.id);
                        onChange(selectedItem.value);
                      }
                    }}