|
|
|
@ -12,7 +12,11 @@ import {
|
|
|
|
|
} from "tabulator-tables";
|
|
|
|
|
import { ValidationError } from "../../../core/Data/validate";
|
|
|
|
|
import Papa from "papaparse";
|
|
|
|
|
import { parseBool, parseNumber } from "../../../core/Operations/commonOps";
|
|
|
|
|
import {
|
|
|
|
|
parseBool,
|
|
|
|
|
parseNullableNumber,
|
|
|
|
|
parseNumber,
|
|
|
|
|
} from "../../../core/Operations/commonOps";
|
|
|
|
|
import { UnitCommitmentScenario } from "../../../core/Data/types";
|
|
|
|
|
|
|
|
|
|
export interface ColumnSpec {
|
|
|
|
@ -243,6 +247,12 @@ export const parseCsv = (
|
|
|
|
|
data[name][spec.title] = val;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "number?": {
|
|
|
|
|
const [val, err] = parseNullableNumber(row[spec.title]);
|
|
|
|
|
if (err) return [null, { message: err.message + rowRef }];
|
|
|
|
|
data[name][spec.title] = val;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "busRef":
|
|
|
|
|
const busName = row[spec.title];
|
|
|
|
|
if (!(busName in scenario.Buses)) {
|
|
|
|
@ -359,6 +369,7 @@ const DataTable = (props: DataTableProps) => {
|
|
|
|
|
const tableContainerRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
const tableRef = useRef<Tabulator | null>(null);
|
|
|
|
|
const [isTableBuilt, setTableBuilt] = useState<Boolean>(false);
|
|
|
|
|
const [activeCell, setActiveCell] = useState<CellComponent | null>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const onCellEdited = (cell: CellComponent) => {
|
|
|
|
@ -396,6 +407,7 @@ const DataTable = (props: DataTableProps) => {
|
|
|
|
|
const height = computeTableHeight(data);
|
|
|
|
|
|
|
|
|
|
if (tableRef.current === null) {
|
|
|
|
|
console.log("new Tabulator");
|
|
|
|
|
tableRef.current = new Tabulator(tableContainerRef.current, {
|
|
|
|
|
layout: "fitColumns",
|
|
|
|
|
data: data,
|
|
|
|
@ -411,13 +423,31 @@ const DataTable = (props: DataTableProps) => {
|
|
|
|
|
const newColumns = columns;
|
|
|
|
|
const newData = data;
|
|
|
|
|
const oldRows = tableRef.current.getRows();
|
|
|
|
|
const activeRowPosition = activeCell?.getRow().getPosition() as number;
|
|
|
|
|
const activeField = activeCell?.getField();
|
|
|
|
|
|
|
|
|
|
// Update data
|
|
|
|
|
tableRef.current.replaceData(newData).then(() => {});
|
|
|
|
|
|
|
|
|
|
// Restore active cell selection
|
|
|
|
|
if (activeCell) {
|
|
|
|
|
tableRef.current
|
|
|
|
|
?.getRowFromPosition(activeRowPosition!!)
|
|
|
|
|
?.getCell(activeField!!)
|
|
|
|
|
?.edit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update columns
|
|
|
|
|
if (newColumns.length !== tableRef.current.getColumns().length) {
|
|
|
|
|
let newColCount = 0;
|
|
|
|
|
newColumns.forEach((col) => {
|
|
|
|
|
if (col.columns) newColCount += col.columns.length;
|
|
|
|
|
else newColCount += 1;
|
|
|
|
|
});
|
|
|
|
|
if (newColCount !== tableRef.current.getColumns().length) {
|
|
|
|
|
tableRef.current.setColumns(newColumns);
|
|
|
|
|
const rows = tableRef.current!.getRows()!;
|
|
|
|
|
const firstRow = rows[0];
|
|
|
|
|
if (firstRow) firstRow.scrollTo().then((r) => {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update height
|
|
|
|
@ -435,15 +465,32 @@ const DataTable = (props: DataTableProps) => {
|
|
|
|
|
}, 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update callbacks
|
|
|
|
|
// Remove old callbacks
|
|
|
|
|
tableRef.current.off("cellEdited");
|
|
|
|
|
tableRef.current.off("cellEditing");
|
|
|
|
|
tableRef.current.off("cellEditCancelled");
|
|
|
|
|
|
|
|
|
|
// Set new callbacks
|
|
|
|
|
tableRef.current.on("cellEditing", (cell) => {
|
|
|
|
|
console.log("cellEditing", cell);
|
|
|
|
|
setActiveCell(cell);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tableRef.current.on("cellEditCancelled", (cell) => {
|
|
|
|
|
setActiveCell(null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tableRef.current.on("cellEdited", (cell) => {
|
|
|
|
|
onCellEdited(cell);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [props, isTableBuilt]);
|
|
|
|
|
|
|
|
|
|
return <div className="tableContainer" ref={tableContainerRef} />;
|
|
|
|
|
return (
|
|
|
|
|
<div className="tableWrapper">
|
|
|
|
|
<div ref={tableContainerRef} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default DataTable;
|
|
|
|
|