-
Notifications
You must be signed in to change notification settings - Fork 31
/
CheckBox.js
119 lines (112 loc) · 2.94 KB
/
CheckBox.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
import React from 'react';
import {
View,
TouchableOpacity,
TouchableNativeFeedback,
Platform,
StyleSheet,
} from 'react-native';
import PropTypes from 'prop-types';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
import {useThemeContext} from '../util/ThemeProvider';
import {fontBases, fontVariants, sizes} from '../util/prop-types';
import {Text} from '../Text';
const getTextStyle = ({theme, size, textColor, iconRight}) => {
const textStyle = [
{
marginLeft: theme.space.lg,
},
];
if (iconRight) {
textStyle.push({
marginLeft: 0,
marginRight: 5,
});
}
return textStyle;
};
const renderIcon = ({style, theme, size, color, ...props}) => {
if (props.checked) {
return (
props.checkedIcon || (
<MaterialIcons
name="check-box"
size={theme.fontSize[size] * 1.5}
color={theme.colors[color]}
/>
)
);
} else {
return (
props.uncheckedIcon || (
<MaterialIcons
name="check-box-outline-blank"
size={theme.fontSize[size] * 1.5}
color={theme.colors[color]}
/>
)
);
}
};
const CheckBox = React.forwardRef(({style, textStyle, ...props}, ref) => {
const theme = useThemeContext();
const propsWithTheme = {...props, theme};
const TouchableElement =
Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
return (
<TouchableElement
{...props}
ref={ref}
disabled={props.disabled}
onPress={props.onPress}>
<View style={StyleSheet.flatten([styles.container, style])}>
{!props.iconRight && renderIcon(propsWithTheme)}
{typeof props.children !== 'function' ? (
<Text
color={props.textColor}
fontBase={props.fontBase}
fontVariant={props.fontVariant}
style={StyleSheet.flatten([
getTextStyle(propsWithTheme),
textStyle,
])}>
{props.children}
</Text>
) : (
props.children
)}
{props.iconRight && renderIcon(propsWithTheme)}
</View>
</TouchableElement>
);
});
CheckBox.propTypes = {
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
textStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
children: PropTypes.oneOfType([PropTypes.string, PropTypes.element])
.isRequired,
checked: PropTypes.bool,
iconRight: PropTypes.bool,
color: PropTypes.string,
textColor: PropTypes.string,
size: sizes,
onPress: PropTypes.func.isRequired,
checkedIcon: PropTypes.element,
uncheckedIcon: PropTypes.element,
fontBase: fontBases,
fontVariant: fontVariants,
};
CheckBox.defaultProps = {
size: 'md',
color: 'primary',
textColor: 'para',
fontBase: 'body',
fontVariant: 'regular',
};
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
},
});
export default CheckBox;