RadioButton example not showing value
Current behaviour
When using the RadioButton component on Android (not tested in iOS yet) the value prop seems to be ignored. The radio buttons are shown, but no text is displayed.
Expected behaviour
According to the docs it seems value prop should represent the text to be shown next to the radio button. I suspect this text would respond to touch as well. However, the sample in the documentation code and screenshots do not match.
Code sample
See the sample in the docs. Clicking the link to open on Snack reproduces the issue.
Screenshots (if applicable)
/
What have you tried
I have put a border around the wrapping component to check whether it was not a matter of having no space to display the text, but the space seems free. As a workaround I am now wrapping the radiobutton in a view together with a text element.
Your Environment
| software | version |
|---|---|
| ios or android | android |
| react-native | 0.57.4 |
| react-native-paper | 2.2.8 |
| node | 11.5.0 |
| npm or yarn | 6.5.0 |
| expo sdk | N/A |
The value prop doesn't display any text. It's for the value the radio button represents. You must display the text yourself.
Can the documentation be updated so that the code matches the screenshot? See: https://callstack.github.io/react-native-paper/radio-button.html
The docs also left me a little confused. This is how you use RadioButton:
import * as React from 'react';
import { View } from 'react-native';
import { RadioButton, Text } from 'react-native-paper';
export default class MyComponent extends React.Component {
state = {
value: 'first',
};
render() {
return(
<RadioButton.Group
onValueChange={value => this.setState({ value })}
value={this.state.value}
>
<View>
<Text>First</Text>
<RadioButton value="first" />
</View>
<View>
<Text>Second</Text>
<RadioButton value="second" />
</View>
</RadioButton.Group>
)
}
}
Would like to leave another note to this that I also found the documentation for the RadioButton components quite confusing - I expected value to render text next to the button, and it does not.
@mcjohnalds how do we make the Text on the same line as the radiobutton and fully clickable?
@DianaKoenraadt Check source of radio button example in our example app. We wrap it with a Touchable and set pointerEvents to properly handle onPress.
The docs also left me a little confused. This is how you use RadioButton:
import * as React from 'react'; import { View } from 'react-native'; import { RadioButton, Text } from 'react-native-paper'; export default class MyComponent extends React.Component { state = { value: 'first', }; render() { return( <RadioButton.Group onValueChange={value => this.setState({ value })} value={this.state.value} > <View> <Text>First</Text> <RadioButton value="first" /> </View> <View> <Text>Second</Text> <RadioButton value="second" /> </View> </RadioButton.Group> ) } }
If you need to show if checked or not you can add to each RadioButton the status props:
<RadioButton.Group>
<View style={{flexDirection: 'row'}}>
<RadioButton value="first" status={value === 'first' ? 'checked' : 'unchecked'} />
<Text style={{marginLeft: 10}}>SOMETHING</Text>
</View>
</RadioButton.Group>
import * as React from 'react';
import { View } from 'react-native';
import { RadioButton, Text } from 'react-native-paper';
const MyComponent = () => {
const [value, setValue] = React.useState('first');
return (
<RadioButton.Group onValueChange={newValue => setValue(newValue)} value={value}>
<View>
<Text>First</Text>
<RadioButton value="first" />
</View>
<View>
<Text>Second</Text>
<RadioButton value="second" />
</View>
</RadioButton.Group>
);
};
export default MyComponent;
### Typescript RadioField.tsx
import * as React from "react";
import {View, Text, StyleSheet, TouchableOpacity} from "react-native";
import {RadioButton} from "react-native-paper";
export default function ({value, title, error, padding = 8, options = [], onValueChange}: {
title: string,
error?: string,
padding?: number,
value?: string,
options: string[],
onValueChange?: (value: string) => void
}) {
return (
<View style={{
padding: padding
}}>
{title && <Text style={styles.title}>{title}</Text>}
<RadioButton.Group
onValueChange={(value) => {
if (onValueChange) onValueChange(value)
}}
value={value ? value : ""}>
{options.map((item) => (
<TouchableOpacity onPress={() => {
if (onValueChange)
onValueChange(item)
}}>
<View key={item} style={styles.radioContainer}>
<Text>{item}</Text>
<RadioButton
color={'red'}
value={item}
status={`${value}` === `${item}` ? 'checked' : 'unchecked'}
onPress={() => {
if (onValueChange) onValueChange(`${item}`)
}}
/>
</View>
</TouchableOpacity>
))}
</RadioButton.Group>
{error && <Text style={styles.error}>{error}</Text>}
</View>
)
}
const styles = StyleSheet.create({
radioContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
backgroundColor: "#ffffff",
minHeight: 48,
paddingStart: 8,
paddingEnd: 8,
borderRadius: 4,
marginBottom: 4
},
title: {
color: "#424242",
marginBottom: 4
},
error: {
color: "#ff5e6a",
marginTop: 4,
fontSize: 12
}
});
@mcjohnalds how do we make the
Texton the same line as the radiobutton and fully clickable?
1.Wrap Text and Radiobutton inside a View element and add style ( flexDirection: 'row' ). This puts text and radio button on same line.
2. Add an onPress() Handler to