项目作者: marouanekadiri

项目描述 :
Flexible and Easy Radio Form components for react native
高级语言: JavaScript
项目地址: git://github.com/marouanekadiri/Radio-react-native.git
创建时间: 2018-03-10T15:32:43Z
项目社区:https://github.com/marouanekadiri/Radio-react-native

开源协议:

下载


radio-react-native

a react native javascript components that allow you to create a Radio Button group.

Installation

  1. npm install --save radio-react-native

Demo

ezgif-1-e0ecd0dd6a

Getting Started

  1. import {Radio, RadioGroup,RadioButton} from "radio-react-native";
  2. <RadioGroup
  3. defaultChoice={1}
  4. onChoose={(value,index)=>this.onChooseGender(value,index)}
  5. >
  6. <RadioButton value={"M"}>
  7. <Text>Male</Text><Radio></Radio>
  8. </RadioButton>
  9. <RadioButton value={"F"}>
  10. <Radio></Radio><Text> Female</Text>
  11. </RadioButton>
  12. </RadioGroup>

Examples

The Demo section shows Three different examples of the RadioGroup. In this section, I will show you how to use the radio-react-native library.

Gender example

  1. <RadioGroup
  2. defaultChoice={1}
  3. style={styles.flexRow}
  4. onChoose={(value,index)=>this.onChooseGender(value,index)}>
  5. <RadioButton
  6. style={[styles.flexRow,styles.center,styles.halfWidth]}
  7. value={"M"}>
  8. <Text style={styles.text}>Male </Text>
  9. <Radio></Radio>
  10. </RadioButton>
  11. <RadioButton
  12. style={[styles.flexRow,styles.center,styles.halfWidth]}
  13. value={"F"}>
  14. <Radio></Radio>
  15. <Text style={styles.text}\> Female</Text>
  16. </RadioButton>
  17. </RadioGroup>

In this example, we have a radio form that contains two choices (Male, female). Two RadioButtons wrapped inside the Radio Group.
Once you click on a radio the event OnChoose is emitted and you can get the value and the index of the choosen radioButton.

Age example

  1. const ages = [
  2. {id:1,age:'0 - 2',label:'baby'},
  3. {id:2,age:'3 - 8',label:'kid'},
  4. {id:'3',age:'9 - 16',label:'teenager'},
  5. {id:4,age:'17 - 24',label:'adult'}
  6. ];
  7. <RadioGroup
  8. defaultChoice={3}
  9. onChoose={(value,index)=>this.onChooseAge(value,index)}>
  10. {ages.map((age)=>
  11. <RadioButton
  12. key={age.id}
  13. style={[styles.flexRow,styles.start]}
  14. value={age}>
  15. <Radio></Radio>
  16. <Text style={styles.text}>{age.label} ({age.age} years old)</Text>
  17. </RadioButton>
  18. )}
  19. </RadioGroup>

You can also iterate on a list and render a radio Form to choose between many options.

Custom Radio example

  1. class ThumpsUp extends Component<Props>{
  2. render(){
  3. const {checked} = this.props;
  4. if(checked){
  5. return <Icon
  6. name={"ios-thumbs-up"}
  7. style={{color:'blue'}}></Icon>
  8. }
  9. else{
  10. return <Icon
  11. name={"ios-thumbs-up-outline"} ></Icon>
  12. }
  13. }
  14. }
  15. class ThumpsDown extends Component<Props>{
  16. render(){
  17. const {checked} = this.props;
  18. if(checked){
  19. return <Icon
  20. name={"ios-thumbs-down"}
  21. style={{color:'red'}}></Icon>
  22. }
  23. else{
  24. return <Icon
  25. name={"ios-thumbs-down-outline"} ></Icon>
  26. }
  27. }
  28. }

ThumpsUp and ThumpsDown are two component defined to be used as a clickable radio in our Radio group.
We need to use the checked props to return the representation of our Radio instead of the default one.

  1. <RadioGroup
  2. defaultChoice={0}
  3. onChoose={(value,index)=>this.onChooseLike(value,index)}>
  4. <RadioButton
  5. style={[styles.flexRow,styles.start]}
  6. value={true}>
  7. <Radio CustomComponent={ThumpsUp}></Radio>
  8. <Text style={styles.text}\> Yes</Text>
  9. </RadioButton>
  10. <RadioButton
  11. style={[styles.flexRow,styles.start]}
  12. value={false}>
  13. <Radio CustomComponent={ThumpsDown}></Radio>
  14. <Text style={styles.text}\> No</Text>
  15. </RadioButton>
  16. </RadioGroup>

In this example, we customize the radio to render ThumpsUp adn ThumpsDown instead of the default Radio.