-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathCheckboxItem.tsx
236 lines (224 loc) · 5.51 KB
/
CheckboxItem.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import * as React from 'react';
import {
ColorValue,
GestureResponderEvent,
PressableAndroidRippleConfig,
StyleProp,
StyleSheet,
TextStyle,
View,
ViewStyle,
} from 'react-native';
import Checkbox from './Checkbox';
import CheckboxAndroid from './CheckboxAndroid';
import CheckboxIOS from './CheckboxIOS';
import { useInternalTheme } from '../../core/theming';
import type { ThemeProp, MD3TypescaleKey } from '../../types';
import TouchableRipple from '../TouchableRipple/TouchableRipple';
import Text from '../Typography/Text';
export type Props = {
/**
* Status of checkbox.
*/
status: 'checked' | 'unchecked' | 'indeterminate';
/**
* Whether checkbox is disabled.
*/
disabled?: boolean;
/**
* Label to be displayed on the item.
*/
label: string;
/**
* Function to execute on press.
*/
onPress?: (e: GestureResponderEvent) => void;
/**
* Function to execute on long press.
*/
onLongPress?: (e: GestureResponderEvent) => void;
/**
* Type of background drawabale to display the feedback (Android).
* https://reactnative.dev/docs/pressable#rippleconfig
*/
background?: PressableAndroidRippleConfig;
/**
* Accessibility label for the touchable. This is read by the screen reader when the user taps the touchable.
*/
accessibilityLabel?: string;
/**
* Custom color for unchecked checkbox.
*/
uncheckedColor?: string;
/**
* Custom color for checkbox.
*/
color?: string;
/**
* Color of the ripple effect.
*/
rippleColor?: ColorValue;
/**
* Additional styles for container View.
*/
style?: StyleProp<ViewStyle>;
/**
* Specifies the largest possible scale a label font can reach.
*/
labelMaxFontSizeMultiplier?: number;
/**
* Style that is passed to Label element.
*/
labelStyle?: StyleProp<TextStyle>;
/**
* @supported Available in v5.x with theme version 3
*
* Label text variant defines appropriate text styles for type role and its size.
* Available variants:
*
* Display: `displayLarge`, `displayMedium`, `displaySmall`
*
* Headline: `headlineLarge`, `headlineMedium`, `headlineSmall`
*
* Title: `titleLarge`, `titleMedium`, `titleSmall`
*
* Label: `labelLarge`, `labelMedium`, `labelSmall`
*
* Body: `bodyLarge`, `bodyMedium`, `bodySmall`
*/
labelVariant?: keyof typeof MD3TypescaleKey;
/**
* @optional
*/
theme?: ThemeProp;
/**
* testID to be used on tests.
*/
testID?: string;
/**
* Checkbox control position.
*/
position?: 'leading' | 'trailing';
/**
* Whether `<Checkbox.Android />` or `<Checkbox.IOS />` should be used.
* Left undefined `<Checkbox />` will be used.
*/
mode?: 'android' | 'ios';
};
/**
* Checkbox.Item allows you to press the whole row (item) instead of only the Checkbox.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { View } from 'react-native';
* import { Checkbox } from 'react-native-paper';
*
* const MyComponent = () => (
* <View>
* <Checkbox.Item label="Item" status="checked" />
* </View>
* );
*
* export default MyComponent;
*```
*/
const CheckboxItem = ({
style,
status,
label,
onPress,
onLongPress,
labelStyle,
theme: themeOverrides,
testID,
mode,
position = 'trailing',
accessibilityLabel = label,
disabled,
labelVariant = 'bodyLarge',
labelMaxFontSizeMultiplier = 1.5,
rippleColor,
background,
...props
}: Props) => {
const theme = useInternalTheme(themeOverrides);
const checkboxProps = { ...props, status, theme, disabled };
const isLeading = position === 'leading';
let checkbox;
if (mode === 'android') {
checkbox = <CheckboxAndroid {...checkboxProps} />;
} else if (mode === 'ios') {
checkbox = <CheckboxIOS {...checkboxProps} />;
} else {
checkbox = <Checkbox {...checkboxProps} />;
}
const textColor = theme.isV3 ? theme.colors.onSurface : theme.colors.text;
const disabledTextColor = theme.isV3
? theme.colors.onSurfaceDisabled
: theme.colors.disabled;
const textAlign = isLeading ? 'right' : 'left';
const computedStyle = {
color: disabled ? disabledTextColor : textColor,
textAlign,
} as TextStyle;
return (
<TouchableRipple
accessibilityLabel={accessibilityLabel}
accessibilityRole="checkbox"
accessibilityState={{
checked: status === 'checked',
disabled,
}}
onPress={onPress}
onLongPress={onLongPress}
testID={testID}
disabled={disabled}
rippleColor={rippleColor}
theme={theme}
background={background}
>
<View
style={[styles.container, style]}
pointerEvents="none"
importantForAccessibility="no-hide-descendants"
>
{isLeading && checkbox}
<Text
variant={labelVariant}
testID={`${testID}-text`}
maxFontSizeMultiplier={labelMaxFontSizeMultiplier}
style={[
styles.label,
!theme.isV3 && styles.font,
computedStyle,
labelStyle,
]}
>
{label}
</Text>
{!isLeading && checkbox}
</View>
</TouchableRipple>
);
};
CheckboxItem.displayName = 'Checkbox.Item';
export default CheckboxItem;
// @component-docs ignore-next-line
export { CheckboxItem };
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingVertical: 8,
paddingHorizontal: 16,
},
label: {
flexShrink: 1,
flexGrow: 1,
},
font: {
fontSize: 16,
},
});