import { View, TouchableOpacity, Text, StyleSheet, type ColorValue, } from 'react-native'; import type { BorderStyle, CellPadding, Column, SortingIcons, } from '../utils/types'; interface TableHeaderProps { columns: Column[]; sortConfig: { key: keyof T | null; direction: 'asc' | 'desc' }; cellPadding?: CellPadding; sortingIcons?: SortingIcons; borderStyle?: BorderStyle; headerBackground?: ColorValue; onSort: (key: keyof T) => void; } export const TableHeader = ({ columns, sortConfig, cellPadding, sortingIcons, borderStyle, headerBackground = 'white', onSort, }: TableHeaderProps) => { const seenKeys = new Set(); columns.forEach((column) => { const identifier = column.id ?? String(column.key); if (seenKeys.has(identifier)) { throw new Error( `Duplicate column identifier detected for key "${String(column.key)}". Ensure each column has a unique id or key.` ); } seenKeys.add(identifier); }); return ( {columns.map((column, index) => ( column.sortable && onSort(column.key)} style={[styles.row]} disabled={!column.sortable} > {column.header ? ( column.header(column.label) ) : ( {column.label} )} {column.sortable && sortConfig.key === column.key ? ( sortingIcons ? ( sortConfig.direction === 'asc' ? ( sortingIcons.asc ) : ( sortingIcons.desc ) ) : ( {sortConfig.direction === 'asc' ? '↑' : '↓'} ) ) : null} ))} ); }; const styles = StyleSheet.create({ row: { flexDirection: 'row' }, text: { fontWeight: 'bold' }, sorting: { marginLeft: 3 }, });