-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathMenuAddButton.js
167 lines (158 loc) · 4.12 KB
/
MenuAddButton.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
import PropTypes from 'prop-types';
import React from 'react';
import {
Platform,
StyleSheet,
Text,
TouchableNativeFeedback,
TouchableOpacity,
View,
} from 'react-native';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
import {radii, shadows, sizes} from '../util/prop-types';
import {useThemeContext, useThemeMode} from '../util/ThemeProvider';
import {removeBackgroundProp} from '../util/touchable';
const getContainerStyle = ({
theme,
size,
count,
disabled,
shadow,
radius,
background,
}) => {
const buttonStyle = [styles.container];
buttonStyle.push({
backgroundColor: theme.colors[background],
paddingVertical: theme.buttonSize.paddingVertical[size],
paddingHorizontal: theme.buttonSize.paddingHorizontal[size],
flexDirection: 'row',
justifyContent: 'center',
borderRadius: theme.radius[radius],
});
if (shadow) {
buttonStyle.push(theme.shadow[shadow]);
}
if (count < 1) {
buttonStyle.push({
backgroundColor: theme.colors[background],
justifyContent: 'center',
alignItems: 'center',
});
}
if (disabled) {
buttonStyle.push({
backgroundColor: theme.colors.disabled,
justifyContent: 'center',
alignItems: 'center',
});
}
return buttonStyle;
};
const getTextStyle = ({theme, size, disabled}) => {
const textStyle = [
{
fontSize: theme.fontSize[size],
paddingHorizontal: 10,
color: theme.colors.para,
},
];
if (disabled) {
textStyle.push({
color: theme.colors.disabledText,
});
}
return textStyle;
};
const MenuAddButton = ({style, textStyle, ...props}) => {
const theme = useThemeContext();
const {isDarkMode} = useThemeMode();
const TouchableElement =
Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
const updateProps = removeBackgroundProp(props);
if (props.count < 1) {
return (
<TouchableElement
{...updateProps}
disabled={props.disabled}
onPress={props.onIncrement}>
<View style={[getContainerStyle({...props, theme, isDarkMode}), style]}>
<Text
style={[getTextStyle({...props, theme, isDarkMode}), textStyle]}>
ADD
</Text>
</View>
</TouchableElement>
);
}
return (
<View style={[getContainerStyle({...props, theme}), style]}>
<TouchableElement {...props} onPress={props.onDecrement}>
<View style={styles.icon}>
{props.minusIcon || (
<MaterialIcons
name="remove"
color={props.iconColor || theme.colors.para}
size={theme.fontSize[props.size]}
/>
)}
</View>
</TouchableElement>
<View style={[styles.countView]}>
<Text style={[getTextStyle({...props, theme}), textStyle]}>
{props.count}
</Text>
</View>
<TouchableElement {...props} onPress={props.onIncrement}>
<View style={styles.icon}>
{props.plusIcon || (
<MaterialIcons
name="add"
color={props.iconColor || theme.colors.para}
size={theme.fontSize[props.size]}
/>
)}
</View>
</TouchableElement>
</View>
);
};
MenuAddButton.propTypes = {
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
textStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
count: PropTypes.number.isRequired,
onIncrement: PropTypes.func.isRequired,
onDecrement: PropTypes.func.isRequired,
plusIcon: PropTypes.element,
minusIcon: PropTypes.element,
iconColor: PropTypes.string,
disabled: PropTypes.bool,
size: sizes,
shadow: shadows,
radius: radii,
};
MenuAddButton.defaultProps = {
count: 0,
size: 'md',
shadow: 'none',
radius: 'sm',
background: 'bg200',
};
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
backgroundColor: '#f8f8f8',
},
icon: {
flex: 2,
justifyContent: 'center',
alignItems: 'center',
paddingHorizontal: 15,
},
countView: {
flex: 3,
justifyContent: 'center',
alignItems: 'center',
},
});
export default MenuAddButton;