-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathRadioButtonItem.tsx
More file actions
276 lines (265 loc) · 6.8 KB
/
RadioButtonItem.tsx
File metadata and controls
276 lines (265 loc) · 6.8 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import * as React from 'react';
import {
ColorValue,
GestureResponderEvent,
PressableAndroidRippleConfig,
StyleProp,
StyleSheet,
TextStyle,
View,
ViewStyle,
} from 'react-native';
import RadioButton from './RadioButton';
import RadioButtonAndroid from './RadioButtonAndroid';
import { RadioButtonContext, RadioButtonContextType } from './RadioButtonGroup';
import RadioButtonIOS from './RadioButtonIOS';
import { handlePress, isChecked } from './utils';
import { useInternalTheme } from '../../core/theming';
import type { ThemeProp, MD3TypescaleKey } from '../../types';
import TouchableRipple, {
Props as TouchableRippleProps,
} from '../TouchableRipple/TouchableRipple';
import Text from '../Typography/Text';
export type Props = {
/**
* Value of the radio button.
*/
value: string;
/**
* Label to be displayed on the item.
*/
label: string;
/**
* Whether radio is disabled.
*/
disabled?: boolean;
/**
* Type of background drawabale to display the feedback (Android).
* https://reactnative.dev/docs/pressable#rippleconfig
*/
background?: PressableAndroidRippleConfig;
/**
* Function to execute on press.
*/
onPress?: (e: GestureResponderEvent) => void;
/**
* Function to execute on long press.
*/
onLongPress?: (e: GestureResponderEvent) => void;
/**
* Accessibility label for the touchable. This is read by the screen reader when the user taps the touchable.
*/
accessibilityLabel?: string;
/**
* Custom color for unchecked radio.
*/
uncheckedColor?: string;
/**
* Custom color for radio.
*/
color?: string;
/**
* Color of the ripple effect.
*/
rippleColor?: ColorValue;
/**
* Status of radio button.
*/
status?: 'checked' | 'unchecked';
/**
* Additional styles for container View.
*/
style?: StyleProp<ViewStyle>;
/**
* 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;
/**
* Specifies the largest possible scale a label font can reach.
*/
labelMaxFontSizeMultiplier?: number;
/**
* @optional
*/
theme?: ThemeProp;
/**
* testID to be used on tests.
*/
testID?: string;
/**
* Whether `<RadioButton.Android />` or `<RadioButton.IOS />` should be used.
* Left undefined `<RadioButton />` will be used.
*/
mode?: 'android' | 'ios';
/**
* Radio button control position.
*/
position?: 'leading' | 'trailing';
/**
* Sets additional distance outside of element in which a press can be detected.
*/
hitSlop?: TouchableRippleProps['hitSlop'];
};
/**
* RadioButton.Item allows you to press the whole row (item) instead of only the RadioButton.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { RadioButton } from 'react-native-paper';
*
* const MyComponent = () => {
* const [value, setValue] = React.useState('first');
*
* return (
* <RadioButton.Group onValueChange={value => setValue(value)} value={value}>
* <RadioButton.Item label="First item" value="first" />
* <RadioButton.Item label="Second item" value="second" />
* </RadioButton.Group>
* );
* };
*
* export default MyComponent;
*```
*/
const RadioButtonItem = ({
value,
label,
style,
labelStyle,
onPress,
onLongPress,
disabled,
color,
uncheckedColor,
rippleColor,
status,
theme: themeOverrides,
background,
accessibilityLabel = label,
testID,
mode,
position = 'trailing',
labelVariant = 'bodyLarge',
labelMaxFontSizeMultiplier,
hitSlop,
}: Props) => {
const theme = useInternalTheme(themeOverrides);
const radioButtonProps = {
value,
disabled,
status,
color,
theme,
uncheckedColor,
};
const isLeading = position === 'leading';
let radioButton: any;
if (mode === 'android') {
radioButton = <RadioButtonAndroid {...radioButtonProps} aria-hidden />;
} else if (mode === 'ios') {
radioButton = <RadioButtonIOS {...radioButtonProps} />;
} else {
radioButton = <RadioButton {...radioButtonProps} />;
}
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 (
<RadioButtonContext.Consumer>
{(context?: RadioButtonContextType) => {
const checked =
isChecked({
contextValue: context?.value,
status,
value,
}) === 'checked';
return (
<TouchableRipple
onPress={(event) =>
handlePress({
onPress: onPress,
onValueChange: context?.onValueChange,
value,
event,
})
}
onLongPress={onLongPress}
accessibilityLabel={accessibilityLabel}
accessibilityRole="radio"
accessibilityState={{
checked,
disabled,
}}
testID={testID}
disabled={disabled}
background={background}
theme={theme}
rippleColor={rippleColor}
hitSlop={hitSlop}
>
<View style={[styles.container, style]} pointerEvents="none">
{isLeading && radioButton}
<Text
variant={labelVariant}
style={[
styles.label,
!theme.isV3 && styles.font,
computedStyle,
labelStyle,
]}
maxFontSizeMultiplier={labelMaxFontSizeMultiplier}
>
{label}
</Text>
{!isLeading && radioButton}
</View>
</TouchableRipple>
);
}}
</RadioButtonContext.Consumer>
);
};
RadioButtonItem.displayName = 'RadioButton.Item';
export default RadioButtonItem;
// @component-docs ignore-next-line
export { RadioButtonItem };
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingVertical: 8,
paddingHorizontal: 16,
},
label: {
flexShrink: 1,
flexGrow: 1,
},
font: {
fontSize: 16,
},
});