import Icon from '@components/Icon' import { StyleConstants } from '@utils/styles/constants' import layoutAnimation from '@utils/styles/layoutAnimation' import { useTheme } from '@utils/styles/ThemeManager' import React, { useEffect, useMemo, useRef, useState } from 'react' import { AccessibilityProps, Pressable, StyleProp, View, ViewStyle } from 'react-native' import { Flow } from 'react-native-animated-spinkit' import CustomText from './Text' export interface Props { accessibilityLabel?: AccessibilityProps['accessibilityLabel'] accessibilityHint?: AccessibilityProps['accessibilityHint'] style?: StyleProp type: 'icon' | 'text' content: string selected?: boolean loading?: boolean destructive?: boolean disabled?: boolean strokeWidth?: number size?: 'S' | 'M' | 'L' spacing?: 'XS' | 'S' | 'M' | 'L' round?: boolean overlay?: boolean onPress: () => void } const Button: React.FC = ({ accessibilityLabel, accessibilityHint, style: customStyle, type, content, selected, loading = false, destructive = false, disabled = false, strokeWidth, size = 'M', spacing = 'S', round = false, overlay = false, onPress }) => { const { colors, theme } = useTheme() const mounted = useRef(false) useEffect(() => { if (mounted.current) { layoutAnimation() } else { mounted.current = true } }, [content, loading, disabled]) const loadingSpinkit = useMemo( () => ( ), [theme] ) const mainColor = useMemo(() => { if (selected) { return colors.blue } else if (overlay) { return colors.primaryOverlay } else if (disabled || loading) { return colors.disabled } else { if (destructive) { return colors.red } else { return colors.primaryDefault } } }, [theme, disabled, loading, selected]) const colorBackground = useMemo(() => { if (overlay) { return colors.backgroundOverlayInvert } else { return colors.backgroundDefault } }, [theme]) const children = useMemo(() => { switch (type) { case 'icon': return ( <> {loading ? loadingSpinkit : null} ) case 'text': return ( <> {loading ? loadingSpinkit : null} ) } }, [theme, content, loading, disabled]) const [layoutHeight, setLayoutHeight] = useState() return ( setLayoutHeight(nativeEvent.layout.height) })} testID='base' onPress={onPress} children={children} disabled={selected || disabled || loading} /> ) } export default Button