import { ComponentProps, memo, useMemo } from "react"; import { GestureResponderEvent, Platform, Switch, View } from "react-native"; import { useIOTheme } from "../../context"; import { IOSelectionListItemStyles, IOSelectionListItemVisualParams } from "../../core"; import { useIOFontDynamicScale } from "../../utils/accessibility"; import { WithTestID } from "../../utils/types"; import { Badge } from "../badge"; import { IOIcons, Icon } from "../icons"; import { HSpacer, VSpacer } from "../layout"; import { LoadingSpinner } from "../loadingSpinner"; import { IOLogoPaymentType, LogoPayment } from "../logos"; import { NativeSwitch } from "../switch/NativeSwitch"; import { BodySmall, H6, LabelMini } from "../typography"; type PartialProps = WithTestID<{ label: string; onSwitchValueChange?: (newValue: boolean) => void; description?: string; action?: SwitchAction; isLoading?: boolean; badge?: Badge; switchTestID?: string; }>; export type SwitchAction = { label: string; onPress: (event: GestureResponderEvent) => void; }; export type ListItemSwitchGraphicProps = | { icon?: never; paymentLogo: IOLogoPaymentType } | { icon: IOIcons; paymentLogo?: never } | { icon?: never; paymentLogo?: never }; const DISABLED_OPACITY = 0.5; /* Estimated height of the native switch component, both on iOS & Android */ const ESTIMATED_SWITCH_HEIGHT: number = 32; export type ListItemSwitchProps = PartialProps & ListItemSwitchGraphicProps & Pick, "value" | "disabled">; export const ListItemSwitch = memo( ({ label, description, icon, paymentLogo, value = false, disabled, action, isLoading, badge, onSwitchValueChange, switchTestID, testID }: ListItemSwitchProps) => { const theme = useIOTheme(); const { dynamicFontScale, spacingScaleMultiplier, hugeFontEnabled } = useIOFontDynamicScale(); // If we have a badge or we are loading, we can't render the switch // this affects the accessibility tree and the rendering of the component const canRenderSwitch = useMemo( () => !isLoading && !badge, [isLoading, badge] ); return ( {icon && !hugeFontEnabled && ( )} {paymentLogo && ( )}
{label}
{badge && ( )} {isLoading && } {canRenderSwitch && ( )}
{description && ( <> {description} )} {action && ( <> {action.label} )}
); } );