import {IDatatable} from "../interfaces/datatable.interface"; import {IHeader} from "../interfaces/header.interface"; import {IAction} from "../interfaces/action.interface"; import {FormCheck, OverlayTrigger, Tooltip} from "react-bootstrap"; import {useEffect, useState} from "react"; export const Datatable = ({ headers = [], actions = [], items = [], page = 1, limit = 15, className = '', rowClasses = '', rowStyles = {}, hasCheckbox = false, hasIndexNumbers = true, loading = false, loaderComponent =
Loading ...
, emptyStateComponent =
There is no data!
, onRowClick = (item: any) => { }, onItemsSelected = (items: any[]) => { }, ...props }: IDatatable) => { const [selectedItems, setSelectedItems] = useState([]); const [checked, setChecked] = useState(false); useEffect(() => { if (selectedItems.length === items.length) { setChecked(true); } else { setChecked(false); } onItemsSelected(selectedItems); }, [JSON.stringify(selectedItems)]); const selectAll = ($e: any) => { const checked = $e.target.checked; if (checked) { setSelectedItems(items); } else { setSelectedItems([]); } } const selectOne = ($e: any, item: any) => { const checked = $e.target.checked; let tmpSelectedItems = [...selectedItems]; if (checked) { tmpSelectedItems.push(item); } else { tmpSelectedItems = tmpSelectedItems.filter(selectedItem => JSON.stringify(selectedItem) !== JSON.stringify(item)); } setSelectedItems(tmpSelectedItems); } return <>{props.children ?
{props.children}
: !loading ? <> {items.length > 0 ? <>
{hasIndexNumbers && } {hasCheckbox && } {headers .filter(({hidden}: any) => !hidden) .map((header: IHeader, index: number) => )} {actions?.length > 0 && } {items.map((item: any, idx: number) => onRowClick(item)}> {hasIndexNumbers && } {hasCheckbox && } {headers.filter(({hidden}: any) => !hidden).map((header: IHeader, index: number) => )} {actions?.length > 0 && } )}
# {header.title}
{(page - 1) * limit + idx + 1} JSON.stringify(selectedItem) === JSON.stringify(item))} onChange={($e: any) => selectOne($e, item)} /> {item[header.key]} {actions.map((btn: IAction, index: number) => {btn.title}}> { $e.stopPropagation(); btn.action(item); }}> {btn.icon} )}
: <>{emptyStateComponent}} : <>{loaderComponent}} } export default Datatable;