////////////////////////////////////////////////////////////// // Copyright (c) 2017 Ben Jackman // All Rights Reserved // please contact ben@jackman.biz // for licensing inquiries // Created by bjackman @ 6/8/17 3:00 PM ////////////////////////////////////////////////////////////// import * as React from "react"; import * as _ from "lodash"; function tablify(data: any[]): { keys: string[], rows: any[][] } | null { const keys: string[] = [] const rows: string[][] = [] try { //Keys is first pass data.forEach(dr => { if (_.isObject(dr)) { const ks = _.keys(dr) keys.push(...ks.filter(k => !_.includes(keys, k))) } else { throw new Error("Bad Tacos") } }) //Now just use the keys to build out the table data.forEach(dr => { if (_.isObject(dr)) { rows.push(keys.map(k => (dr as any)[k])) } else { throw new Error("Bad Tacos") } }) return {keys, rows} } catch (e) { return null } } interface Props { data : T[] } export class AutoTable extends React.Component, {}> { shouldComponentUpdate(nextProps: Readonly>, nextState: Readonly<{}>, nextContext: any): boolean { //Should Component Update Called const ret = nextProps.data !== this.props.data // console.log("shouldComponentUpdate called for AutoTable", ret) return ret; } tableEl = React.createRef() componentDidMount(): void { this.tableEl.current!.border = "1" } render(): JSX.Element { const punt = (r: string = "") => { console.error("Punting to json Expected an array of objects " + r, this.props.data) return