forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadioButtonAndroid.js
203 lines (186 loc) · 5.1 KB
/
RadioButtonAndroid.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
195
196
197
198
199
200
201
202
203
/* @flow */
import * as React from 'react';
import { Animated, View, StyleSheet } from 'react-native';
import color from 'color';
import { RadioButtonContext } from './RadioButtonGroup';
import TouchableRipple from './TouchableRipple';
import { withTheme } from '../core/theming';
import type { Theme, $RemoveChildren } from '../types';
type Props = $RemoveChildren<typeof TouchableRipple> & {|
/**
* Value of the radio button
*/
value: string,
/**
* Status of radio button.
*/
status?: 'checked' | 'unchecked',
/**
* Whether radio is disabled.
*/
disabled?: boolean,
/**
* Function to execute on press.
*/
onPress?: () => mixed,
/**
* Custom color for unchecked radio.
*/
uncheckedColor?: string,
/**
* Custom color for radio.
*/
color?: string,
/**
* @optional
*/
theme: Theme,
|};
type State = {
borderAnim: Animated.Value,
radioAnim: Animated.Value,
};
const BORDER_WIDTH = 2;
/**
* Radio buttons allow the selection a single option from a set.
* This component follows platform guidelines for Android.
*
* <div class="screenshots">
* <figure>
* <img src="screenshots/radio-enabled.android.png" />
* <figcaption>Enabled</figcaption>
* </figure>
* <figure>
* <img src="screenshots/radio-disabled.android.png" />
* <figcaption>Disabled</figcaption>
* </figure>
* </div>
*/
class RadioButtonAndroid extends React.Component<Props, State> {
static displayName = 'RadioButton.Android';
state = {
borderAnim: new Animated.Value(BORDER_WIDTH),
radioAnim: new Animated.Value(1),
};
componentDidUpdate(prevProps) {
if (prevProps.status === this.props.status) {
return;
}
if (this.props.status === 'checked') {
this.state.radioAnim.setValue(1.2);
Animated.timing(this.state.radioAnim, {
toValue: 1,
duration: 150,
}).start();
} else {
this.state.borderAnim.setValue(10);
Animated.timing(this.state.borderAnim, {
toValue: BORDER_WIDTH,
duration: 150,
}).start();
}
}
render() {
return (
<RadioButtonContext.Consumer>
{context => {
const { disabled, onPress, theme, ...rest } = this.props;
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, radioColor;
const checked = context
? context.value === this.props.value
: this.props.status === 'checked';
if (disabled) {
rippleColor = color(theme.colors.text)
.alpha(0.16)
.rgb()
.string();
radioColor = theme.colors.disabled;
} else {
rippleColor = color(checkedColor)
.fade(0.32)
.rgb()
.string();
radioColor = checked ? checkedColor : uncheckedColor;
}
return (
<TouchableRipple
{...rest}
borderless
rippleColor={rippleColor}
onPress={
disabled
? undefined
: () => {
context && context.onValueChange(this.props.value);
onPress && onPress();
}
}
accessibilityTraits={disabled ? ['button', 'disabled'] : 'button'}
accessibilityComponentType={
checked ? 'radiobutton_checked' : 'radiobutton_unchecked'
}
accessibilityRole="button"
accessibilityStates={disabled ? ['disabled'] : undefined}
accessibilityLiveRegion="polite"
style={styles.container}
>
<Animated.View
style={[
styles.radio,
{
borderColor: radioColor,
borderWidth: this.state.borderAnim,
},
]}
>
{checked ? (
<View
style={[StyleSheet.absoluteFill, styles.radioContainer]}
>
<Animated.View
style={[
styles.dot,
{
backgroundColor: radioColor,
transform: [{ scale: this.state.radioAnim }],
},
]}
/>
</View>
) : null}
</Animated.View>
</TouchableRipple>
);
}}
</RadioButtonContext.Consumer>
);
}
}
const styles = StyleSheet.create({
container: {
borderRadius: 18,
},
radioContainer: {
alignItems: 'center',
justifyContent: 'center',
},
radio: {
height: 20,
width: 20,
borderRadius: 10,
margin: 8,
},
dot: {
height: 10,
width: 10,
borderRadius: 5,
},
});
export default withTheme(RadioButtonAndroid);