forked from galio-org/galio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Checkbox.js
194 lines (178 loc) · 4.97 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* eslint-disable object-curly-newline */
import React from 'react';
import { View, TouchableOpacity, StyleSheet, Image } from 'react-native';
import PropTypes from 'prop-types';
// galio dependency
import Icon from './atomic/ions/Icon';
import Text from './atomic/ions/Text';
import GalioTheme, { withGalio } from './theme';
function Checkbox({
checkboxStyle,
color,
disabled,
flexDirection,
image,
imageStyle,
iconColor,
iconFamily,
iconName,
iconSize,
initialValue,
label,
labelStyle,
onChange,
style,
styles,
theme,
}) {
const [checked, setChecked] = React.useState(initialValue);
// adding the necessary margins depending on the flexDirection
function spaceAround(direction) {
switch (direction) {
case 'row-reverse':
return { marginRight: 10 };
case 'column':
return { marginTop: 10 };
case 'column-reverse':
return { marginBottom: 10 };
default:
return { marginLeft: 10 };
}
}
// rendering the image/text for the checkbox
function renderLabel() {
const labelStyles = [
styles.textStyles,
disabled && styles.disabledLabel,
labelStyle,
flexDirection && spaceAround(flexDirection),
];
const imageStyles = [styles.imgStyles, imageStyle, flexDirection && spaceAround(flexDirection)];
if (image && !label) {
return <Image style={imageStyles} source={{ uri: image }} />;
}
if (!image && label) return <Text style={labelStyles}>{label}</Text>;
// if (!label && !image) return null;
return null;
}
// adding the check icon
function renderChecked() {
if (checked) {
return <Icon name={iconName} family={iconFamily} color={iconColor} size={iconSize} />;
}
return null;
}
// onPress function that changes the component's state and callbacks the onChange prop
function _onPress() {
const current = !checked;
onChange(current);
setChecked(current);
}
const colorStyle = theme.COLORS[color.toUpperCase()]; // this sets the correct color for the theme file
const checkBoxContainerStyle = [styles.container, flexDirection && { flexDirection }, style];
const checkedInnerStyles = [
styles.checked,
color && { backgroundColor: colorStyle, borderColor: colorStyle },
color && !colorStyle && { backgroundColor: color, borderColor: color },
];
const checkBoxViewStyles = [
styles.checkBoxView,
styles.uncheckedBoxView,
color && { borderColor: colorStyle },
color && !colorStyle && { borderColor: color },
checked && checkedInnerStyles, // apply the ckecked styling
disabled && styles.disabled,
checkboxStyle,
];
return (
<TouchableOpacity
onPress={() => _onPress()}
style={checkBoxContainerStyle}
activeOpacity={0.8}
disabled={disabled}>
<View style={checkBoxViewStyles}>{renderChecked()}</View>
{renderLabel()}
</TouchableOpacity>
);
}
const styles = theme =>
StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
},
checkBoxView: {
width: theme.SIZES.CHECKBOX_WIDTH,
height: theme.SIZES.CHECKBOX_HEIGHT,
borderWidth: theme.SIZES.BORDER_WIDTH,
alignItems: 'center',
justifyContent: 'center',
borderRadius: theme.SIZES.BORDER_RADIUS,
},
uncheckedBoxView: {
backgroundColor: theme.COLORS.TRANSPARENT,
borderColor: theme.COLORS.GREY,
},
checked: {
backgroundColor: theme.COLORS.THEME,
borderColor: theme.COLORS.THEME,
},
disabled: {
borderColor: theme.COLORS.MUTED,
},
textStyles: {
color: theme.COLORS.BLACK,
},
disabledLabel: {
color: theme.COLORS.MUTED,
opacity: theme.SIZES.OPACITY,
},
imgStyles: {
width: 200,
height: 200,
},
});
Checkbox.defaultProps = {
checkboxStyle: null,
color: 'theme',
disabled: false,
flexDirection: 'row',
iconColor: '#fff',
iconName: 'check',
iconSize: 15,
iconFamily: 'FontAwesome',
image: null,
imageStyle: null,
initialValue: false,
label: null,
labelStyle: null,
onChange: () => {},
styles: {},
theme: GalioTheme,
};
Checkbox.propTypes = {
checkboxStyle: PropTypes.any,
color: PropTypes.oneOfType([
PropTypes.oneOf(['primary', 'theme', 'error', 'warning', 'success', 'transparent', 'info']),
PropTypes.string,
]),
disabled: PropTypes.bool,
flexDirection: PropTypes.oneOfType([
PropTypes.oneOf(['row', 'row-reverse', 'column', 'column-reverse']),
PropTypes.string,
]),
iconColor: PropTypes.string,
iconName: PropTypes.string,
iconSize: PropTypes.number,
iconFamily: PropTypes.string,
image: PropTypes.string,
imageStyle: PropTypes.any, // style the image
initialValue: PropTypes.bool,
label: PropTypes.string.isRequired,
labelStyle: PropTypes.any, // style the text
onChange: PropTypes.func,
styles: PropTypes.any, // style the whole View element,
theme: PropTypes.any,
};
export default withGalio(Checkbox, styles);