bietkul / react-reactive-form

Angular like reactive forms in React.
MIT License
309 stars 32 forks source link

How to implement Radio button with Reactive-Forms in React Native #50

Closed Dhirajbhujbal closed 5 years ago

Dhirajbhujbal commented 5 years ago

Actually, I want to implement the Radio button in React Native.

I tried to display Radio buttons on the UI and rendred under Field Control. but when I am selecting another radio button Form Control render is not re-rendred.

bietkul commented 5 years ago

Can you please share the code snippet?

Dhirajbhujbal commented 5 years ago

yes

`/*

import * as React from 'react'; import { StyleSheet, Text, View, Button, TextInput, Alert } from 'react-native'; import { FormBuilder, FieldGroup, FieldControl, Validators, AbstractControl, } from "react-reactive-form"; import { RadioButton } from 'react-native-paper';

/**

const styles = StyleSheet.create({ container: { flex: 1, flexDirection: 'column', backgroundColor: 'rgba(0,0,0,0.8)', borderBottomRightRadius: 30, }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, } }); `

bietkul commented 5 years ago

You can use it like that.

<FieldControl
    name='gender'
    render={({handler}) => {
        const inputHandler = handler('switch');
        (
        <>
            {/* here want to right the code for radio button
                but in React Native there is no any Native tag For Radio button
                So I need to create some custom components
                and need to maintain state here
                I called setState on click of the radio button but form is not re-rendered.
            */}

            <RadioButton.Group
                onValueChange={inputHandler.onChange}
                {...inputHandler}
            >
                <View>
                    <Text>First</Text>
                    <RadioButton value='first' />
                </View>
                <View>
                    <Text>Second</Text>
                    <RadioButton value='second' />
                </View>
            </RadioButton.Group>

        </>
    )
    }}
/>
Dhirajbhujbal commented 5 years ago

okay, I will check this. Also, I found one solution in which my issue is resolved, Instead of the field control, I have used Field Component provided by react-reactive-form by referring this (check last content for DatePickerIOS).

Thanks 👍