import { forwardRef, memo, useCallback, useMemo } from 'react'; import { Pressable, type ViewStyle } from 'react-native'; import { Icon, type IconProps, type IconType } from '../Icon'; import { Tooltip, type TooltipProps } from '../Tooltip'; import { resolveStateVariant } from '../../utils'; import { ratingItemStyles } from './utils'; export type Props = Omit & { index: number; value: number; onChange: (val: number) => void; activeIconName: string; activeIconType: IconType; disabled?: boolean; readonly?: boolean; iconContainerStyle?: ViewStyle; activeColor?: string; color?: string; showTooltips?: boolean; tooltipProps?: Omit; }; const RatingItem = ( { style, disabled = false, readonly = false, iconContainerStyle, value, index, onChange, name, type, activeIconName, activeIconType, activeColor = 'rgb(250, 175, 0)', color, showTooltips = true, tooltipProps = {}, ...rest }: Props, ref: any, ) => { const active = value >= index; // const componentStyles = useComponentStyles( // 'Rating_Item', // [active ? { color: activeColor } : color ? { color } : {}, style], // { // state: resolveStateVariant({ // activeAndDisabled: active && disabled, // activeAndReadonly: active && readonly, // active, // disabled, // readonly, // }), // }, // ); const state = resolveStateVariant({ activeAndDisabled: active && disabled, activeAndReadonly: active && readonly, active, disabled, readonly, }); ratingItemStyles.useVariants({ state: state as any, }); const componentStyles = useMemo( () => [ ratingItemStyles.root, active ? { color: activeColor } : color ? { color } : {}, style, ], // eslint-disable-next-line react-hooks/exhaustive-deps [active, activeColor, color, style, state], ); const onPress = useCallback(() => { onChange(value === index ? 0 : index); }, [index, onChange, value]); const IconComponent = useMemo( () => ( = index ? activeIconName : name} type={value >= index ? activeIconType : type} /> ), [ activeIconName, activeIconType, componentStyles, disabled, iconContainerStyle, index, name, onPress, readonly, ref, rest, type, value, ], ); return showTooltips ? ( {IconComponent} {index} ) : ( IconComponent ); }; export default memo(forwardRef(RatingItem));