import React from 'react'; import cn from 'classnames'; import { useTable, useExpanded } from 'react-table'; type FlightTableProps = { data: { date: string; type: string; aircraft: string; route: string; depArr: string; pilot: string; coPilot: string; }[]; columns: any; renderRowSubComponent: any; }; export const FlightTable: React.FC = ({ columns, data, renderRowSubComponent, }: FlightTableProps) => { const classes = cn('bfo-flight-table'); const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow, visibleColumns, } = useTable( { columns, data, }, useExpanded, // We can useExpanded to track the expanded state // for sub components too! ); return (
{headerGroups.length > 0 && ( {headerGroups.map(headerGroup => ( {headerGroup.headers.map(column => ( ))} ))} )} {rows.map(row => { prepareRow(row); return ( // Use a React.Fragment here so the table markup is still valid {row.cells.map(cell => { return ( ); })} {/* If the row is in an expanded state, render a row with a column that fills the entire length of the table. */} {row.isExpanded ? ( ) : null} ); })}
{column.render('Header')}
{cell.render('Cell')}
{/* Inside it, call our renderRowSubComponent function. In reality, you could pass whatever you want as props to a component like this, including the entire table instance. But for this example, we'll just pass the row */} {renderRowSubComponent({ row })}

); };