import React from 'react';
import {
TouchableOpacity,
Platform,
TouchableOpacityProps,
} from 'react-native';
import Icon from '../Icon';
import Box from '../Box';
import { useThemeProps } from '../../../hooks';
import type { IRadioProps } from './types';
import { mergeRefs } from './../../../utils';
import { useHover } from '@react-native-aria/interactions';
import { useRadio } from '@react-native-aria/radio';
import { VisuallyHidden } from '@react-aria/visually-hidden';
import { RadioContext } from './RadioGroup';
import { useFocusRing } from '@react-native-aria/focus';
const Radio = ({ icon, children, ...props }: IRadioProps, ref: any) => {
let contextState = React.useContext(RadioContext);
const {
activeColor,
borderColor,
size,
// isInvalid,
...newProps
} = useThemeProps('Radio', {
...contextState,
...props,
});
const inputRef = React.useRef(null);
let { inputProps } = useRadio(props, contextState.state, inputRef);
// only calling below function when icon exist.
const sizedIcon = () =>
//@ts-ignore
React.cloneElement(icon, {
size,
color:
//@ts-ignore
icon.props.color ?? isSelected
? inputProps.disabled
? borderColor
: activeColor
: borderColor,
});
let isSelected = contextState.state.selectedValue === props.value;
const _ref = React.useRef(null);
const { isHovered } = useHover({}, _ref);
const mergedRefs = mergeRefs([_ref, ref]);
const outlineColor =
isHovered && !inputProps.disabled
? activeColor
: isSelected
? inputProps.disabled
? borderColor
: activeColor
: borderColor;
let component = (
{icon && isSelected ? (
sizedIcon()
) : (
)}
{children}
);
const { focusProps, isFocusVisible } = useFocusRing();
return (
<>
{Platform.OS === 'web' ? (
{component}
) : (
{component}
)}
>
);
};
export default React.memo(React.forwardRef(Radio));