From 60c8c1d40eda4ee03cd0d61574fe37ee461b6421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Brand=C3=A9ho?= Date: Wed, 1 Nov 2023 01:06:49 +0100 Subject: [PATCH] cleanup dataeditor js code and hooks (#2095) --- .../web/utils/helpers/dataeditor.js | 13 +++++++++++- reflex/components/datadisplay/dataeditor.py | 21 +++++++------------ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/reflex/.templates/web/utils/helpers/dataeditor.js b/reflex/.templates/web/utils/helpers/dataeditor.js index bd8ce89b1..5861d60bf 100644 --- a/reflex/.templates/web/utils/helpers/dataeditor.js +++ b/reflex/.templates/web/utils/helpers/dataeditor.js @@ -47,10 +47,21 @@ export function formatCell(value, column) { readonly: !editable, } default: + console.log("Warning: column.type is undefined for column.title=" + column.title) return { kind: GridCellKind.Text, data: value, - displayData: "type not specified in column definition" + displayData: column.type } }; }; + +export function formatDataEditorCells(col, row, columns, data) { + if (row < data.length && col < columns.length) { + const column = getDEColumn(columns, col); + const rowData = getDERow(data, row); + const cellData = locateCell(rowData, column); + return formatCell(cellData, column); + } + return { kind: GridCellKind.Loading }; +} \ No newline at end of file diff --git a/reflex/components/datadisplay/dataeditor.py b/reflex/components/datadisplay/dataeditor.py index 6f4fc8c48..cab6020c4 100644 --- a/reflex/components/datadisplay/dataeditor.py +++ b/reflex/components/datadisplay/dataeditor.py @@ -211,11 +211,9 @@ class DataEditor(NoSSRComponent): }, self.library: {ImportVar(tag="GridCellKind")}, "/utils/helpers/dataeditor.js": { - ImportVar(tag=f"getDEColumn", is_default=False, install=False), - ImportVar(tag=f"getDERow", is_default=False, install=False), - ImportVar(tag=f"locateCell", is_default=False, install=False), - ImportVar(tag=f"formatCell", is_default=False, install=False), - ImportVar(tag=f"onEditCell", is_default=False, install=False), + ImportVar( + tag=f"formatDataEditorCells", is_default=False, install=False + ), }, }, ) @@ -258,21 +256,16 @@ class DataEditor(NoSSRComponent): code = [f"function {data_callback}([col, row])" "{"] + columns_path = f"{self.columns._var_full_name}" + data_path = f"{self.data._var_full_name}" + code.extend( [ - f" if (row < {self.data._var_full_name}.length && col < {self.columns._var_full_name}.length)" - " {", - f" const column = getDEColumn({self.columns._var_full_name}, col);", - f" const rowData = getDERow({self.data._var_full_name}, row);", - f" const cellData = locateCell(rowData, column);", - " return formatCell(cellData, column);", + f" return formatDataEditorCells(col, row, {columns_path}, {data_path});", " }", - " return { kind: GridCellKind.Loading};", ] ) - code.append("}") - return "\n".join(code) @classmethod