import React, { FunctionComponent, useEffect, useState } from 'react'; import { View, Text, TouchableOpacity, Animated } from 'react-native'; import Icon from '../icon'; import { IComponent, ComponentDefaults } from '../utils/typings'; import { useConfig } from '../configprovider'; import collapseStyles from './styles'; import pt from '../utils/pt'; export interface CollapseItemProps extends IComponent { title: string; name: string; isOpen: boolean; icon: string; iconSize: string; iconColor: string; disabled: boolean; rotate: number; subTitle: string; titleIcon: string; titleIconColor: string; titleIconPosition: string; titleIconSize: string; childnull: boolean; children: any; onToggle: (isOpen: boolean, name: string) => void; } const defaultProps = { ...ComponentDefaults, title: '', name: '', isOpen: false, icon: '', iconSize: '', iconColor: '', disabled: false, rotate: 180, subTitle: '', titleIcon: '', titleIconColor: '', titleIconPosition: '', titleIconSize: '', childnull: true } as CollapseItemProps; export const CollapseItem: FunctionComponent< Partial > = (props) => { const { children, title, isOpen, onToggle, name, disabled, icon, rotate, subTitle, titleIcon, titleIconColor, titleIconPosition, titleIconSize, iconSize, iconColor, childnull, iconClassPrefix, iconFontClassName } = { ...defaultProps, ...props }; const { theme } = useConfig(); const styles = collapseStyles(theme); const [currHeight, setCurrHeight] = useState('auto'); // 设置content的高度 const [iconStyle, setIconStyle] = useState({ transform: [{ translateY: -pt(20) }] }); useEffect(() => { // 一开始content都有高度,在这里根据isOpen,改变其高度 setTimeout(() => { const newIconStyle = isOpen ? { transform: [{ translateY: -pt(20) }, { rotate: `${rotate}deg` }] } : { transform: [{ translateY: -pt(20) }] }; setIconStyle(newIconStyle); }, 10); }, [isOpen, rotate]); useEffect(() => { if (!isOpen) { setCurrHeight(0); } else { setCurrHeight('auto'); } }, [children, isOpen]); return ( <> { if (disabled) return; onToggle?.(isOpen, name); }} > {!!titleIcon && titleIconPosition === 'left' && ( )} {title} {!!titleIcon && titleIconPosition === 'right' && ( )} {subTitle} {childnull && ( {typeof children === 'string' ? {children} : children} )} ); }; CollapseItem.defaultProps = defaultProps; CollapseItem.displayName = 'NutCollapseItem';