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; /** * Specifies the largest possible scale a label font can reach. */ labelMaxFontSizeMultiplier?: number; /** * Style that is passed to Label element. */ labelStyle?: StyleProp; /** * @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 `` or `` should be used. * Left undefined `` 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 = () => ( * * * * ); * * 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 = ; } else if (mode === 'ios') { checkbox = ; } else { checkbox = ; } 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 ( {isLeading && checkbox} {label} {!isLeading && checkbox} ); }; 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, }, });