import React from 'react'; import { TouchableOpacity } from 'react-native'; import { Icon } from '../../primitives'; import { Center } from '../../composites'; import { usePropsConfig } from '../../../theme'; import { FormControlContext, IFormControlContext, } from '../../composites/FormControl'; import { CheckboxContext } from './CheckboxGroup'; import type { ICheckboxProps, ICheckboxContext } from './props'; const Checkbox = ({ children, ...props }: ICheckboxProps, ref: any) => { const formControlContext: IFormControlContext = React.useContext( FormControlContext ); const { groupValueChangeHandler, value: cValue = [], ...context }: ICheckboxContext = React.useContext(CheckboxContext); const { activeColor, borderColor, iconColor, onChange, isIndeterminate, isChecked: pIsChecked, defaultIsChecked, isDisabled, value: pValue, ariaLabel, icon, size, ...newProps } = usePropsConfig('Checkbox', { ...context, ...formControlContext, ...props, }); let isChecked = pIsChecked || cValue.includes(pValue); const [checkboxState, setCheckboxState] = React.useState( isChecked || defaultIsChecked ); const pressHandler = () => { isChecked = !checkboxState; setCheckboxState(isChecked); if (!isIndeterminate) { groupValueChangeHandler && groupValueChangeHandler(pValue, isChecked); onChange && onChange(pValue, isChecked); } }; const sizedIcon = () => React.cloneElement( icon, { size, color: icon.props.color ? icon.props.color : iconColor, }, icon.props.children ); return ( pressHandler()} accessible accessibilityLabel={ariaLabel} accessibilityRole="checkbox" ref={ref} >
{icon && checkboxState ? ( sizedIcon() ) : ( )}
{children}
); }; export default React.forwardRef(Checkbox);