Skip to content

Commit c908b42

Browse files
Merge pull request #250 from syncfusion/OtpInputValueMismatch
Resolved the OtpInput value mismatch issue for input types text and password.
2 parents 6e12e19 + 040f0f1 commit c908b42

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

maui/src/OtpInput/SfOtpInput.cs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -937,27 +937,11 @@ static void OnValuePropertyChanged(BindableObject bindable, object oldValue, obj
937937

938938
string newValueStr = newValue?.ToString() ?? string.Empty;
939939
string oldValueStr = oldValue?.ToString() ?? string.Empty;
940-
941-
if (otpInput.Type == OtpInputType.Number)
942-
{
943-
newValueStr = new string(newValueStr.Where(c => !char.IsControl(c)).ToArray());
944-
oldValueStr = new string(oldValueStr.Where(c => !char.IsControl(c)).ToArray());
945-
}
946-
947-
if (otpInput.ValueChanged != null)
948-
{
949-
if (otpInput.Type == OtpInputType.Number)
950-
{
951-
RaiseValueChangedEvent(otpInput, oldValueStr, newValueStr);
952-
}
953-
else
954-
{
955-
RaiseValueChangedEvent(otpInput, oldValue?.ToString() ?? string.Empty, newValue?.ToString() ?? string.Empty);
956-
}
957-
}
958-
959-
otpInput.UpdateValue(bindable, newValue?? string.Empty);
960-
}
940+
newValueStr = new string(newValueStr.Where(c => !char.IsControl(c)).ToArray());
941+
oldValueStr = new string(oldValueStr.Where(c => !char.IsControl(c)).ToArray());
942+
RaiseValueChangedEvent(otpInput, oldValueStr, newValueStr);
943+
otpInput.UpdateValue(bindable, newValue ?? string.Empty);
944+
}
961945
}
962946

963947
/// <summary>
@@ -970,7 +954,6 @@ static void OnLengthPropertyChanged(BindableObject bindable, object oldValue, ob
970954
{
971955
if (bindable is SfOtpInput otpInput && newValue is double newLength && oldValue is double oldLength)
972956
{
973-
974957
if (newLength <= 0)
975958
{
976959
newLength = oldLength;

maui/tests/Syncfusion.Maui.Toolkit.UnitTest/Editors/SfOtpInputUnitTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,37 @@ public void RaiseValueChangedEvent_ShouldTrigger_ValueChangedEvent()
474474
Assert.True(eventTriggered);
475475
}
476476

477+
[Theory]
478+
[InlineData(OtpInputType.Number, "12", "12")]
479+
[InlineData(OtpInputType.Text, "ab", "ab")]
480+
[InlineData(OtpInputType.Password, "xy", "xy")]
481+
public void ValueChangedEvent_WithPartialInput_DoesNotContainNullCharacters(OtpInputType type, string input, string expected)
482+
{
483+
// Arrange
484+
var otpInput = new SfOtpInput
485+
{
486+
Length = 4,
487+
Type = type
488+
};
489+
string? newValueFromEvent = null;
490+
string? oldValueFromEvent = null;
491+
otpInput.ValueChanged += (sender, e) =>
492+
{
493+
newValueFromEvent = e.NewValue;
494+
oldValueFromEvent = e.OldValue;
495+
};
496+
// Act
497+
otpInput.Value = input;
498+
// Assert
499+
Assert.Equal(expected, newValueFromEvent);
500+
Assert.Equal(string.Empty, oldValueFromEvent);
501+
if (newValueFromEvent != null)
502+
{
503+
Assert.DoesNotContain('\0', newValueFromEvent!);
504+
}
505+
}
506+
507+
477508
#endregion
478509

479510
#region Public Methods

0 commit comments

Comments
 (0)