Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 5 additions & 22 deletions maui/src/OtpInput/SfOtpInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -937,27 +937,11 @@ static void OnValuePropertyChanged(BindableObject bindable, object oldValue, obj

string newValueStr = newValue?.ToString() ?? string.Empty;
string oldValueStr = oldValue?.ToString() ?? string.Empty;

if (otpInput.Type == OtpInputType.Number)
{
newValueStr = new string(newValueStr.Where(c => !char.IsControl(c)).ToArray());
oldValueStr = new string(oldValueStr.Where(c => !char.IsControl(c)).ToArray());
}

if (otpInput.ValueChanged != null)
{
if (otpInput.Type == OtpInputType.Number)
{
RaiseValueChangedEvent(otpInput, oldValueStr, newValueStr);
}
else
{
RaiseValueChangedEvent(otpInput, oldValue?.ToString() ?? string.Empty, newValue?.ToString() ?? string.Empty);
}
}

otpInput.UpdateValue(bindable, newValue?? string.Empty);
}
newValueStr = new string(newValueStr.Where(c => !char.IsControl(c)).ToArray());
oldValueStr = new string(oldValueStr.Where(c => !char.IsControl(c)).ToArray());
RaiseValueChangedEvent(otpInput, oldValueStr, newValueStr);
otpInput.UpdateValue(bindable, newValue ?? string.Empty);
}
}

/// <summary>
Expand All @@ -970,7 +954,6 @@ static void OnLengthPropertyChanged(BindableObject bindable, object oldValue, ob
{
if (bindable is SfOtpInput otpInput && newValue is double newLength && oldValue is double oldLength)
{

if (newLength <= 0)
{
newLength = oldLength;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,37 @@ public void RaiseValueChangedEvent_ShouldTrigger_ValueChangedEvent()
Assert.True(eventTriggered);
}

[Theory]
[InlineData(OtpInputType.Number, "12", "12")]
[InlineData(OtpInputType.Text, "ab", "ab")]
[InlineData(OtpInputType.Password, "xy", "xy")]
public void ValueChangedEvent_WithPartialInput_DoesNotContainNullCharacters(OtpInputType type, string input, string expected)
{
// Arrange
var otpInput = new SfOtpInput
{
Length = 4,
Type = type
};
string? newValueFromEvent = null;
string? oldValueFromEvent = null;
otpInput.ValueChanged += (sender, e) =>
{
newValueFromEvent = e.NewValue;
oldValueFromEvent = e.OldValue;
};
// Act
otpInput.Value = input;
// Assert
Assert.Equal(expected, newValueFromEvent);
Assert.Equal(string.Empty, oldValueFromEvent);
if (newValueFromEvent != null)
{
Assert.DoesNotContain('\0', newValueFromEvent!);
}
}


#endregion

#region Public Methods
Expand Down