import { IconButton, TableHead } from "@mui/material"; import { styled } from "@mui/system"; import React, { useState } from "react"; import { FaMinus, FaPlus } from "react-icons/fa"; import { TableComponent } from "./"; import { StyledTableCell, StyledTableRow } from "./common"; type AnyObject = { [key: string]: any }; function isObject(value: any): value is AnyObject { return value && typeof value === 'object' && !Array.isArray(value) && !(value instanceof Date); } function mergeArrays(...arrays: T[][]): T[] { const combinedArray = arrays.reduce((acc, curr) => acc.concat(curr), []); const uniqueArray = Array.from(new Set(combinedArray)); return uniqueArray; } export const ObjInTableShowHideWrapperComponent = (props: ObjInTableComponentProps) => { const [show, setShow] = useState(false) return
setShow(!show)}>{show ? : } {show ? : <>}
} export interface ObjInTableComponentProps extends React.DetailedHTMLProps, HTMLTableElement> { obj: any, quoteStr?: boolean, } export const TableObjWrapperComponent = (props: ObjInTableComponentProps) => { const { obj, quoteStr } = props let objIsObj = isObject(obj) let objIsArray = Array.isArray(obj) let data: any[] = objIsObj ? [obj] : objIsArray ? [...obj.map((o: any) => (isObject(o) ? o : { value: o }))] : [{ value: obj }] const getColumns = (): any[] => { return mergeArrays(...data.map(d => Object.keys(d))) } const getColData = (objData: any, col: string, idx: number, quoteStr?: boolean) => { let lIsObject = isObject(objData[col]) let lIsArray = Array.isArray(objData[col]) return {lIsObject || lIsArray ? : typeof objData[col] === 'string' && !quoteStr ?
{objData[col]}
:
{JSON.stringify(objData[col])}
}
} return obj ? <> {getColumns().map((c, idx) => {c})} } bodyList={data} bodyRowBuilder={(data: any, idx: number) => { return {getColumns().map((col, idx) => getColData(data, col, idx, quoteStr))} }} /> : <>Nada a exibir } export const StyledTableTitleCell = styled(StyledTableCell)(() => ({ fontWeight: 'bold', whiteSpace: 'nowrap', }))