|
1 | | -import React from 'react'; |
| 1 | +import React from "react"; |
2 | 2 | import { |
3 | | - ColorValue, |
4 | | - TextInput, |
5 | | - TextInputProps, |
6 | | - TouchableOpacity, |
7 | | - View, |
8 | | - ViewStyle, |
9 | | -} from 'react-native'; |
10 | | -import { component, theme } from '../../constants/colors'; |
11 | | -import { Ionicons } from '@expo/vector-icons'; |
| 3 | + ColorValue, |
| 4 | + TextInput, |
| 5 | + TextInputProps, |
| 6 | + TouchableOpacity, |
| 7 | + View, |
| 8 | + ViewStyle, |
| 9 | +} from "react-native"; |
| 10 | +import { component, themeColor } from "../../constants/colors"; |
| 11 | +import { Ionicons } from "@expo/vector-icons"; |
| 12 | +import { useTheme } from "../../provider/ThemeProvider"; |
12 | 13 |
|
13 | 14 | interface Props { |
14 | | - value: boolean; |
15 | | - onValueChange?: (newValue: boolean) => void; |
16 | | - checkedColor?: ColorValue; |
17 | | - uncheckedColor?: ColorValue; |
18 | | - inverseColor?: ColorValue; |
19 | | - disabled?: boolean; |
20 | | - style?: ViewStyle; |
21 | | - size?: number; |
| 15 | + value: boolean; |
| 16 | + onValueChange?: (newValue: boolean) => void; |
| 17 | + checkedColor?: ColorValue; |
| 18 | + uncheckedColor?: ColorValue; |
| 19 | + inverseColor?: ColorValue; |
| 20 | + disabled?: boolean; |
| 21 | + style?: ViewStyle; |
| 22 | + size?: number; |
22 | 23 | } |
23 | 24 |
|
24 | 25 | const Checkbox: React.FC<Props> = ({ |
25 | | - value = false, |
26 | | - onValueChange, |
27 | | - checkedColor = component.checkBox.checkedColor, |
28 | | - uncheckedColor = component.checkBox.uncheckedColor, |
29 | | - inverseColor = theme.white, |
30 | | - disabled = false, |
31 | | - size = 24, |
32 | | - style, |
| 26 | + value = false, |
| 27 | + onValueChange, |
| 28 | + checkedColor, |
| 29 | + uncheckedColor, |
| 30 | + inverseColor = themeColor.white, |
| 31 | + disabled = false, |
| 32 | + size = 24, |
| 33 | + style, |
33 | 34 | }) => { |
34 | | - const handleChange = () => { |
35 | | - if (onValueChange) { |
36 | | - onValueChange(!value); |
37 | | - } |
38 | | - }; |
| 35 | + const { theme } = useTheme(); |
| 36 | + const selectedCheckedColor = checkedColor |
| 37 | + ? checkedColor |
| 38 | + : component[theme].checkBox.checkedColor; |
| 39 | + const selectedUncheckedColor = uncheckedColor |
| 40 | + ? uncheckedColor |
| 41 | + : component[theme].checkBox.uncheckedColor; |
39 | 42 |
|
40 | | - return ( |
41 | | - <TouchableOpacity |
42 | | - onPress={handleChange} |
43 | | - style={{ |
44 | | - ...style, |
45 | | - width: size, |
46 | | - height: size, |
47 | | - alignItems: 'center', |
48 | | - justifyContent: 'center', |
49 | | - borderRadius: style?.borderRadius || 4, |
50 | | - borderColor: value ? checkedColor : uncheckedColor, |
51 | | - backgroundColor: disabled |
52 | | - ? component.checkBox.disabledColor |
53 | | - : value |
54 | | - ? checkedColor |
55 | | - : 'transparent', |
56 | | - borderWidth: 1, |
57 | | - }} |
58 | | - disabled={disabled} |
59 | | - > |
60 | | - <Ionicons |
61 | | - name="md-checkmark" |
62 | | - size={size - 2} |
63 | | - color={disabled ? inverseColor : value ? inverseColor : uncheckedColor} |
64 | | - /> |
65 | | - </TouchableOpacity> |
66 | | - ); |
| 43 | + const handleChange = () => { |
| 44 | + if (onValueChange) { |
| 45 | + onValueChange(!value); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + return ( |
| 50 | + <TouchableOpacity |
| 51 | + onPress={handleChange} |
| 52 | + style={{ |
| 53 | + ...style, |
| 54 | + width: size, |
| 55 | + height: size, |
| 56 | + alignItems: "center", |
| 57 | + justifyContent: "center", |
| 58 | + borderRadius: style?.borderRadius || 4, |
| 59 | + borderColor: value ? selectedCheckedColor : selectedUncheckedColor, |
| 60 | + backgroundColor: disabled |
| 61 | + ? component[theme].checkBox.disabledColor |
| 62 | + : value |
| 63 | + ? selectedCheckedColor |
| 64 | + : "transparent", |
| 65 | + borderWidth: 1, |
| 66 | + }} |
| 67 | + disabled={disabled} |
| 68 | + > |
| 69 | + <Ionicons |
| 70 | + name="md-checkmark" |
| 71 | + size={size - 2} |
| 72 | + color={ |
| 73 | + disabled |
| 74 | + ? inverseColor |
| 75 | + : value |
| 76 | + ? inverseColor |
| 77 | + : selectedUncheckedColor |
| 78 | + } |
| 79 | + /> |
| 80 | + </TouchableOpacity> |
| 81 | + ); |
67 | 82 | }; |
68 | 83 |
|
69 | 84 | export default Checkbox; |
0 commit comments