import { ComponentProps, useState } from "react"; import { Pressable, View } from "react-native"; import ReactNativeHapticFeedback from "react-native-haptic-feedback"; import Animated from "react-native-reanimated"; import { useIOTheme } from "../../context"; import { IOSelectionListItemStyles, IOSelectionListItemVisualParams, IOSelectionTickVisualParams } from "../../core"; import { useListItemAnimation } from "../../hooks"; import { useIOFontDynamicScale } from "../../utils/accessibility"; import { AnimatedCheckbox } from "../checkbox/AnimatedCheckbox"; import { IOIcons, Icon } from "../icons"; import { HSpacer, VSpacer } from "../layout"; import { BodySmall, H6 } from "../typography"; type Props = { value: string; description?: string; icon?: IOIcons; selected?: boolean; onValueChange?: (newValue: boolean) => void; }; const DISABLED_OPACITY = 0.5; type ListItemCheckboxProps = Props & Pick< ComponentProps, "onPress" | "accessibilityLabel" | "accessibilityHint" | "disabled" >; /** * with the automatic state management that uses a {@link AnimatedCheckBox} * The toggleValue change when a `onPress` event is received and dispatch the `onValueChange`. * * @param props * @constructor */ export const ListItemCheckbox = ({ value, description, icon, selected, accessibilityLabel, accessibilityHint, disabled, onValueChange }: ListItemCheckboxProps) => { const { dynamicFontScale, spacingScaleMultiplier, hugeFontEnabled } = useIOFontDynamicScale(); const [toggleValue, setToggleValue] = useState(selected ?? false); const { onPressIn, onPressOut, scaleAnimatedStyle, backgroundAnimatedStyle } = useListItemAnimation(); // Theme const theme = useIOTheme(); // Accessibility // Comma = Small pause when announcing content const fallbackAccessibilityLabel = description ? `${value}, ${description}` : value; const toggleCheckbox = () => { ReactNativeHapticFeedback.trigger("impactLight"); setToggleValue(!toggleValue); if (onValueChange !== undefined) { onValueChange(!toggleValue); } }; return ( {icon && !hugeFontEnabled && ( )}
{value}
{description && ( {description} )}
); };