forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadioButtonIOS.js
129 lines (119 loc) · 3.31 KB
/
RadioButtonIOS.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
/* @flow */
import * as React from 'react';
import { StyleSheet, View } from 'react-native';
import color from 'color';
import { RadioButtonContext } from './RadioButtonGroup';
import Icon from './Icon';
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 radio.
*/
color?: string,
/**
* @optional
*/
theme: Theme,
|};
/**
* Radio buttons allow the selection a single option from a set.
* This component follows platform guidelines for iOS.
*
* <div class="screenshots">
* <figure>
* <img src="screenshots/radio-enabled.ios.png" />
* <figcaption>Enabled</figcaption>
* </figure>
* <figure>
* <img src="screenshots/radio-disabled.ios.png" />
* <figcaption>Disabled</figcaption>
* </figure>
* </div>
*/
class RadioButtonIOS extends React.Component<Props> {
static displayName = 'RadioButton.IOS';
render() {
return (
<RadioButtonContext.Consumer>
{context => {
const { disabled, onPress, theme, ...rest } = this.props;
const checkedColor = disabled
? theme.colors.disabled
: this.props.color || theme.colors.accent;
let rippleColor;
const checked = context
? context.value === this.props.value
: this.props.status === 'checked';
if (disabled) {
rippleColor = color(theme.colors.text)
.alpha(0.16)
.rgb()
.string();
} else {
rippleColor = color(checkedColor)
.fade(0.32)
.rgb()
.string();
}
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}
>
<View style={{ opacity: checked ? 1 : 0 }}>
<Icon
allowFontScaling={false}
source="done"
size={24}
color={checkedColor}
/>
</View>
</TouchableRipple>
);
}}
</RadioButtonContext.Consumer>
);
}
}
const styles = StyleSheet.create({
container: {
borderRadius: 18,
padding: 6,
},
});
export default withTheme(RadioButtonIOS);