-
Notifications
You must be signed in to change notification settings - Fork 31
/
RadioButton.js
127 lines (119 loc) · 3.33 KB
/
RadioButton.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import React, {useContext, createContext} from 'react';
import {
View,
StyleSheet,
TouchableNativeFeedback,
TouchableOpacity,
Platform,
} from 'react-native';
import PropTypes from 'prop-types';
import Ionicons from 'react-native-vector-icons/Ionicons';
import {useThemeContext} from '../util/ThemeProvider';
import {extractAccessibilityPropsFromProps} from '../util/accessibility';
import {sizes} from '../util/prop-types';
import {Text} from '../Text';
const Context = createContext();
const {Provider} = Context;
const getTextStyle = ({iconRight}) => {
const textStyle = [
{
marginLeft: 10,
},
];
if (iconRight) {
textStyle.push({
marginLeft: 0,
marginRight: 10,
});
}
return textStyle;
};
const renderIcon = ({theme, size, color, id, activeId, ...props}) => {
if (activeId === id) {
return (
props.checkedIcon || (
<Ionicons
name="ios-radio-button-on"
size={theme.fontSize[size] * 1.2}
color={theme.colors[color]}
/>
)
);
} else {
return (
props.uncheckedIcon || (
<Ionicons
name="ios-radio-button-off"
size={theme.fontSize[size] * 1.2}
color={theme.colors[color]}
/>
)
);
}
};
export const RadioItem = ({children, id, ...otherProps}) => {
const {selectItem, style, ...props} = useContext(Context);
const propsToPass = {...props, id};
const TouchableElement =
Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
return (
<TouchableElement
{...props}
{...extractAccessibilityPropsFromProps(otherProps)}
onPress={() => selectItem(id)}>
<View style={[styles.itemContainer, style]}>
{!props.iconRight && renderIcon(propsToPass)}
<View style={{flex: 1}}>
{typeof children === 'function' || typeof children === 'object' ? (
children
) : (
<Text
color={otherProps.textColor}
size={otherProps.size}
fontBase={props.fontBase}
fontVariant={props.fontVariant}
style={StyleSheet.flatten([
getTextStyle(propsToPass),
props.textStyle,
])}>
{children}
</Text>
)}
</View>
{props.iconRight && renderIcon(propsToPass)}
</View>
</TouchableElement>
);
};
const RadioButton = ({children, ...props}) => {
const theme = useThemeContext();
return <Provider value={{...props, theme}}>{children}</Provider>;
};
RadioButton.propTypes = {
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
textStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
children: PropTypes.oneOfType([PropTypes.array, PropTypes.element])
.isRequired,
activeId: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
.isRequired,
iconRight: PropTypes.bool,
color: PropTypes.string,
textColor: PropTypes.string,
size: sizes,
selectItem: PropTypes.func.isRequired,
checkedIcon: PropTypes.element,
uncheckedIcon: PropTypes.element,
};
RadioButton.defaultProps = {
size: 'md',
color: 'primary',
textColor: 'para',
};
const styles = StyleSheet.create({
itemContainer: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
});
export default RadioButton;