import { Key, SetStateAction, useState } from 'react'; import { CSV_Data } from '../types'; const DataTable = ( props: { 'data': CSV_Data } ) => { if ( props.data.length == 0 ) return <>; const header = Object.keys( props.data[0]! ); const [ sortCol, setSortCol ] = useState( 'None' ); const [ sortType, setSortType ] = useState( 'asc' ); const sortingHandler = ( col: string ) => { if ( sortCol !== col ) { setSortCol( col as SetStateAction ); setSortType( 'asc' ); } else if ( sortType === 'asc' ) { setSortType( 'desc' ); } else { setSortCol( 'None' ); setSortType( 'None' ); } }; if ( sortCol !== 'None' && sortType === 'asc' ) { props.data.sort( ( a, b ) => { if ( a[sortCol]! < b[sortCol]! ) return -1; if ( a[sortCol]! > b[sortCol]! ) return 1; return 0; } ); } else if ( sortCol !== 'None' && sortType === 'desc' ) { props.data.sort( ( a, b ) => { if ( a[sortCol]! > b[sortCol]! ) return -1; if ( a[sortCol]! < b[sortCol]! ) return 1; return 0; } ); } return (

Data table

{ header.map( col => ) } { props.data.map( ( row, i ) => ( { header.map( col => ) } ) ) }
); }; const ColHeader = ( props: { 'col': string, 'sortingHandle': ( s: string ) => void, 'isSelected': boolean, 'sortType': string } ) => { return ( { props.sortingHandle!( props.col ); }} key={props.col as Key}> {props.col} ); }; const Row = ( props: { 'col': string, 'content': string, 'key': Key } ) => { return {props.content}; }; export default DataTable;