reflex-components-dataeditor 0.10.0__tar.gz → 0.10.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,14 @@
1
+ ## v0.10.1 (2026-10-26)
2
+
3
+ ### Bug Fixes
4
+
5
+ - `rx.data_editor` columns backed by state now get the same defaults as literal columns: `type` defaults to `"str"`, `id` to the lowercased `title`, and a column may be given as just its title string. The column dicts passed to `columns` are no longer modified in place. ([#7607](https://github.com/reflex-dev/reflex/issues/7607))
6
+
7
+ ### Performance
8
+
9
+ - Faster redraws for `rx.data_editor` grids whose `columns` or `data` come from state. ([#7607](https://github.com/reflex-dev/reflex/issues/7607))
10
+
11
+
1
12
  ## v0.10.0 (2026-10-08)
2
13
 
3
14
  ### Breaking Changes
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: reflex-components-dataeditor
3
- Version: 0.10.0
3
+ Version: 0.10.1
4
4
  Summary: Reflex dataeditor components.
5
5
  Author-email: Khaleel Al-Adhami <khaleel@reflex.dev>
6
6
  Maintainer-email: Khaleel Al-Adhami <khaleel@reflex.dev>
@@ -12,7 +12,7 @@ Classifier: Programming Language :: Python :: 3.13
12
12
  Classifier: Programming Language :: Python :: 3.14
13
13
  Classifier: Programming Language :: Python :: 3.15
14
14
  Requires-Python: >=3.11
15
- Requires-Dist: reflex-base>=0.10.0
15
+ Requires-Dist: reflex-base>=0.10.1
16
16
  Requires-Dist: reflex-components-core>=0.10.0
17
17
  Description-Content-Type: text/markdown
18
18
 
@@ -16,7 +16,7 @@ classifiers = [
16
16
  "Programming Language :: Python :: 3.15",
17
17
  ]
18
18
  dependencies = [
19
- "reflex-base >= 0.10.0",
19
+ "reflex-base >= 0.10.1",
20
20
  "reflex-components-core >= 0.10.0",
21
21
  ]
22
22
 
@@ -14,8 +14,8 @@ from reflex_base.event import EventHandler, no_args_event_spec, passthrough_even
14
14
  from reflex_base.utils import format, types
15
15
  from reflex_base.utils.imports import ImportDict, ImportVar
16
16
  from reflex_base.utils.serializers import serializer
17
- from reflex_base.vars import get_unique_variable_name
18
- from reflex_base.vars.base import Var, VarData
17
+ from reflex_base.vars import get_leaf_vars, get_unique_variable_name
18
+ from reflex_base.vars.base import LiteralVar, Var, VarData
19
19
  from reflex_base.vars.function import FunctionStringVar
20
20
  from reflex_base.vars.sequence import ArrayVar
21
21
 
@@ -178,6 +178,88 @@ class GridColumn(TypedDict):
178
178
  group: str | None
179
179
 
180
180
 
181
+ # Fills in the documented column defaults for a state-backed column list at
182
+ # render time, taking the same shapes as _format_column: a column is a title
183
+ # string or an object, and a value the object sets explicitly wins through the
184
+ # spread.
185
+ _COLUMN_DEFAULTS_JS = (
186
+ '(col) => typeof col === "string" ? { title: col, id: col.toLowerCase(), type: "str" } '
187
+ ': { type: "str", id: (col.title ?? "").toLowerCase(), ...col }'
188
+ )
189
+
190
+
191
+ def _format_column(col: str | dict[str, Any] | Var) -> dict[str, Any] | Var:
192
+ """Fill in the default id, type and overlay icon of a literal column.
193
+
194
+ Args:
195
+ col: The column title, or the column definition.
196
+
197
+ Returns:
198
+ A new column definition with the defaults filled in; a Var is returned as is.
199
+
200
+ Raises:
201
+ ValueError: If the column is neither a string, a dict nor a Var.
202
+ """
203
+ if isinstance(col, str):
204
+ return {"title": col, "id": col.lower(), "type": "str"}
205
+ if isinstance(col, dict):
206
+ column = {"type": "str", "overlayIcon": None, **col}
207
+ if "id" not in column:
208
+ column["id"] = column["title"].lower()
209
+ return column
210
+ if isinstance(col, Var):
211
+ return col
212
+ msg = f"unexpected type ({type(col).__name__}: {col}) for column header in data_editor"
213
+ raise ValueError(msg)
214
+
215
+
216
+ def _hook_deps(*hook_vars: Var) -> str:
217
+ """Render the dependency array of a React hook closing over the given vars.
218
+
219
+ A derived var such as ``state.columns.filter(...)`` renders to a fresh value
220
+ on every evaluation, so the array lists the vars each one reads instead: the
221
+ state fields, loop vars and memo props behind it, down to those inside a
222
+ literal or a predicate, and none of the constants.
223
+
224
+ Args:
225
+ *hook_vars: The vars the hook reads.
226
+
227
+ Returns:
228
+ The dependency array as JavaScript.
229
+ """
230
+ deps = {str(dep): None for var in hook_vars for dep in get_leaf_vars(var)}
231
+ return f"[{', '.join(deps)}]"
232
+
233
+
234
+ def _with_column_defaults(columns: Var, editor_id: str) -> Var:
235
+ """Fill in the default type and id of a column list at render time.
236
+
237
+ The mapping runs in a ``useMemo`` keyed on the vars the columns read, so it
238
+ is redone when they change rather than on every render, let alone for every
239
+ cell Glide draws.
240
+
241
+ Args:
242
+ columns: The column list var.
243
+ editor_id: The unique id of the editor the columns belong to.
244
+
245
+ Returns:
246
+ A var naming the memoized column list, carrying the hook that defines it.
247
+ """
248
+ name = f"columns_{editor_id}"
249
+ return Var(
250
+ _js_expr=name,
251
+ # The mapping yields column dicts whatever the input holds, titles included.
252
+ _var_type=Sequence[dict[str, Any]],
253
+ _var_data=VarData(
254
+ hooks={
255
+ f"const {name} = useMemo(() => ({columns!s}).map({_COLUMN_DEFAULTS_JS}), "
256
+ f"{_hook_deps(columns)})": columns._get_all_var_data()
257
+ },
258
+ imports={"react": "useMemo"},
259
+ ),
260
+ )
261
+
262
+
181
263
  class DataEditor(NoSSRComponent):
182
264
  """The DataEditor Component."""
183
265
 
@@ -415,9 +497,6 @@ class DataEditor(NoSSRComponent):
415
497
  "react-responsive-carousel/lib/styles/carousel.min.css",
416
498
  ],
417
499
  self.library: ["GridCellKind", "CompactSelection"],
418
- "$/utils/helpers/dataeditor.js": ImportVar(
419
- tag="formatDataEditorCells", is_default=False, install=False
420
- ),
421
500
  }
422
501
 
423
502
  def add_custom_code(self) -> list[str]:
@@ -469,45 +548,23 @@ class DataEditor(NoSSRComponent):
469
548
  """
470
549
  ]
471
550
 
472
- def add_hooks(self) -> list[str]:
473
- """Get the hooks to render.
474
-
475
- Returns:
476
- The hooks to render.
477
- """
478
- # Define the id of the component in case multiple are used in the same page.
479
- editor_id = get_unique_variable_name()
480
-
481
- # Define the name of the getData callback associated with this component and assign to get_cell_content.
482
- if self.get_cell_content is not None:
483
- data_callback = self.get_cell_content._js_expr
484
- else:
485
- data_callback = f"getData_{editor_id}"
486
- self.get_cell_content = Var(_js_expr=data_callback)
487
-
488
- code = [f"function {data_callback}([col, row]){{"]
489
-
490
- columns_path = str(self.columns)
491
- data_path = str(self.data)
492
-
493
- code.extend([
494
- f" return formatDataEditorCells(col, row, {columns_path}, {data_path});",
495
- " }",
496
- ])
497
-
498
- return ["\n".join(code)]
499
-
500
551
  @classmethod
501
552
  def create(cls, *children, extended_cell_types: bool = False, **props) -> Component:
502
553
  """Create the DataEditor component.
503
554
 
555
+ The column defaults and the ``getCellContent`` callback are settled here:
556
+ a state-backed column list is mapped through the defaults in a ``useMemo``
557
+ keyed on it, and the callback is a ``useCallback`` over the columns and
558
+ data, so neither is rebuilt by a render that changes something else. Both
559
+ hooks ride on the vars assigned to the props.
560
+
504
561
  Args:
505
562
  *children: The children of the data editor.
506
563
  extended_cell_types: Whether to enable extended cell types.
507
564
  **props: The props of the data editor.
508
565
 
509
566
  Returns:
510
- The DataEditor component.&
567
+ The DataEditor component.
511
568
 
512
569
  Raises:
513
570
  ValueError: invalid input.
@@ -526,15 +583,52 @@ class DataEditor(NoSSRComponent):
526
583
 
527
584
  props["rows"] = data.length() if isinstance(data, ArrayVar) else len(data)
528
585
 
529
- if not isinstance(columns, Var) and len(columns):
530
- if types.is_dataframe(type(data)) or (
531
- isinstance(data, Var) and types.is_dataframe(data._var_type)
586
+ # Define the id of the component in case multiple are used in the same page.
587
+ editor_id = get_unique_variable_name()
588
+
589
+ if isinstance(columns, Var):
590
+ columns = _with_column_defaults(columns, editor_id)
591
+ else:
592
+ if columns and (
593
+ types.is_dataframe(type(data))
594
+ or (isinstance(data, Var) and types.is_dataframe(data._var_type))
532
595
  ):
533
596
  msg = "Cannot pass in both a pandas dataframe and columns to the data_editor component."
534
597
  raise ValueError(msg)
535
- props["columns"] = [
536
- format.format_data_editor_column(col) for col in columns
537
- ]
598
+ formatted = [_format_column(col) for col in columns]
599
+ columns = LiteralVar.create(formatted)
600
+ if any(isinstance(col, Var) for col in formatted):
601
+ # A var among the columns gets its defaults at render time, like
602
+ # a state-backed list; the mapping leaves the literal ones as is.
603
+ columns = _with_column_defaults(columns, editor_id)
604
+ if not isinstance(data, Var):
605
+ data = LiteralVar.create(data)
606
+ props["columns"] = columns
607
+ props["data"] = data
608
+
609
+ if props.pop("get_cell_content", None) is not None:
610
+ logger.warning(
611
+ "get_cell_content is not user configurable, the provided value will be discarded"
612
+ )
613
+ data_callback = f"getData_{editor_id}"
614
+ props["get_cell_content"] = Var(
615
+ _js_expr=data_callback,
616
+ _var_data=VarData(
617
+ hooks={
618
+ f"const {data_callback} = useCallback(([col, row]) => "
619
+ f"formatDataEditorCells(col, row, {columns!s}, {data!s}), "
620
+ f"{_hook_deps(columns, data)})": VarData.merge(
621
+ columns._get_all_var_data(), data._get_all_var_data()
622
+ )
623
+ },
624
+ imports={
625
+ "react": "useCallback",
626
+ "$/utils/helpers/dataeditor.js": ImportVar(
627
+ tag="formatDataEditorCells", is_default=False, install=False
628
+ ),
629
+ },
630
+ ),
631
+ )
538
632
 
539
633
  if "theme" in props:
540
634
  theme = props.get("theme")
@@ -547,11 +641,6 @@ class DataEditor(NoSSRComponent):
547
641
  # Disable on_paste by default if not provided.
548
642
  props.setdefault("on_paste", False)
549
643
 
550
- if props.pop("get_cell_content", None) is not None:
551
- logger.warning(
552
- "get_cell_content is not user configurable, the provided value will be discarded"
553
- )
554
-
555
644
  # Apply the reconstruction function to grid_selection if it's a Var
556
645
  if (grid_selection := props.get("grid_selection")) is not None and isinstance(
557
646
  grid_selection, Var
@@ -132,10 +132,11 @@ class GridColumn(TypedDict):
132
132
  title: str
133
133
  group: str | None
134
134
 
135
+ _COLUMN_DEFAULTS_JS = '(col) => typeof col === "string" ? { title: col, id: col.toLowerCase(), type: "str" } : { type: "str", id: (col.title ?? "").toLowerCase(), ...col }'
136
+
135
137
  class DataEditor(NoSSRComponent):
136
138
  def add_imports(self) -> ImportDict: ...
137
139
  def add_custom_code(self) -> list[str]: ...
138
- def add_hooks(self) -> list[str]: ...
139
140
  @classmethod
140
141
  def create(
141
142
  cls,
@@ -258,6 +259,12 @@ class DataEditor(NoSSRComponent):
258
259
  ) -> DataEditor:
259
260
  """Create the DataEditor component.
260
261
 
262
+ The column defaults and the ``getCellContent`` callback are settled here:
263
+ a state-backed column list is mapped through the defaults in a ``useMemo``
264
+ keyed on it, and the callback is a ``useCallback`` over the columns and
265
+ data, so neither is rebuilt by a render that changes something else. Both
266
+ hooks ride on the vars assigned to the props.
267
+
261
268
  Args:
262
269
  *children: The children of the data editor.
263
270
  extended_cell_types: Whether to enable extended cell types.
@@ -344,7 +351,7 @@ class DataEditor(NoSSRComponent):
344
351
  **props: The props of the data editor.
345
352
 
346
353
  Returns:
347
- The DataEditor component.&
354
+ The DataEditor component.
348
355
 
349
356
  Raises:
350
357
  ValueError: invalid input.