2018-04-26 04:58:42 -05:00
|
|
|
import React, { PureComponent } from 'react';
|
2018-08-03 03:20:13 -05:00
|
|
|
import TableModel from 'app/core/table_model';
|
2018-04-26 04:58:42 -05:00
|
|
|
|
2018-08-03 03:20:13 -05:00
|
|
|
const EMPTY_TABLE = new TableModel();
|
2018-04-26 04:58:42 -05:00
|
|
|
|
2018-08-03 03:20:13 -05:00
|
|
|
interface TableProps {
|
|
|
|
className?: string;
|
|
|
|
data: TableModel;
|
2018-08-04 04:07:48 -05:00
|
|
|
loading: boolean;
|
2018-08-03 03:20:13 -05:00
|
|
|
onClickCell?: (columnKey: string, rowValue: string) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SFCCellProps {
|
|
|
|
columnIndex: number;
|
|
|
|
onClickCell?: (columnKey: string, rowValue: string, columnIndex: number, rowIndex: number, table: TableModel) => void;
|
|
|
|
rowIndex: number;
|
|
|
|
table: TableModel;
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Cell(props: SFCCellProps) {
|
|
|
|
const { columnIndex, rowIndex, table, value, onClickCell } = props;
|
|
|
|
const column = table.columns[columnIndex];
|
|
|
|
if (column && column.filterable && onClickCell) {
|
|
|
|
const onClick = event => {
|
|
|
|
event.preventDefault();
|
|
|
|
onClickCell(column.text, value, columnIndex, rowIndex, table);
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<td>
|
|
|
|
<a className="link" onClick={onClick}>
|
|
|
|
{value}
|
|
|
|
</a>
|
|
|
|
</td>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return <td>{value}</td>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Table extends PureComponent<TableProps, {}> {
|
2018-04-26 04:58:42 -05:00
|
|
|
render() {
|
2018-08-04 04:07:48 -05:00
|
|
|
const { className = '', data, loading, onClickCell } = this.props;
|
|
|
|
let tableModel = data || EMPTY_TABLE;
|
|
|
|
if (!loading && data && data.rows.length === 0) {
|
|
|
|
return (
|
|
|
|
<table className={`${className} filter-table`}>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Table</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<td className="muted">The queries returned no data for a table.</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
);
|
|
|
|
}
|
2018-04-26 04:58:42 -05:00
|
|
|
return (
|
|
|
|
<table className={`${className} filter-table`}>
|
|
|
|
<thead>
|
|
|
|
<tr>{tableModel.columns.map(col => <th key={col.text}>{col.text}</th>)}</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2018-08-03 03:20:13 -05:00
|
|
|
{tableModel.rows.map((row, i) => (
|
|
|
|
<tr key={i}>
|
|
|
|
{row.map((value, j) => (
|
2018-08-07 10:42:00 -05:00
|
|
|
<Cell
|
|
|
|
key={j}
|
|
|
|
columnIndex={j}
|
|
|
|
rowIndex={i}
|
|
|
|
value={String(value)}
|
|
|
|
table={data}
|
|
|
|
onClickCell={onClickCell}
|
|
|
|
/>
|
2018-08-03 03:20:13 -05:00
|
|
|
))}
|
|
|
|
</tr>
|
|
|
|
))}
|
2018-04-26 04:58:42 -05:00
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|