Skip to content

Instantly share code, notes, and snippets.

@JamesGelok
Last active August 4, 2021 18:29
Show Gist options
  • Save JamesGelok/fbec675267112c18c64089147e3aefa2 to your computer and use it in GitHub Desktop.
Save JamesGelok/fbec675267112c18c64089147e3aefa2 to your computer and use it in GitHub Desktop.
intended for sandboxing
import React from "react";
// prettier-ignore
export interface BasicHtmlTableProps { head?: React.ReactNode[][]; body?: React.ReactNode[][]; foot?: React.ReactNode[][]; tStyle?: React.CSSProperties; p?(cell: React.ReactNode): Record<string, any>; }
// prettier-ignore
export const BasicHtmlTable = ({ head, body, foot, tStyle = { border: "1px solid black" }, p = (cell: any) => ({ key: Math.random(), children: cell, style: tStyle }) }: BasicHtmlTableProps) => {
const headJsx = head?.map((r) => <tr {...p(r.map((c, i) => <th {...p(c)} />))}/>); // prettier-ignore
const bodyJsx = body?.map((r) => <tr {...p(r.map((c, i) => <td {...p(c)} />))}/>); // prettier-ignore
const footJsx = foot?.map((r) => <tr {...p(r.map((c, i) => <td {...p(c)} />))}/>); // prettier-ignore
return (
<table style={tStyle}>
{head && <thead>{headJsx}</thead>}
{body && <tbody>{bodyJsx}</tbody>}
{foot && <tfoot>{footJsx}</tfoot>}
</table>
);
};
// <BasicHtmlTable
// head={[["Month", "Savings"]]}
// body={[["January", "$100"],["February", "$80"]]}
// foot={[["Sum", "$180"]]}
// />
export const row = (
oneKeyObj: Record<string, any>,
key = Object.keys(oneKeyObj).pop()
) => [key, oneKeyObj[key!]];
export const ObjectTable = ({ obj }: any) => (
<BasicHtmlTable
head={[["Key", "Value"]]}
body={Object.keys(obj).map((k) => row({ [k]: obj[k] }))}
/>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment