import { memo, useCallback } from "react"; import { Platform, StyleSheet, Text, TouchableOpacity, View } from "react-native"; import type { BuiltInCheckBoxViewProps, CheckboxValueType } from "../types/treeView.types"; import { Checkbox } from "@futurejj/react-native-checkbox"; // Intentionally narrow: only re-render when the checkbox value or label text changes. // Other props (callbacks, styles) are stable references from parent memoization. function arePropsEqual( prevProps: BuiltInCheckBoxViewProps, nextProps: BuiltInCheckBoxViewProps ) { return ( prevProps.value === nextProps.value && prevProps.text === nextProps.text ); } export const CheckboxView = memo(_CheckboxView, arePropsEqual); function _CheckboxView(props: BuiltInCheckBoxViewProps) { const { value, onValueChange, text, outermostParentViewStyle = defaultCheckboxViewStyles.mainView, checkboxParentViewStyle = defaultCheckboxViewStyles.checkboxView, textTouchableStyle, checkboxProps, textProps = { style: defaultCheckboxViewStyles.checkboxTextStyle, numberOfLines: 1, ellipsizeMode: "middle", }, } = props; const customCheckboxValToCheckboxValType = useCallback(( customCheckboxValueType: CheckboxValueType ) => { return customCheckboxValueType === "indeterminate" ? "indeterminate" : customCheckboxValueType ? "checked" : "unchecked"; }, []); /** * This function modifies the change in value when the previous state was indeterminate. * If the prior value is 'indeterminate', it will mark the CheckBox as checked upon click. * * @param newValue This represents the updated CheckBox value after it's clicked. */ const onValueChangeModifier = useCallback(() => { // If the previous state was 'indeterminate', set checked to true if (value === "indeterminate") onValueChange(true); else onValueChange(!value); }, [onValueChange, value]); return ( {text ? ( {text} ) : null} ); } export const defaultCheckboxViewStyles = StyleSheet.create({ mainView: { alignSelf: "center", alignItems: "center", flexDirection: "row", marginEnd: 10 }, checkboxView: { marginStart: 5, transform: [{ scale: 1.2 }] }, checkboxTextStyle: { color: "black", marginTop: /* istanbul ignore next -- Platform.OS is never "android" in jest */ Platform.OS === "android" ? 2 : undefined, }, });