Reputation: 1575
I am converting React code to React Native. So I need to implement radio buttons.
Upvotes: 47
Views: 166629
Reputation: 11
import React, { useState } from "react";
import { Ionicons } from "@expo/vector-icons";
import { View, Text, TouchableOpacity } from "react-native";
export default function RadioButton() {
const [radioButton, setRadioButton] = useState("Yes");
return (
<View style={{ flexDirection: "row", justifyContent: "space-around" }}>
<TouchableOpacity onPress={() => setRadioButton("Yes")}>
<Text>
<Ionicons name={ radioButton === "Yes" ? "radio-button-on" : "radio-button-off" } size={18} color="green" />
Yes
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => setRadioButton("No")}>
<Text>
<Ionicons name={radioButton === "No" ? "radio-button-on" : "radio-button-off"} size={18} color="green" />
No
</Text>
</TouchableOpacity>
</View>
)}
Upvotes: 1
Reputation: 184
i am using my own custom radio buttons group.it style can be custmoized also.
// radio button component
import React, { useState } from 'react'
import { Text, TouchableOpacity, View } from 'react-native'
const RadioButtons = (props) => {
const radioPress = () => {
props.setChecked(props?.item?.id)
}
return (
<View style={{
flexDirection: "row",
alignItems: "center",
justifyContent: "center",
margin:5,
}}>
<TouchableOpacity style={{
}} onPress={radioPress}>
<View style={[{
height: 24,
width: 24,
borderRadius: 12,
borderWidth: 2,
borderColor: '#000',
alignItems: 'center',
justifyContent: 'center',
}, props.style]}>
{
props?.checked == props?.item?.id ?
<View style={{
height: 12,
width: 12,
borderRadius: 6,
backgroundColor: '#000',
}} />
: null
}
</View>
</TouchableOpacity>
<Text style={{
marginLeft: 5,
fontWeight:"500",
fontSize:20,
textTransform:"capitalize"
}}>
{props?.item?.label}
</Text>
</View>
)
}
export default RadioButtons
used in any where eg:
<View style={{
flexDirection:"row",
flexWrap:"wrap",
}}>
{
[{label:'male',id:0}, {label:'female',id:1},
{label:'other',id:2}].map((item,i)=>{
return(
<RadioButtons key={i} item={item} checked=
{checked} setChecked={setChecked} />
)
})
}
</View>
Upvotes: 0
Reputation: 11244
import RadioForm, { RadioButton, RadioButtonInput, RadioButtonLabel } from 'react-native-simple-radio-button';
const radioProps = [
{ label: 'Male', value: false },
{ label: 'Female, value: true }
];
<View style={{ marginTop: 30 }}>
<RadioForm
buttonColor={gray}
buttonSize={12}
radioStyle={{paddingTop:25}}
selectedButtonColor="#000000"
radio_props={radioProps}
initial={0}
animation={false}
onPress={(value) => setGender(value)}
/>
</View>
Upvotes: 0
Reputation: 101
Here is my solution for radio button using functional components.
Note - I have used images for checked & unchecked radio icon
import React, {useState} from 'react';
import {View, Text, StyleSheet, TouchableOpacity, Image} from 'react-native';
const Radio = () => {
const [checked, setChecked] = useState(0);
var gender = ['Male', 'Female'];
return (
<View>
<View style={styles.btn}>
{gender.map((gender, key) => {
return (
<View key={gender}>
{checked == key ? (
<TouchableOpacity style={styles.btn}>
<Image
style={styles.img}
source={require('../images/radio_Checked.jpg')}
/>
<Text>{gender}</Text>
</TouchableOpacity>
) : (
<TouchableOpacity
onPress={() => {
setChecked(key);
}}
style={styles.btn}>
<Image
style={styles.img}
source={require('../images/radio_Unchecked.png')}
/>
<Text>{gender}</Text>
</TouchableOpacity>
)}
</View>
);
})}
</View>
{/* <Text>{gender[checked]}</Text> */}
</View>
);
};
const styles = StyleSheet.create({
radio: {
flexDirection: 'row',
},
img: {
height: 20,
width: 20,
marginHorizontal: 5,
},
btn: {
flexDirection: 'row',
alignItems: 'center',
},
});
export default Radio;
Upvotes: 8
Reputation: 21
Maybe you could style into a circle button such as a radio button or something:
const period = [
{
key: 1,
name : '1 Month',
value : 1,
},
{
key : 2,
name : '12 Month',
value : 12,
}
];
constructor(props) {
super(props);
this.state = {
value: 0,
};
}
onChangeTabs = (tabs) => {
this.setState({
value : tabs,
})
}
renderTabs = () => {
return period.map(item => (
<TouchableWithoutFeedback
key={item.key}
value={item.value}
onPress={this.onChangeTabs.bind(this, item.value)}
>
<View style={this.state.value == item.value ? styles.periodActive : styles.period}>
<Text style={styles.periodText}>{item.name}</Text>
</View>
</TouchableWithoutFeedback>
));
};
const styles = StyleSheet.create({
period: {
borderRadius: 5,
borderColor: '#fff',
borderWidth: 1,
marginHorizontal: 5,
},
periodActive: {
backgroundColor: '#333',
},
});
Upvotes: 1
Reputation: 69
I use Checkbox
in react-native
for creating the radio button. Please refer below code.
constructor(props){
super(props);
this.state = {radioButton:'value1'};
}
render(){
return(
<View>
<CheckBox
title='value1'
checkedIcon='dot-circle-o'
uncheckedIcon='circle-o'
checked={this.state.radioButton === 'value1'}
onPress={() => this.setState({radioButton: 'value1'})}
></CheckBox>
<CheckBox
title='value2'
checkedIcon='dot-circle-o'
uncheckedIcon='circle-o'
checked={this.state.radioButton === 'value2'}
onPress={() => this.setState({radioButton: 'value2'})}
></CheckBox>
<CheckBox
title='value3'
checkedIcon='dot-circle-o'
uncheckedIcon='circle-o'
checked={this.state.radioButton === 'value3'}
onPress={() => this.setState({radioButton: 'value3'})}
></CheckBox>
<CheckBox
title='value4'
checkedIcon='dot-circle-o'
uncheckedIcon='circle-o'
checked={this.state.radioButton === 'value4'}
onPress={() => this.setState({radioButton: 'value4'})}
></CheckBox>
<CheckBox
title='value5'
checkedIcon='dot-circle-o'
uncheckedIcon='circle-o'
checked={this.state.radioButton === 'value5'}
onPress={() => this.setState({radioButton: 'value5'})}
></CheckBox>
</View>
);
}
Upvotes: 2
Reputation: 1596
This is another way of creating radioButtons (Source, thanks to php step by step channel)
Method 1
constructor(props) {
super(props);
this.state = {
radioBtnsData: ['Item1', 'Item2', 'Item3'],
checked: 0
}
}
import { View, TextInput, TouchableOpacity } from 'react-native';
{this.state.radioBtnsData.map((data, key) => {
return (
<View key={key}>
{this.state.checked == key ?
<TouchableOpacity style={styles.btn}>
<Image style={styles.img} source={require("./img/rb_selected.png")}/>
<Text>{data}</Text>
</TouchableOpacity>
:
<TouchableOpacity onPress={()=>{this.setState({checked: key})}} style={styles.btn}>
<Image style={styles.img} source={require("./img/rb_unselected.png")} />
<Text>{data}</Text>
</TouchableOpacity>
}
</View>
)
})}
const styles = StyleSheet.create({
img:{
height:20,
width: 20
},
btn:{
flexDirection: 'row'
}
});
Place below images in img folder
Method 2
Elaborated LaneRettig ex for new developers
Thanks to Lane Rettig
constructor(props){
super(props);
this.state = {
radioSelected: 1
}
}
radioClick(id) {
this.setState({
radioSelected: id
})
}
render() {
const products = [{
id: 1
},
{
id: 2
},
{
id: 3
}];
return (
products.map((val) => {
return (
<TouchableOpacity key={val.id} onPress={this.radioClick.bind(this, val.id)}>
<View style={{
height: 24,
width: 24,
borderRadius: 12,
borderWidth: 2,
borderColor: '#000',
alignItems: 'center',
justifyContent: 'center',
}}>
{
val.id == this.state.radioSelected ?
<View style={{
height: 12,
width: 12,
borderRadius: 6,
backgroundColor: '#000',
}} />
: null
}
</View>
</TouchableOpacity>
)
})
);
}
Upvotes: 22
Reputation: 905
You can use react-native-radio-input. Its very simple to use.
import RadioGroup,{Radio} from "react-native-radio-input";
.
.
.
//Receive the checked value (ES6 syntax)
getChecked = (value) => {
// value = our checked value
alert(value)
}
<RadioGroup getChecked={this.getChecked}>
<Radio iconName={"lens"} label={"The first option"} value={"A"} />
<Radio iconName={"lens"} label={"The first option"} value={"B"} />
<Radio iconName={"lens"} label={"I need numbers"} value={1} />
<Radio label={"Is IconName Optional?"} value={"Yes"} />
<Radio label={"Show Sentence Value"} value={"This is a Sentence"} />
</RadioGroup>
.
.
Upvotes: 0
Reputation: 6950
You can mimic a radio button really easily using just barebones RN. Here's one simple implementation which I use. Tweak size, colors etc. as you like. It looks like this (with a different tint, and some text). Add TouchableOpacity
on top to turn it into a button that does something.
function RadioButton(props) {
return (
<View style={[{
height: 24,
width: 24,
borderRadius: 12,
borderWidth: 2,
borderColor: '#000',
alignItems: 'center',
justifyContent: 'center',
}, props.style]}>
{
props.selected ?
<View style={{
height: 12,
width: 12,
borderRadius: 6,
backgroundColor: '#000',
}}/>
: null
}
</View>
);
}
Upvotes: 131
Reputation: 17771
There is a react-native component called react-native-radio-buttons that may do some of what you need:
Upvotes: 7