apply<T extends DataGridRow> method

  1. @override
DataGridState<T>? apply<T extends DataGridRow>(
  1. EventContext<T> context
)
override

Apply this event's transformation to the state. Returns the new state, or null if no state change should occur. Can return a Future for async operations.

Implementation

@override
DataGridState<T>? apply<T extends DataGridRow>(EventContext<T> context) {
  final column = context.state.columns.firstWhere(
    (c) => c.id == columnId,
    orElse: () => throw Exception('Column not found'),
  );

  if (!column.editable) {
    return null;
  }

  if (context.canEditCell != null && !context.canEditCell!(rowId, columnId)) {
    return null;
  }

  final row = context.state.rowsById[rowId];
  if (row == null) {
    return null;
  }

  final cellId = context.state.edit.createCellId(rowId, columnId);
  final currentValue = context.dataIndexer.getCellValue(row, column);

  var newState = context.state;

  if (context.state.edit.isEditing) {
    final currentCellId = context.state.edit.editingCellId!;

    if (currentCellId != cellId) {
      final parts = currentCellId.split('_');
      final currentRowId = double.parse(parts[0]);
      final currentColumnId = int.parse(parts[1]);
      final editValue = context.state.edit.editingValue;

      context.dispatchEvent(
        UpdateCellEvent(
          rowId: currentRowId,
          columnId: currentColumnId,
          value: editValue,
        ),
      );
    }
  }

  return newState.copyWith(
    edit: newState.edit.copyWith(
      editingCellId: cellId,
      editingValue: currentValue,
    ),
  );
}