forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckboxAndroid.tsx
182 lines (165 loc) · 4.46 KB
/
CheckboxAndroid.tsx
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
import * as React from 'react';
import { Animated, View, StyleSheet } from 'react-native';
import color from 'color';
import Icon from './Icon';
import TouchableRipple from './TouchableRipple';
import { withTheme } from '../core/theming';
import { Theme, $RemoveChildren } from '../types';
type Props = $RemoveChildren<typeof TouchableRipple> & {
/**
* Status of checkbox.
*/
status: 'checked' | 'unchecked' | 'indeterminate';
/**
* Whether checkbox is disabled.
*/
disabled?: boolean;
/**
* Function to execute on press.
*/
onPress?: () => void;
/**
* Custom color for unchecked checkbox.
*/
uncheckedColor?: string;
/**
* Custom color for checkbox.
*/
color?: string;
/**
* @optional
*/
theme: Theme;
};
type State = {
scaleAnim: Animated.Value;
};
// From https://material.io/design/motion/speed.html#duration
const ANIMATION_DURATION = 100;
/**
* Checkboxes allow the selection of multiple options from a set.
* This component follows platform guidelines for Android.
*
* <div class="screenshots">
* <figure>
* <img src="screenshots/checkbox-enabled.android.png" />
* <figcaption>Enabled</figcaption>
* </figure>
* <figure>
* <img src="screenshots/checkbox-disabled.android.png" />
* <figcaption>Disabled</figcaption>
* </figure>
* </div>
*/
class CheckboxAndroid extends React.Component<Props, State> {
static displayName = 'Checkbox.Android';
state = {
scaleAnim: new Animated.Value(1),
};
componentDidUpdate(prevProps: Props) {
if (prevProps.status === this.props.status) {
return;
}
const checked = this.props.status === 'checked';
const { animation } = this.props.theme;
Animated.sequence([
Animated.timing(this.state.scaleAnim, {
toValue: 0.85,
duration: checked ? ANIMATION_DURATION * animation.scale : 0,
}),
Animated.timing(this.state.scaleAnim, {
toValue: 1,
duration: checked
? ANIMATION_DURATION * animation.scale
: ANIMATION_DURATION * animation.scale * 1.75,
}),
]).start();
}
render() {
const { status, disabled, onPress, theme, ...rest } = this.props;
const checked = status === 'checked';
const indeterminate = status === 'indeterminate';
const checkedColor = this.props.color || theme.colors.accent;
const uncheckedColor =
this.props.uncheckedColor ||
color(theme.colors.text)
.alpha(theme.dark ? 0.7 : 0.54)
.rgb()
.string();
let rippleColor, checkboxColor;
if (disabled) {
rippleColor = color(theme.colors.text)
.alpha(0.16)
.rgb()
.string();
checkboxColor = theme.colors.disabled;
} else {
rippleColor = color(checkedColor)
.fade(0.32)
.rgb()
.string();
checkboxColor = checked ? checkedColor : uncheckedColor;
}
const borderWidth = this.state.scaleAnim.interpolate({
inputRange: [0.8, 1],
outputRange: [7, 0],
});
const icon = indeterminate
? 'minus-box'
: checked
? 'checkbox-marked'
: 'checkbox-blank-outline';
return (
<TouchableRipple
{...rest}
borderless
rippleColor={rippleColor}
onPress={onPress}
disabled={disabled}
accessibilityTraits={disabled ? ['button', 'disabled'] : 'button'}
accessibilityComponentType="button"
accessibilityRole="button"
accessibilityStates={disabled ? ['disabled'] : []}
accessibilityLiveRegion="polite"
style={styles.container}
>
<Animated.View style={{ transform: [{ scale: this.state.scaleAnim }] }}>
<Icon
allowFontScaling={false}
source={icon}
size={24}
color={checkboxColor}
/>
<View style={[StyleSheet.absoluteFill, styles.fillContainer]}>
<Animated.View
style={[
styles.fill,
{ borderColor: checkboxColor },
{ borderWidth },
]}
/>
</View>
</Animated.View>
</TouchableRipple>
);
}
}
const styles = StyleSheet.create({
container: {
borderRadius: 18,
width: 36,
height: 36,
padding: 6,
},
fillContainer: {
alignItems: 'center',
justifyContent: 'center',
},
fill: {
height: 14,
width: 14,
},
});
export default withTheme(CheckboxAndroid);
// @component-docs ignore-next-line
export { CheckboxAndroid };