Skip to content

Commit

Permalink
Merge pull request #109 from anmcgrath/cell-value-set-take-into-accou…
Browse files Browse the repository at this point in the history
…nt-type

Don't convert editor input string if the cell type is text.
  • Loading branch information
anmcgrath authored Oct 15, 2024
2 parents 7415cfa + 95362ef commit 07a46f6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/BlazorDatasheet.Core/Edit/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public Editor(Sheet sheet)
/// <param name="col"></param>
/// <param name="isSoftEdit">If true, the editor should hold on to the edit when arrow keys are pressed.</param>
/// <param name="mode">Details on the method that was used to begin the edit.</param>
/// <param name="mode">Details on the method that was used to begin the edit.</param>
/// <param name="key">The key that was used to begin the edit, if the entry mode was using the keyboard</param>
public void BeginEdit(int row, int col, bool isSoftEdit = true, EditEntryMode mode = EditEntryMode.None,
string? key = null)
Expand Down Expand Up @@ -155,7 +156,7 @@ public bool AcceptEdit()
}

var formulaResult = isFormula ? Sheet.FormulaEngine.Evaluate(parsedFormula) : CellValue.Empty;
var editValue = isFormula ? formulaResult : new CellValue(this.EditValue);
var editValue = isFormula ? formulaResult : GetValueAsCellValue(EditCell.Row, EditCell.Col, EditValue);

var beforeAcceptEdit = new BeforeAcceptEditEventArgs(EditCell, editValue, parsedFormula, formulaString);
BeforeEditAccepted?.Invoke(this, beforeAcceptEdit);
Expand Down Expand Up @@ -196,6 +197,18 @@ public bool AcceptEdit()
return false;
}

private CellValue GetValueAsCellValue(int row, int col, string editValue)
{
var type = Sheet.Cells.GetCellType(row, col);
switch (type)
{
case "text":
return CellValue.Text(editValue);
}

return new CellValue(editValue);
}

private void ClearEdit()
{
IsEditing = false;
Expand Down
4 changes: 2 additions & 2 deletions src/BlazorDatasheet/DatasheetRow.razor
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
@switch (visualCell.CellType)
{
case "default":
case "text":
case "datetime":
<div style="width: 100%;">
@visualCell.FormattedString
Expand Down Expand Up @@ -145,5 +146,4 @@
}


}

}
12 changes: 12 additions & 0 deletions test/BlazorDatasheet.Test/Edit/EditTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BlazorDatasheet.Core.Data;
using FluentAssertions;
using NUnit.Framework;

namespace BlazorDatasheet.Test.Edit;
Expand Down Expand Up @@ -70,4 +71,15 @@ public void Accept_Edit_Is_Undoable()
sheet.Commands.Undo();
Assert.AreEqual(null, sheet.Cells.GetValue(0, 0));
}

[Test]
public void Do_Not_Do_Conversion_If_Cell_Type_Is_Set_To_Text()
{
var sheet = new Sheet(10, 10);
sheet.Cells[0, 0].Type = "text";
sheet.Editor.BeginEdit(0, 0);
sheet.Editor.EditValue = "04-10-1";
sheet.Editor.AcceptEdit();
sheet.Cells[0,0].Value.Should().Be("04-10-1");
}
}

0 comments on commit 07a46f6

Please sign in to comment.