TableFromObject.tsx 904 B

1234567891011121314151617181920212223242526272829303132333435
  1. import React from "react";
  2. import { Table } from "react-bootstrap";
  3. const TableFromObject = (props: {
  4. data: string | { [key: string]: string };
  5. }) => {
  6. const { data } = props;
  7. if (!data) return <span />;
  8. if (typeof data === "string") return <span>{data}</span>;
  9. const keys = Object.keys(data);
  10. return (
  11. <Table className="text-light">
  12. <tbody>
  13. {keys.map((key: string) =>
  14. typeof data[key] === "object" ? (
  15. <tr key={key}>
  16. <td colSpan={2}>
  17. <TableFromObject data={data[key]} />
  18. </td>
  19. </tr>
  20. ) : (
  21. <tr key={key}>
  22. <td className="text-left text-bold">
  23. <b>{key}</b>
  24. </td>
  25. <td className="text-right">{data[key]}</td>
  26. </tr>
  27. )
  28. )}
  29. </tbody>
  30. </Table>
  31. );
  32. };
  33. export default TableFromObject;