Skip to content

Commit d318146

Browse files
committed
fix: Update text according to AcceptsReturn
1 parent 28b08e5 commit d318146

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

src/Uno.UI.Tests/Windows_UI_XAML_Controls/TextBoxTests/Given_TextBox.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ public void When_BeforeTextChanging_Cancel()
9494
var textChangingCount = 0;
9595
var beforeTextChangingCount = 0;
9696
textBox.BeforeTextChanging += (tb, e) =>
97-
{
98-
beforeTextChangingCount++;
99-
if (e.NewText == "Papaya")
100-
{
101-
e.Cancel = true;
102-
}
103-
};
97+
{
98+
beforeTextChangingCount++;
99+
if (e.NewText == "Papaya")
100+
{
101+
e.Cancel = true;
102+
}
103+
};
104104
textBox.TextChanging += (tb, e) => textChangingCount++;
105105
textBox.Text = "Chirimoya";
106106
Assert.AreEqual("Chirimoya", textBox.Text);
@@ -116,6 +116,31 @@ public void When_BeforeTextChanging_Cancel()
116116
Assert.AreEqual(2, beforeTextChangingCount);
117117
}
118118

119+
[TestMethod]
120+
public void When_Multi_Line_Text_And_Not_AcceptsReturn()
121+
{
122+
var textBox = new TextBox();
123+
Assert.AreEqual(false, textBox.AcceptsReturn);
124+
textBox.Text = "Hello\nWorld";
125+
Assert.AreEqual("Hello", textBox.Text);
126+
127+
textBox.Text = "Hello\rWorld";
128+
Assert.AreEqual("Hello", textBox.Text);
129+
}
130+
131+
[TestMethod]
132+
public void When_Multi_Line_Text_And_Not_AcceptsReturn_After_Text_Was_Set()
133+
{
134+
var textBox = new TextBox();
135+
136+
textBox.AcceptsReturn = true;
137+
textBox.Text = "Hello\nWorld";
138+
Assert.AreEqual("Hello\nWorld", textBox.Text);
139+
140+
textBox.AcceptsReturn = false;
141+
Assert.AreEqual("Hello", textBox.Text);
142+
}
143+
119144
public class MySource : System.ComponentModel.INotifyPropertyChanged
120145
{
121146
private string _sourceText;

src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,20 @@ public string Text
168168
}
169169
}
170170

171+
private static string GetSingleLine(string value)
172+
{
173+
for (int i = 0; i < value.Length; i++)
174+
{
175+
var c = value[i];
176+
if (c == '\r' || c == '\n')
177+
{
178+
return value.Substring(0, i);
179+
}
180+
}
181+
182+
return value;
183+
}
184+
171185
public static DependencyProperty TextProperty { get; } =
172186
DependencyProperty.Register(
173187
"Text",
@@ -265,14 +279,19 @@ private object CoerceText(object baseValue)
265279
return DependencyProperty.UnsetValue;
266280
}
267281

282+
if (!AcceptsReturn)
283+
{
284+
baseString = GetSingleLine(baseString);
285+
}
286+
268287
var args = new TextBoxBeforeTextChangingEventArgs(baseString);
269288
BeforeTextChanging?.Invoke(this, args);
270289
if (args.Cancel)
271290
{
272291
return DependencyProperty.UnsetValue;
273292
}
274293

275-
return baseValue;
294+
return baseString;
276295
}
277296

278297
#endregion
@@ -404,6 +423,16 @@ public bool AcceptsReturn
404423

405424
private void OnAcceptsReturnChanged(DependencyPropertyChangedEventArgs e)
406425
{
426+
if (e.NewValue is false)
427+
{
428+
var text = Text;
429+
var singleLineText = GetSingleLine(text);
430+
if (text != singleLineText)
431+
{
432+
Text = singleLineText;
433+
}
434+
}
435+
407436
OnAcceptsReturnChangedPartial(e);
408437
UpdateButtonStates();
409438
}

0 commit comments

Comments
 (0)