Table.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import _ from 'lodash';
  2. import React, { PureComponent } from 'react';
  3. import ReactTable from 'react-table';
  4. import TableModel from 'app/core/table_model';
  5. const EMPTY_TABLE = new TableModel();
  6. interface TableProps {
  7. data: TableModel;
  8. loading: boolean;
  9. onClickCell?: (columnKey: string, rowValue: string) => void;
  10. }
  11. function prepareRows(rows, columnNames) {
  12. return rows.map(cells => _.zipObject(columnNames, cells));
  13. }
  14. export default class Table extends PureComponent<TableProps> {
  15. getCellProps = (state, rowInfo, column) => {
  16. return {
  17. onClick: () => {
  18. const columnKey = column.Header;
  19. const rowValue = rowInfo.row[columnKey];
  20. this.props.onClickCell(columnKey, rowValue);
  21. },
  22. };
  23. };
  24. render() {
  25. const { data, loading } = this.props;
  26. const tableModel = data || EMPTY_TABLE;
  27. const columnNames = tableModel.columns.map(({ text }) => text);
  28. const columns = tableModel.columns.map(({ filterable, text }) => ({
  29. Header: text,
  30. accessor: text,
  31. show: text !== 'Time',
  32. Cell: row => <span className={filterable ? 'link' : ''}>{row.value}</span>,
  33. }));
  34. const noDataText = data ? 'The queries returned no data for a table.' : '';
  35. return (
  36. <ReactTable
  37. columns={columns}
  38. data={tableModel.rows}
  39. getTdProps={this.getCellProps}
  40. loading={loading}
  41. minRows={0}
  42. noDataText={noDataText}
  43. resolveData={data => prepareRows(data, columnNames)}
  44. showPagination={data}
  45. />
  46. );
  47. }
  48. }