import React from 'react'; import { scale } from 'react-native-size-matters'; import { StyleSheet, Animated, Easing } from 'react-native'; import { Text } from '../Text'; import { Ripple } from '../Ripple'; import { useCollapse } from './Context'; import useTheme from '../../context/theme/useTheme'; import type { CollapseInternalItemProps } from './types'; import { Title } from '../Title'; const Header: React.FC< Pick< CollapseInternalItemProps, | 'id' | 'title' | 'disabled' | 'subTitle' | 'styleHeader' | 'isFirstElement' | 'isLastElement' > & { onPress: () => void; } > = ({ id, title, disabled, subTitle, styleHeader = {}, onPress, isFirstElement, isLastElement }) => { const { openKeys, icon, borderColor, iconPosition, showArrow, headerColor, borderless } = useCollapse(); const { fontSizes } = useTheme(); const rotateAnimation = React.useRef( new Animated.Value(0) ).current; const spin = rotateAnimation.interpolate({ inputRange: [0, 1], outputRange: ['0deg', '90deg'] }); const collapsed = !openKeys.includes(id); // memos const internalOpenIcon = React.useMemo(() => { if (icon && showArrow) { return ( {React.cloneElement(icon as any, { color: disabled ? 'accents7' : 'text' })} ); } return null; }, [icon, iconPosition, showArrow, spin, disabled]); // effects React.useEffect(() => { // First set up animation Animated.timing(rotateAnimation, { toValue: collapsed ? 0 : 1, duration: 150, easing: Easing.linear, // Easing is an additional import from react-native useNativeDriver: true // To make use of native driver for performance }).start(); }, [rotateAnimation, collapsed]); return ( <> {iconPosition === 'left' ? internalOpenIcon : null} {typeof title === 'string' || typeof title === 'number' ? ( {title} ) : ( React.cloneElement(title as any, { color: disabled ? 'accents7' : 'text' }) )} {typeof subTitle === 'string' ? ( {subTitle} ) : subTitle ? ( React.cloneElement(subTitle as any, { color: disabled ? 'accents7' : 'accents5' }) ) : null} {iconPosition === 'right' ? internalOpenIcon : null} ); }; const styles = StyleSheet.create({ header: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }, headerContent: { flex: 1 }, iconLeft: { marginRight: scale(5) }, iconRight: { marginLeft: scale(5) } }); export default Header;