forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckboxIOS.js
111 lines (100 loc) · 2.63 KB
/
CheckboxIOS.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
/* @flow */
import * as React from 'react';
import { StyleSheet, View } from 'react-native';
import color from 'color';
import Icon from './Icon';
import TouchableRipple from './TouchableRipple';
import { withTheme } from '../core/theming';
import type { 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?: () => mixed,
/**
* Custom color for checkbox.
*/
color?: string,
/**
* @optional
*/
theme: Theme,
|};
/**
* Checkboxes allow the selection of multiple options from a set.
* This component follows platform guidelines for iOS.
*
* <div class="screenshots">
* <figure>
* <img src="screenshots/checkbox-enabled.ios.png" />
* <figcaption>Enabled</figcaption>
* </figure>
* <figure>
* <img src="screenshots/checkbox-disabled.ios.png" />
* <figcaption>Disabled</figcaption>
* </figure>
* </div>
*/
class CheckboxIOS extends React.Component<Props> {
static displayName = 'Checkbox.IOS';
render() {
const { status, disabled, onPress, theme, ...rest } = this.props;
const checked = status === 'checked';
const indeterminate = status === 'indeterminate';
const checkedColor = disabled
? theme.colors.disabled
: this.props.color || theme.colors.accent;
let rippleColor;
if (disabled) {
rippleColor = color(theme.colors.text)
.alpha(0.16)
.rgb()
.string();
} else {
rippleColor = color(checkedColor)
.fade(0.32)
.rgb()
.string();
}
const icon = indeterminate ? 'remove' : 'done';
return (
<TouchableRipple
{...rest}
borderless
rippleColor={rippleColor}
onPress={onPress}
disabled={disabled}
accessibilityTraits={disabled ? ['button', 'disabled'] : 'button'}
accessibilityComponentType="button"
accessibilityRole="button"
accessibilityStates={disabled ? ['disabled'] : undefined}
accessibilityLiveRegion="polite"
style={styles.container}
>
<View style={{ opacity: indeterminate || checked ? 1 : 0 }}>
<Icon
allowFontScaling={false}
source={icon}
size={24}
color={checkedColor}
/>
</View>
</TouchableRipple>
);
}
}
const styles = StyleSheet.create({
container: {
borderRadius: 18,
padding: 6,
},
});
export default withTheme(CheckboxIOS);