Skip to content

Commit

Permalink
Fix EntryCellRenderer to use fromhandler for text changes (#16462)
Browse files Browse the repository at this point in the history
Co-authored-by: Shane Neuville <[email protected]>
  • Loading branch information
github-actions[bot] and PureWeen authored Aug 3, 2023
1 parent 1869d0f commit fb23473
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ void OnEditingCompleted()
void OnTextChanged(string text)
{
var entryCell = (EntryCell)Cell;
entryCell.Text = text;
entryCell
.SetValue(EntryCell.TextProperty, text, specificity: SetterSpecificity.FromHandler);
}

void UpdateHeight()
Expand Down
3 changes: 2 additions & 1 deletion src/Compatibility/Core/src/iOS/Cells/EntryCellRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ static void OnTextFieldTextChanged(object sender, EventArgs eventArgs)
var cell = (EntryCellTableViewCell)sender;
var model = (EntryCell)cell.Cell;

model.Text = cell.TextField.Text;
model
.SetValue(EntryCell.TextProperty, cell.TextField.Text, specificity: SetterSpecificity.FromHandler);
}

static void UpdateHorizontalTextAlignment(EntryCellTableViewCell cell, EntryCell entryCell)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ void OnEditingCompleted()
void OnTextChanged(string text)
{
var entryCell = (EntryCell)Cell;
entryCell.Text = text;

entryCell
.SetValue(EntryCell.TextProperty, text, specificity: SetterSpecificity.FromHandler);
}

void UpdateHeight()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ static void OnTextFieldTextChanged(object sender, EventArgs eventArgs)
var cell = (EntryCellTableViewCell)sender;
var model = (EntryCell)cell.Cell;

model.Text = cell.TextField.Text;
model
.SetValue(EntryCell.TextProperty, cell.TextField.Text, specificity: SetterSpecificity.FromHandler);
}

static void UpdateHorizontalTextAlignment(EntryCellTableViewCell cell, EntryCell entryCell)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.ObjectModel;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
Expand All @@ -19,6 +21,7 @@ void SetupBuilder()
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<EntryCell, EntryCellRenderer>();
handlers.AddHandler<ViewCell, ViewCellRenderer>();
handlers.AddHandler<TextCell, TextCellRenderer>();
handlers.AddHandler<ListView, ListViewRenderer>();
Expand Down Expand Up @@ -113,6 +116,62 @@ await CreateHandlerAndAddToWindow<LayoutHandler>(layout, async (handler) =>
});
}

[Fact]
public async Task EntryCellBindingCorrectlyUpdates()
{
SetupBuilder();
var vm = new EntryCellBindingCorrectlyUpdatesVM();
var data = new[]
{
vm
};

var listView = new ListView()
{
ItemTemplate = new DataTemplate(() =>
{
var cell = new EntryCell();
cell.SetBinding(EntryCell.TextProperty, "Value");
return cell;
}),
ItemsSource = data
};

var layout = new VerticalStackLayout()
{
listView
};

await CreateHandlerAndAddToWindow<LayoutHandler>(layout, async (handler) =>
{
await Task.Yield();
var entryCell = listView.TemplatedItems[0] as EntryCell;

// Initial Value is correct
Assert.Equal(vm.Value, entryCell.Text);

// Validate that the binding stays operational
for (int i = 0; i < 3; i++)
{
vm.ChangeValue();
await Task.Yield();
Assert.Equal(vm.Value, entryCell.Text);
}
});
}
class EntryCellBindingCorrectlyUpdatesVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public string Value { get; set; }

public void ChangeValue()
{
Value = Guid.NewGuid().ToString();
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
}
}

[Fact]
public async Task ClearItemsListViewDoesntCrash()
{
Expand Down

0 comments on commit fb23473

Please sign in to comment.