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

69 lines
2.1 KiB
TypeScript
Raw Normal View History

import _ from 'lodash';
2018-04-26 04:58:42 -05:00
import React, { PureComponent } from 'react';
import ReactTable, { RowInfo } 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: any[], columnNames: string[]) {
return rows.map(cells => _.zipObject(columnNames, cells));
}
2018-10-18 03:42:25 -05:00
export default class Table extends PureComponent<TableProps> {
getCellProps = (state: any, rowInfo: RowInfo, column: any) => {
return {
onClick: (e: React.SyntheticEvent) => {
// Only handle click on link, not the cell
if (e.target) {
const link = e.target as HTMLElement;
if (link.className === 'link') {
const columnKey = column.Header().props.title;
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: () => <span title={text}>{text}</span>,
accessor: text,
className: VALUE_REGEX.test(text) ? 'text-right' : '',
show: text !== 'Time',
Cell: (row: any) => (
<span className={filterable ? 'link' : ''} title={text + ': ' + row.value}>
{typeof row.value === 'string' ? row.value : JSON.stringify(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)}
showPagination={Boolean(data)}
/>
2018-04-26 04:58:42 -05:00
);
}
}