From 5d169b693b8821cd5ea4df2888443adad6ae73fb Mon Sep 17 00:00:00 2001 From: Lendemor Date: Tue, 31 Oct 2023 16:58:19 +0100 Subject: [PATCH] cleanup dataeditor js code and hooks --- .../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 bd8ce89b1c..5861d60bf4 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 6f4fc8c487..cab6020c4c 100644 --- a/reflex/components/datadisplay/dataeditor.py +++ b/reflex/components/datadisplay/dataeditor.py @@ -211,11 +211,9 @@ def _get_imports(self): }, 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 @@ def _get_hooks(self) -> str | None: 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