GithubHelp home page GithubHelp logo

moschan / react-native-simple-radio-button Goto Github PK

View Code? Open in Web Editor NEW
452.0 13.0 129.0 1022 KB

Simple and handy animated radio button component for React Native

License: MIT License

Java 4.57% JavaScript 74.45% Objective-C 15.06% Python 5.92%

react-native-simple-radio-button's Introduction

react-native-simple-radio-button

simple and useful radio button component for React Native

npmnpm

NPM

Demo

Demo

Installation

in Cli

npm i react-native-simple-radio-button --save

Getting started

import RadioForm, {RadioButton, RadioButtonInput, RadioButtonLabel} from 'react-native-simple-radio-button';


var radio_props = [
  {label: 'param1', value: 0 },
  {label: 'param2', value: 1 }
];

var RadioButtonProject = React.createClass({
  getInitialState: function() {
    return {
      value: 0,
    }
  },
  render: function() {
    return (
      <View>
        <RadioForm
          radio_props={radio_props}
          initial={0}
          onPress={(value) => {this.setState({value:value})}}
        />
      </View>
    );
  }
});

Basic

<RadioForm
  radio_props={radio_props}
  initial={0}
  onPress={(value) => {this.setState({value:value})}}
/>

Advanced

<RadioForm
  radio_props={this.state.types}
  initial={0}
  formHorizontal={false}
  labelHorizontal={true}
  buttonColor={'#2196f3'}
  animation={true}
  onPress={(value) => {this.setState({value:value})}}
/>

Pro

<RadioForm
  formHorizontal={true}
  animation={true}
>
  {/* To create radio buttons, loop through your array of options */}
  {
    radio_props.map((obj, i) => (
      <RadioButton labelHorizontal={true} key={i} >
        {/*  You can set RadioButtonLabel before RadioButtonInput */}
        <RadioButtonInput
          obj={obj}
          index={i}
          isSelected={this.state.value3Index === i}
          onPress={onPress}
          borderWidth={1}
          buttonInnerColor={'#e74c3c'}
          buttonOuterColor={this.state.value3Index === i ? '#2196f3' : '#000'}
          buttonSize={40}
          buttonOuterSize={80}
          buttonStyle={{}}
          buttonWrapStyle={{marginLeft: 10}}
        />
        <RadioButtonLabel
          obj={obj}
          index={i}
          labelHorizontal={true}
          onPress={onPress}
          labelStyle={{fontSize: 20, color: '#2ecc71'}}
          labelWrapStyle={{}}
        />
      </RadioButton>
    ))
  }  
</RadioForm>

Methods

updateIsActiveIndex

Updating active radio button forcibly

Props

RadioForm Component

radio_props (Default: []) *required

radio button value and label array

onPress *required

callback when radio button clicked.

initial (Default: 0)

The index of selected radio button. This is used when this component is activated. If you want to pass initial as asynchronous, you can use updateIsActiveIndex function.

If you want to make it empty initially, please pass -1

buttonColor(Default: '#2196f3')

change radio button color

<RadioForm
  radio_props={radio_props}
  initial={0}
  buttonColor={'#50C900'}
/>

Demo

labelColor(Default: '#000')

change label color

<RadioForm
  radio_props={radio_props}
  initial={0}
  labelColor={'#50C900'}
/>

formHorizontal(Default: false)

change form position

<RadioForm
  radio_props={radio_props}
  initial={0}
  formHorizontal={true}
/>

Demo

labelHorizontal(Default: true)

change label position

<RadioForm
  radio_props={radio_props}
  initial={0}
  labelHorizontal={false}
/>

Demo

animation (Default: true)

if you pass false, animation of radio button is disabled

Demo

accessible

indicates accessibility for the individual radio button input and radio button label components

accessibilityLabel

used to set accessibilityLabel for individual radio button input and radio button label components, radio button input will have accessibilityLabel = [accessibilityLabel]Input[index] radio button label will have accessibilityLabel = [accessibilityLabel]Label[index]

testID

used to set testID for individual radio button input and radio button label components, radio button input will have testID = [testID]Input[index] radio button label will have testID = [testID]Label[index]

RadioButton Component

isSelected

If you pass true to this param, that button change to selected status.

labelHorizontal

change label position to horizontal

buttonColor

The button color

selectedButtonColor

The selected button color

labelColor

The label color

style

The label style

wrapStyle

Styles that are applied to the wrapping the RadioButton component.

onPress *required

callback when radio button clicked.

idSeparator (Default: |)

separator used for extracting id from accessibilityLabel and testID

accessible

indicates accessibility for the wrapped radio button input and radio button label components

accessibilityLabel

used to set accessibilityLabel for the wrapped radio button input and radio button label components, needs to be of the format [accessibilityLabel][separator][id] radio button input will have accessibilityLabel = [accessibilityLabel]Input[index] radio button label will have accessibilityLabel = [accessibilityLabel]Label[index]

testID

used to set testID for individual radio button input and radio button label components, needs to be of the format [testID][separator][id] radio button input will have testID = [testID]Input[index] radio button label will have testID = [testID]Label[index]

<RadioButton
  accessible={true}
  idSeparator=','
  accessibilityLabel='noteType,1'
  testID='noteType,1'
/>

RadioButtonInput

isSelected

onPress

buttonInnerColor

The button inner color

buttonOuterColor

The button inner color

buttonSize

The button size. Width and height will be same length.

buttonOuterSize

The button size of outer. Width and height will be same length.

buttonStyle

Custom button style

buttonWrapStyle

Custom style for view of button's outside

accessible

indicates accessibility for the radio button input component

accessibilityLabel

used to set accessibilityLabel (content description / label for Android) for the radio button input component

testID

used to set testID (id / name for iOS) for the radio button input component

RadioButtonLabel

labelHorizontal

If you pass true, the button and label will be aligned horizontally.

labelStyle

If you pass style, you can change label text style as you want

labelWrapStyle

If you pass style, you can change label wrapper view style as you want

onPress *required

callback when radio button clicked.

accessible

indicates accessibility for the radio button label component

accessibilityLabel

used to set accessibilityLabel (content description / label for Android) for the radio button label component

testID

used to set testID (id / name for iOS) for the radio button label component

Contributing

Of course! Welcome :)

You can use following command in example dir:

npm run sync

During running this command, when you change source to implement/fix something, these changes will sync to example/node_modules/react-native-simple-radio-button/. You can check your change using example project easily.

License

MIT

react-native-simple-radio-button's People

Contributors

blakeohare avatar drakmail avatar eddieatkinson avatar hygkui avatar jlyman avatar joshbroton avatar jpoliachik avatar lucasbesen avatar merapi avatar moschan avatar palerdot avatar raphaelpor avatar rayansostenes avatar rmariuzzo avatar sushant-sardeshpande avatar takayukicho avatar vivekgidmare avatar viveksmart93 avatar will-ockmore avatar williamhatsv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-simple-radio-button's Issues

label color?

hi, great extension, but how to change the color of the label text and also the align of the label text? for example i need to change the label text color to white and center align it.

Animations are Broken in Android

Animations look good on iOS, using the same code on Android produces "fake" animations. It looks like the animations are defaulting to the "basic" behavior of just fading in and out. I'm not seeing the nice popping animation on Android.

react-native: "latest"
react "15.4.1"

Getting more done in GitHub with ZenHub

Hola! @moschan has created a ZenHub account for the moschan organization. ZenHub is the leading team collaboration and project management solution built for GitHub.


How do I use ZenHub?

To get set up with ZenHub, all you have to do is download the browser extension and log in with your GitHub account. Once you do, you’ll get access to ZenHub’s complete feature-set immediately.

What can ZenHub do?

ZenHub adds a series of enhancements directly inside the GitHub UI:

  • Real-time, customizable task boards for GitHub issues;
  • Burndown charts, estimates, and velocity tracking based on GitHub Milestones;
  • Personal to-do lists and task prioritization;
  • “+1” button for GitHub issues and comments;
  • Drag-and-drop file sharing;
  • Time-saving shortcuts like a quick repo switcher.

Add ZenHub to GitHub

Still curious? See more ZenHub features or read user reviews. This issue was written by your friendly ZenHub bot, posted by request from @moschan.

ZenHub Board

RadioButton sizes

Is there a way we can resize the button size?? I tried with height and width but it just messes up the component. Please help.

How can i set the isSelected value?

Hi guys! was trying to get the evaluations alternatives from a api and then make the radioform to show to the user for which alternatives is going to select, but cant set the isSelected value, all the radio buttons appears like not selected, can you give a hand with this?

captura de pantalla 2017-05-09 a la s 18 16 13

And this is how i make the alternatives:

class Accesibility extends Component {

  constructor(props) {
    super(props);
  }

  _setValue(value, index) {
    var ops = options.vae;
    switch(index) {
        case 0:
          ops["vae[0]"] = value;
          break;
        case 1:
          ops["vae[1]"] = value;
          break;
        case 2:
          ops["vae[2]"] = value;
          break;
        case 3:
          ops["vae[3]"] = value;
          break;
        case 4:
          ops["vae[4]"] = value;
          break;
    }
  }

  renderEvaluations() {
    return this.props.evs.map((data, i) => {
      return(
        <View key={i}>
          <Text key={i} style={[styles.title, {marginTop: 40}]}>{data.title}</Text>
          { this.renderAlternatives(data.opts, i) }
        </View>
      )
    })
  }

  renderAlternatives(opts, index) {
    return opts.map((data, i) => {
      return(
          <RadioForm key={i} formHorizontal={false} animation={true} style={styles.radioForm}>
            <RadioButton labelHorizontal={true} key={i} style={styles.radioContainer}>
              <RadioButtonInput
                obj={data.vie_id}
                index={i}
                buttonSize={13}
                buttonInnerColor={'#FFF'}
                buttonOuterColor={'#7F7F7F'}
                isSelected={true}
                onPress={() => this._setValue(data.vie_id, index)}
              />
              <View style={styles.labelContainer}>
                <Text style={styles.labelFormar}>{data.vie_dsc}</Text>
              </View>
            </RadioButton>
          </RadioForm>
      )
    })
  }
  render() {
    return(
        <KeyboardAwareScrollView style={styles.content}>
        {
          this.renderEvaluations()
        }
        </KeyboardAwareScrollView>
      );
  }

Place a radio buttons with dynamic data

I am using this module. How to place a radio button with dynamic data showing in a listView. I placed the radio button in listView, it shows only radio buttons but not shows the data in an array.
Here is my code:

listRow(list, sectionID:number, rowID:number){
   var attributes=[];
   list.options.map((list)=>{
     attributes.push(
       <View style={styles.listOfAttributesView}>
        <View style={styles.dataRowAttributesView}>
        <Text style={{fontSize:15, color:'black'}}>{list}</Text>
       </View>
       </View>
     )
   })
  return(
    <View>
    <RadioForm
     radio_props={attributes}
     initial={0}
     buttonColor={'black'}
     buttonSize={10}
     onPress={(value) => {this.setState({value:this.state.value})}}/>
    </View>

  )
}

Here is my screen shot:
screen shot 2017-09-27 at 12 00 54 pm
Please give me any suggestion. Thanks

`onPress`’s `value` always returns as `false`

Am I doing something wrong? Keep getting false on the onPress function.

Using the button with redux:

<RadioForm style = {{ alignSelf: 'flex-start', marginTop: 12 }}
            radio_props = {[{label: 'Show password', value: this.props.showPassword }]}
            buttonSize = { 12 }
            labelStyle = {{ fontSize: 13, color: '#535353', paddingLeft: 6}}
            buttonColor = { '#E52C51' }
            initial = { this.props.showPassword }
            onPress = { this.onShowPasswordToggle.bind(this) }
/>
onShowPasswordToggle(toggleState: boolean) {
    this.props.showPasswordToggeled(toggleState)
}

Image Support ?

I want to put a image between radio button and label. How can I do ?

onPress() action on View component

When I style the radio button like this. The clickable part is only the yellow part.
Is it possible to put the onPress() action on a View container so that the whole button is clickable.

Thanks, Adri

image

<RadioForm radioStyle={{ height: 45, alignSelf: 'stretch', paddingLeft: 8 }} />

[ios]display quakes.

My app has 2 pages.
page A --navigator.push--> page B
page B --navigator.pop-->page A

B uses react-native-simple-radio-button.
in page B, the inputed radio's value was calulated , and set result to state.

When back to A from B, the display is quakeing up and down for a minute.

If B doesnot calulate(not set state), A is ok, does not quake.
If B calulates(set state), A is NG, quakes.

this issue is just IOS.
android is ok.

When I set [animation={false}], it will be ok.
thanks.

wrong button color

wrong color selected radio button after update to react-native 0.49.3
default
<RadioForm style={radioStyle} ref="radioForm" radio_props={this.state.types1} initial={0} formHorizontal labelHorizontal buttonColor={'#757171'} labelColor={'#757171'} animation onPress={(value, index) => { this.setState({ value1: value, value1Index: index }); }} />

Dynamically setting Initial value does not seem to work

First off, what you built is perfect for what i need, so thank you.

I'm dynamically setting the initial value based on the current state. When I call the function to get the value for initial, it defaults to the first radio button, even though the number returned from the function is correct (in the console). And if I manually put the exact same number into initial - 2 for example - it will select the appropriate radio button, so for the most part the implementation is correct. Can you see any reason why dynamically setting the "initial" value would not work? Thanks!

I'm using 2.5.0-alpha.1

<RadioForm style={{justifyContent:'center', height: 75}}
              radio_props={distance_metrics}
              formHorizontal={true}
              labelHorizontal={false}
              buttonColor={'#ffffff'}
              animation={true}
              labelStyle={styles.settingsLabel}
              initial={this.getInitialRadioSelection()}
              onPress={(value) => {this.changeDistanceUnitMetric({value:value})}}
            />

getInitialRadioSelection()

getInitialRadioSelection() {
    var distanceUnitMetricIs = this.state.distanceUnitMetric;
    var initialSelection = 0;
    switch (distanceUnitMetricIs) {
      case 'meters':
        return 1;
      case 'yards':
        return 0;
      case 'feet':
        return 2;
      default :
        return 0;
    }
  }

Multiple Radio button Group

Hi, your example(type2 and 3) is only applicable for 1 group of radio buttons which sets the index using this.setState on onClick. How can i set the selected index without setting it in this.setState

Radio Form. can't select

"react": "16.0.0-alpha.12",
"react-native": "0.46.4",
"react-native-simple-radio-button": "^2.6.0",
                      <RadioForm
                          radio_props={this.state.prices}
                          formHorizontal={false}
                          labelHorizontal={true}
                          initial={0}
                          buttonColor={'#d82647'}
                          animation={true}
                          onPress={(value) => {this.updateCart(value)}}
                          style={{alignItems: 'flex-start'}}
                        />

radioform

Alignment of radio buttons with different label length

Hey,

I have run into a problem with vertical alignment of radio buttons with different label text length. See image below:

radio button alignment

I would like the radio buttons to all be aligned and start at the red line - how is this possible? I tried with some styling on different objects but nothing seemed to work..

Thanks in advance!

Disabled State

Needs a way to set buttons to 'disabled', where touches have no effect.

Going to modify for my current use case, then try and put together a PR

button issue

when click on same button twice its UI changes how to fix it.

Example?

I can't figure out how you get the value/label of the chosen item so as to respond to it. Could you provide an example? I don't know what state to call to get the results.

Thanks for the module.

Radio Props

You mispeled radio_props in your library, it's spelled radip_props.

Space between button and label

how to mention the space between button and the label and i also need to know how to place the button right side of label

what the (hell) is obj, here?

<RadioButtonInput
  obj={obj}
  index={i}
  isSelected={this.state.value3Index === i}
  onPress={onPress}
  borderWidth={1}
  buttonInnerColor={'#e74c3c'}
  buttonOuterColor={this.state.value3Index === i ? '#2196f3' : '#000'}
  buttonSize={40}
  buttonOuterSize={80}
  buttonStyle={{}}
  buttonWrapStyle={{marginLeft: 10}}
/>
<RadioButtonLabel
  obj={obj}
  index={i}
  labelHorizontal={true}
  onPress={onPress}
  labelStyle={{fontSize: 20, color: '#2ecc71'}}
  labelWrapStyle={{}}
/>

Create flick/glitch while set navigation params

  1. Prepare StackNavigator
  2. Render Radio Buttons in the second screen
  3. Inside second screen, use this.props.navigation.setParams to set any value.
  4. Press back button, you might see the flick/blink/glitch on the navigator -> very ugly effect. I think navigator might be refreshed after setParams

Remove radio buttons and it works fine with set navigation params.

How to set state to initial properties

Hello,

I have 3 radio button in from male.female and other. after submitting from when user select edit from option, i went select button form state. state is contain value 0,1,2.

How can i resolved ? please give suggestions.

Thanks,

Radio button not showing label

Hi there.
First off, thanks for sharing this lib.

I'm having an issue with the label: the only way i can make it shown is by setting:
<RadioForm formHorizontal={true} animation={true} >

When I eliminate or set false the formHorizontal field, the label doesn't show.

The clicked radio is not selected

When i click through the RadioForm it has a strange behaviour. Only every second click gets active.

2017-07-28 14_29_15

When clicking the same radio twice, it works.
Looks like the created radios are not connected correctly?

react-native-simple-radio-button@^2.5.0-alpha.1:
version "2.6.0"
<RadioForm
  radio_props={radioAnswers}
  initial={10}
  buttonColor={'#444'}
  onPress={(value) => this.setState({ answer: value })}
  style={BasicStyles.radios}
/>

Select with the feedback?

Hi,

I am wondering if it possible to provide a feedback text right next to the options (like "correct" or "wrong" and change the color of that option accordingly).

Align basic radios left?

How can i align all the radios to the left (instead of centered elements)?

I implement the radios with

<RadioForm
  radio_props={radios}
  onPress={(value) => this.setState({ selected: value })}
/>

Result:
bildschirmfoto 2017-07-25 um 20 20 04

The text above is centered per textAlign but the RadioForm has no styling added.

returning values...

hi!

i was wondering how to have values function like a normal radio button? i believe in your examples the radio button's values (value: 0) are being used to render the radio button in a specific order.

in my case and in most cases i think it would be better to use an 'order/index' value to display the buttons in a specific order and leave the value as it is normally used. this way someone can just get the button's value.

example:
types1: [{label: 'param1', order: 0, value: 0}, {label: 'param2', order: 1, value: 1}, {label: 'param2', order: 2, value: 3}]

in the above maybe it would be possible to retrieve 'value: 0' and use it.

what do you think?

Customization

Can you make the radio button more customizable like able to change the width of the button, inner color, outer color?

Radio

You keep using the Radio component in your examples, yet you never import it and when I try to import it I get an error. Is this an old documentation?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.