/* eslint-disable react/jsx-key */ /* FIXME fix datable keys and remo eslint-disable */ import React, { PropsWithoutRef, RefAttributes } from 'react' import useTheme from '../use-theme' import GlobalFilter from './global-filter' import SorterIcon from './sorter-icon' import Pagination from '../pagination' import { useTable, useSortBy, useGlobalFilter, usePagination, useRowSelect, SortingRule } from 'react-table' interface Props { className?: string columns: any selectable: boolean sortable?: boolean sortBy?: Array> searchBar?: boolean pagination?: boolean pageSize?: number searchPadding?: number paginationPadding?: number selectedRowIds?: Record data: Array> onClickRow?: (item: any) => void multiSelect?: boolean manualRowSelectedKey?: string } const defaultProps = { className: '', manualRowSelectedKey: 'isSelected', selectable: false, multiSelect: true, sortable: false, sortBy: [] as Array>, searchBar: false, pagination: false, pageSize: 50, searchPadding: 1, paginationPadding: 1.5, selectedRowIds: {} } type NativeAttrs = Omit, keyof Props> export type DataTableProps = Props & typeof defaultProps & NativeAttrs const DataTable: React.FC = ({ className, columns, data, manualRowSelectedKey, multiSelect, selectable, selectedRowIds, sortable, sortBy, searchBar, pagination, pageSize, searchPadding, paginationPadding, onClickRow }) => { const hooks: any[] = [] if (searchBar) { hooks.push(useGlobalFilter) } if (sortable) { hooks.push(useSortBy) } if (pagination) { hooks.push(usePagination) } if (selectable) { hooks.push(useRowSelect) } const { getTableProps, getTableBodyProps, headerGroups, page, rows, prepareRow, state, pageCount, toggleAllRowsSelected, gotoPage, ...tableProps } = useTable( { columns, data, disableSortBy: !sortable, initialState: { pageSize, selectedRowIds, sortBy }, autoResetSelectedRows: false, manualRowSelectedKey } as any, ...hooks ) as any // FIXME: once new react-datatable types package has been releaesed let setGlobalFilter if (searchBar) { setGlobalFilter = tableProps.setGlobalFilter } const theme = useTheme() const tableRows = pagination ? page : rows return (
{searchBar && setGlobalFilter && (
)} {headerGroups.map((headerGroup: any) => ( {headerGroup.headers.map((column: any) => ( ))} ))} {tableRows.map((row: any) => { prepareRow(row) let className = '' if (onClickRow || selectable) { className = row.isSelected || selectedRowIds[row.original[manualRowSelectedKey]] ? 'selected' : '' } return ( { onClickRow(row) if (selectable) { if (!multiSelect) { toggleAllRowsSelected(false) } row.toggleRowSelected() } }) } > {row.cells.map((cell: any) => { return ( ) })} ) })}
{column.render('Header')} {sortable && !column.disableSortBy && ( )}
{cell.render('Cell')}
{pagination && (
gotoPage(i - 1)} count={pageCount} />
)}
) } type DataTableComponent = React.ForwardRefExoticComponent< PropsWithoutRef

& RefAttributes > type ComponentProps = Partial & Omit & NativeAttrs DataTable.defaultProps = defaultProps export default React.memo(DataTable) as DataTableComponent