import { IconSymbol } from "@/components/ui/icon-symbol"; import { useTrip } from "@/state/use-trip"; import React, { useEffect, useRef, useState } from "react"; import { Animated, Text, TouchableOpacity, View } from "react-native"; import styles from "./style/FishingToolsTable.styles"; const FishingToolsTable: React.FC = () => { const [collapsed, setCollapsed] = useState(true); const [contentHeight, setContentHeight] = useState(0); const animatedHeight = useRef(new Animated.Value(0)).current; const { trip } = useTrip(); const data: Model.FishingGear[] = trip?.fishing_gears ?? []; const tongSoLuong = data.reduce((sum, item) => sum + Number(item.number), 0); // Reset animated height khi dữ liệu thay đổi useEffect(() => { setContentHeight(0); // Reset để tính lại chiều cao setCollapsed(true); // Reset về trạng thái gập lại }, [data]); const handleToggle = () => { const toValue = collapsed ? contentHeight : 0; Animated.timing(animatedHeight, { toValue, duration: 300, useNativeDriver: false, }).start(); setCollapsed((prev) => !prev); }; return ( {/* Header / Toggle */} Danh sách ngư cụ {collapsed && {tongSoLuong}} {/* Nội dung ẩn để đo chiều cao */} { const height = event.nativeEvent.layout.height; if (height > 0 && contentHeight === 0) { setContentHeight(height); } }} > {/* Table Header */} Tên Số lượng {/* Body */} {data.map((item, index) => ( {item.name} {item.number} ))} {/* Footer */} Tổng cộng {tongSoLuong} {/* Nội dung mở/đóng */} {/* Table Header */} Tên Số lượng {/* Body */} {data.map((item, index) => ( {item.name} {item.number} ))} {/* Footer */} Tổng cộng {tongSoLuong} ); }; export default FishingToolsTable;