@janiscommerce/ui-native
Version:
components library for Janis app
50 lines (49 loc) • 1.95 kB
JavaScript
import React from 'react';
import { View, TouchableOpacity, StyleSheet } from 'react-native';
import Typography from '../Typography';
import CheckBox from '../CheckBox';
import { horizontalScale, moderateScale, scaledForDevice } from '../../../scale';
const checkLocation = ['left', 'right'];
const CheckSizeValues = {
sm: 16,
md: 24,
lg: 32,
};
const validPaddingHorizontal = scaledForDevice(16, horizontalScale);
const validMarginVertical = scaledForDevice(10, moderateScale);
const validMarginHorizontal = scaledForDevice(15, horizontalScale);
const styles = StyleSheet.create({
container: {
alignItems: 'center',
paddingHorizontal: validPaddingHorizontal,
marginVertical: validMarginVertical,
height: 'auto',
},
row: {
flexDirection: 'row',
justifyContent: 'space-between',
},
reverseRow: {
flexDirection: 'row-reverse',
justifyContent: 'space-between',
},
checkToLeft: {
marginLeft: validMarginHorizontal,
},
checkToRight: {
marginRight: validMarginHorizontal,
},
});
const RadioButton = ({ children, onPress, selected = false, checkPosition = 'left', checkSize = 'sm', disabled = false, style, ...props }) => {
const { container, row, reverseRow, checkToLeft, checkToRight } = styles;
const isStringChild = typeof children === 'string';
const checkLeft = checkPosition === 'left';
const customSize = CheckSizeValues[checkSize];
return (<TouchableOpacity style={[container, checkLeft ? row : reverseRow, style]} disabled={disabled} onPress={onPress} {...props}>
<CheckBox checked={selected} disabled={disabled} customSize={customSize} borderRadius={customSize / 2} onPress={onPress}/>
<View style={checkLeft ? checkToLeft : checkToRight}>
{children && (isStringChild ? <Typography>{children}</Typography> : children)}
</View>
</TouchableOpacity>);
};
export default RadioButton;