grafana/public/app/features/explore/Table.tsx

59 lines
1.6 KiB
TypeScript
Raw Normal View History

import _ from 'lodash';
2018-04-26 04:58:42 -05:00
import React, { PureComponent } from 'react';
import ReactTable from 'react-table';
import TableModel from 'app/core/table_model';
2018-04-26 04:58:42 -05:00
const EMPTY_TABLE = new TableModel();
// Identify columns that contain values
const VALUE_REGEX = /^[Vv]alue #\d+/;
2018-04-26 04:58:42 -05:00
interface TableProps {
data: TableModel;
loading: boolean;
onClickCell?: (columnKey: string, rowValue: string) => void;
}
function prepareRows(rows, columnNames) {
return rows.map(cells => _.zipObject(columnNames, cells));
}
2018-10-18 03:42:25 -05:00
export default class Table extends PureComponent<TableProps> {
getCellProps = (state, rowInfo, column) => {
return {
onClick: () => {
const columnKey = column.Header;
const rowValue = rowInfo.row[columnKey];
this.props.onClickCell(columnKey, rowValue);
},
};
};
2018-04-26 04:58:42 -05:00
render() {
const { data, loading } = this.props;
const tableModel = data || EMPTY_TABLE;
const columnNames = tableModel.columns.map(({ text }) => text);
const columns = tableModel.columns.map(({ filterable, text }) => ({
Header: text,
accessor: text,
className: VALUE_REGEX.test(text) ? 'text-right' : '',
show: text !== 'Time',
Cell: row => <span className={filterable ? 'link' : ''}>{row.value}</span>,
}));
2018-10-18 03:42:25 -05:00
const noDataText = data ? 'The queries returned no data for a table.' : '';
2018-04-26 04:58:42 -05:00
return (
<ReactTable
columns={columns}
data={tableModel.rows}
getTdProps={this.getCellProps}
loading={loading}
minRows={0}
2018-10-18 03:42:25 -05:00
noDataText={noDataText}
resolveData={data => prepareRows(data, columnNames)}
2018-10-18 03:42:25 -05:00
showPagination={data}
/>
2018-04-26 04:58:42 -05:00
);
}
}