import React, { FC, useState } from "react"; import styled from "styled-components"; import { SortTypes } from "./types"; import Icon from "../Icon"; const StyledTable = styled.table.attrs(() => ({ className: "table content is-hoverable" }))``; const IconWithMarginLeft = styled(Icon)` margin-left: 0.25em; `; type SortableTableProps = { data: any[]; }; // @ts-ignore const Table: FC = ({ data, children }) => { const [tableData, setTableData] = useState(data); const [ascending, setAscending] = useState(false); const [lastSortBy, setlastSortBy] = useState(0); // @ts-ignore const sortFunctions = React.Children.map(children, child => // @ts-ignore child.props.createComparator ? child.props.createComparator(child.props) : undefined ); const mapDataToColumns = (row: any) => { return ( {React.Children.map(children, child => { // @ts-ignore return {React.cloneElement(child, { ...child.props, row })}; })} ); }; const sortDescending = (sortAscending: (a: any, b: any) => number) => { return (a: any, b: any) => { return sortAscending(a, b) * -1; }; }; const tableSort = (index: number) => { const sortableData = [...tableData]; let sortOrder = ascending; if (lastSortBy !== index) { setAscending(true); sortOrder = true; } const sortFunction = sortOrder ? sortFunctions[index] : sortDescending(sortFunctions[index]); sortableData.sort(sortFunction); setTableData(sortableData); setAscending(!sortOrder); setlastSortBy(index); }; return ( tableData.length > 0 && ( {React.Children.map(children, (child, index) => ( // @ts-ignore tableSort(index) : undefined} > {child.props.header} {child.props.createComparator && renderSortIcon(child.props.sortType, ascending)} ))} {tableData.map(mapDataToColumns)} ) ); }; const renderSortIcon = (contentType: string, ascending: boolean) => { if (contentType === SortTypes.Text) { return ; } else { return ; } }; export default Table;