react-native-paper icon indicating copy to clipboard operation
react-native-paper copied to clipboard

RadioButton example not showing value

Open wimgoeman opened this issue 7 years ago • 10 comments

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

wimgoeman avatar Dec 20 '18 12:12 wimgoeman

The value prop doesn't display any text. It's for the value the radio button represents. You must display the text yourself.

satya164 avatar Dec 20 '18 13:12 satya164

Can the documentation be updated so that the code matches the screenshot? See: https://callstack.github.io/react-native-paper/radio-button.html

DianaLaa avatar Apr 03 '19 15:04 DianaLaa

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>
    )
  }
}

mcjohnalds avatar Jul 12 '19 09:07 mcjohnalds

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.

Maximell avatar Jul 29 '19 20:07 Maximell

@mcjohnalds how do we make the Text on the same line as the radiobutton and fully clickable?

DianaLaa avatar Oct 08 '19 16:10 DianaLaa

@DianaKoenraadt Check source of radio button example in our example app. We wrap it with a Touchable and set pointerEvents to properly handle onPress.

Trancever avatar Oct 08 '19 20:10 Trancever

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>

henriquedavid avatar Mar 31 '20 22:03 henriquedavid

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;

anonewb avatar Jan 15 '21 09:01 anonewb

### 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
    }
});

ngima avatar Apr 13 '21 20:04 ngima

@mcjohnalds how do we make the Text on 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 element adjacent to radio button itself and set value, so that even the text element becomes clickable along with the adjacent radio button. Adds Better UX.

Refer:

brahmavidyamandir avatar Jan 24 '23 07:01 brahmavidyamandir