Skip to content

Instantly share code, notes, and snippets.

@shubh007-dev
Created June 25, 2019 14:39
Show Gist options
  • Save shubh007-dev/0d8a0ca4d6f7d1530f3e28d223f9199e to your computer and use it in GitHub Desktop.
Save shubh007-dev/0d8a0ca4d6f7d1530f3e28d223f9199e to your computer and use it in GitHub Desktop.
export class RadioButton extends Component {
constructor(props){
super(props);
this.state = {
radioSelected: this.props.selectedItemId,
springValue: new Animated.Value(1.1),
}
}
radioClick(id) {
this.setState({radioSelected: id});
this.props.onChange(id);
this.spring();
}
spring () {
Animated.spring(
this.state.springValue,
{
toValue: 0.95,
friction: 2,
tension: 15,
}
).start()
}
renderRadioButton = (item) => {
return(
<View key={item.id} style={STYLES.radioContainerView}>
<TouchableOpacity style={STYLES.radioButtonDirectionStyle} onPress={() => this.radioClick(item.id)}>
{ (this.props.labelOnLeft == true) ?
<Text style={[this.props.labelLeftStyle]}>{item.label}</Text>
:
null
}
<View style={[ (item.id == this.state.radioSelected) ?
STYLES.selectedView
:
STYLES.unselectedView
]}>
{
(item.id == this.state.radioSelected) ?
<Animated.View style={[STYLES.radioSelected, { transform: [{scale: this.state.springValue}] } ]} />
:
null
}
</View>
{ (this.props.labelOnRight == true) ?
<Text style={[this.props.labelRightStyle]}>{item.label}</Text>
:
null
}
</TouchableOpacity>
</View>
)
}
render() {
return(
<FlatList
data={this.props.radioOptions}
extraData={this.state}
renderItem={({item}) => this.renderRadioButton(item)}
keyExtractor={(item, index) => index.toString()}
/>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment