Skip to content

Commit

Permalink
unify BitTextFieldType and BitOtpInputType enums into BitInputType #8537
Browse files Browse the repository at this point in the history
  • Loading branch information
msynk committed Sep 7, 2024
1 parent 8d4d867 commit d34284e
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,31 +92,31 @@ public void BitOtpInputVerticalTest(bool vertical)
}

[DataTestMethod,
DataRow(BitOtpInputType.Text),
DataRow(BitOtpInputType.Number),
DataRow(BitOtpInputType.Password)
DataRow(BitInputType.Text),
DataRow(BitInputType.Number),
DataRow(BitInputType.Password)
]
public void BitOtpInputTypeTest(BitOtpInputType inputType)
public void BitInputTypeTest(BitInputType inputType)
{
var com = RenderComponent<BitOtpInput>(parameters =>
{
parameters.Add(p => p.Length, 1);
parameters.Add(p => p.InputType, inputType);
parameters.Add(p => p.Type, inputType);
});

string inputTypeAttribute = inputType switch
{
BitOtpInputType.Text => "text",
BitOtpInputType.Number => "number",
BitOtpInputType.Password => "password",
BitInputType.Text => "text",
BitInputType.Number => "number",
BitInputType.Password => "password",
_ => string.Empty
};

string inputModeAttribute = inputType switch
{
BitOtpInputType.Text => "text",
BitOtpInputType.Number => "numeric",
BitOtpInputType.Password => "text",
BitInputType.Text => "text",
BitInputType.Number => "numeric",
BitInputType.Password => "text",
_ => string.Empty
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void BitTextFieldCanRevealPassword(bool isEnabled)
var component = RenderComponent<BitTextField>(parameters =>
{
parameters.Add(p => p.IsEnabled, isEnabled);
parameters.Add(p => p.Type, BitTextFieldType.Password);
parameters.Add(p => p.Type, BitInputType.Password);
parameters.Add(p => p.CanRevealPassword, true);
});

Expand All @@ -137,11 +137,11 @@ public void BitTextFieldCanRevealPassword(bool isEnabled)
}

[DataTestMethod,
DataRow(BitTextFieldType.Password),
DataRow(BitTextFieldType.Number),
DataRow(BitTextFieldType.Tel)
DataRow(BitInputType.Password),
DataRow(BitInputType.Number),
DataRow(BitInputType.Tel)
]
public void BitTextFieldTypeTest(BitTextFieldType type)
public void BitInputTypeTest(BitInputType type)
{
var component = RenderComponent<BitTextField>(parameters =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
[Parameter] public string Placeholder { get; set; }
[Parameter] public bool CanRevealPassword { get; set; }
[Parameter] public string RevealPasswordAriaLabel { get; set; }
[Parameter] public BitTextFieldType Type { get; set; }
[Parameter] public BitInputType Type { get; set; }
[Parameter] public string AriaLabel { get; set; }
[Parameter] public string Label { get; set; }
[Parameter] public string Prefix { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
namespace Bit.BlazorUI;

public enum BitTextFieldType
public enum BitInputType
{
/// <summary>
/// The TextField characters are shown as text.
/// The input expects text characters.
/// </summary>
Text,

/// <summary>
/// The TextField characters are masked.
/// The input expects password characters.
/// </summary>
Password,

/// <summary>
/// The TextField characters are number.
/// The input expects number characters.
/// </summary>
Number,

/// <summary>
/// The TextField act as an email input.
/// The input expects email characters.
/// </summary>
Email,

/// <summary>
/// The TextField act as a tel input.
/// The input expects tel characters.
/// </summary>
Tel,

/// <summary>
/// The TextField act as a url input.
/// The input expects url characters.
/// </summary>
Url
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ public partial class BitOtpInput : BitInputBase<string?>, IDisposable
/// </summary>
[Parameter] public BitOtpInputClassStyles? Classes { get; set; }

/// <summary>
/// Type of the inputs.
/// </summary>
[Parameter] public BitOtpInputType InputType { get; set; } = BitOtpInputType.Text;

/// <summary>
/// Length of the OTP or number of the inputs.
/// </summary>
Expand Down Expand Up @@ -76,6 +71,11 @@ public partial class BitOtpInput : BitInputBase<string?>, IDisposable
/// </summary>
[Parameter] public BitOtpInputClassStyles? Styles { get; set; }

/// <summary>
/// Type of the inputs.
/// </summary>
[Parameter] public BitInputType? Type { get; set; }

/// <summary>
/// Defines whether to render inputs vertically.
/// </summary>
Expand All @@ -99,7 +99,7 @@ public async Task SetPastedData(string pastedValue)
if (IsEnabled is false) return;
if (ValueHasBeenSet && ValueChanged.HasDelegate is false) return;
if (pastedValue.HasNoValue()) return;
if (InputType is BitOtpInputType.Number && int.TryParse(pastedValue, out _) is false) return;
if (Type is BitInputType.Number && int.TryParse(pastedValue, out _) is false) return;

SetInputsValue(pastedValue);

Expand All @@ -112,6 +112,20 @@ public async Task SetPastedData(string pastedValue)

protected override string RootElementClass => "bit-otp";

protected override void RegisterCssClasses()
{
ClassBuilder.Register(() => Classes?.Root);

ClassBuilder.Register(() => Reversed ? "bit-otp-rvs" : string.Empty);

ClassBuilder.Register(() => Vertical ? "bit-otp-vrt" : string.Empty);
}

protected override void RegisterCssStyles()
{
StyleBuilder.Register(() => Styles?.Root);
}

protected override void OnInitialized()
{
_inputRefs = new ElementReference[Length];
Expand Down Expand Up @@ -152,20 +166,6 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
}
}

protected override void RegisterCssClasses()
{
ClassBuilder.Register(() => Classes?.Root);

ClassBuilder.Register(() => Reversed ? $"{RootElementClass}-rvs" : string.Empty);

ClassBuilder.Register(() => Vertical ? $"{RootElementClass}-vrt" : string.Empty);
}

protected override void RegisterCssStyles()
{
StyleBuilder.Register(() => Styles?.Root);
}

protected override bool TryParseValueFromString(string? value, [MaybeNullWhen(false)] out string? result, [NotNullWhen(false)] out string? parsingErrorMessage)
{
result = value;
Expand All @@ -185,20 +185,22 @@ protected override void Dispose(bool disposing)



private string GetInputType() => InputType switch
private string GetInputType() => Type switch
{
BitOtpInputType.Text => "text",
BitOtpInputType.Number => "number",
BitOtpInputType.Password => "password",
_ => string.Empty
BitInputType.Text => "text",
BitInputType.Number => "number",
BitInputType.Password => "password",
BitInputType.Email => "email",
BitInputType.Tel => "tel",
BitInputType.Url => "url",
_ => "text"
};

private string GetInputMode() => InputType switch
private string GetInputMode() => Type switch
{
BitOtpInputType.Text => "text",
BitOtpInputType.Number => "numeric",
BitOtpInputType.Password => "text",
_ => string.Empty
BitInputType.Text => "text",
BitInputType.Number => "numeric",
_ => "text"
};

private string GetInputStyles(int index)
Expand Down Expand Up @@ -273,7 +275,7 @@ private async Task HandleOnInput(ChangeEventArgs e, int index)
{
var diff = DiffValues(oldValue ?? string.Empty, newValue);

if (InputType is BitOtpInputType.Number && int.TryParse(diff, out _) is false)
if (Type is BitInputType.Number && int.TryParse(diff, out _) is false)
{
_inputValues[index] = oldValue;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</div>
}

@if (IsMultiline && Type == BitTextFieldType.Text)
@if (IsMultiline && Type == BitInputType.Text)
{
<textarea @ref="InputElement" @attributes="InputHtmlAttributes"
@onclick="@HandleOnClick"
Expand Down Expand Up @@ -93,18 +93,18 @@
aria-describedby="@(Description.HasValue() || DescriptionTemplate is not null ? _descriptionId : null)" />
}

@if (CanRevealPassword && Type == BitTextFieldType.Password)
@if (CanRevealPassword && Type == BitInputType.Password)
{
<button @onclick="ToggleRevealPassword"
style="@Styles?.RevealPassword"
class="bit-tfl-prb @Classes?.RevealPassword"
type="button"
aria-label="@RevealPasswordAriaLabel"
aria-pressed="@(_elementType == BitTextFieldType.Text ? "true" : "false")">
aria-pressed="@(_elementType == BitInputType.Text ? "true" : "false")">
<span style="@Styles?.RevealPasswordIconContainer"
class="bit-tfl-rin @Classes?.RevealPasswordIconContainer">
<i style="@Styles?.RevealPasswordIcon"
class="bit-tfl-ric bit-icon bit-icon--@(_elementType == BitTextFieldType.Text ? "Hide3" : "View") @Classes?.RevealPasswordIcon"
class="bit-tfl-ric bit-icon bit-icon--@(_elementType == BitInputType.Text ? "Hide3" : "View") @Classes?.RevealPasswordIcon"
aria-hidden="true" />
</span>
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public partial class BitTextField : BitTextInputBase<string?>
{
private bool _hasFocus;
private bool _isPasswordRevealed;
private BitTextFieldType _elementType;
private BitInputType _elementType;
private string _inputId = string.Empty;
private string _labelId = string.Empty;
private string _inputType = string.Empty;
Expand Down Expand Up @@ -175,7 +175,7 @@ public partial class BitTextField : BitTextInputBase<string?>
/// </summary>
[Parameter, ResetClassBuilder]
[CallOnSet(nameof(SetElementType))]
public BitTextFieldType Type { get; set; }
public BitInputType Type { get; set; } = BitInputType.Text;



Expand All @@ -193,7 +193,7 @@ protected override void RegisterCssClasses()
{
ClassBuilder.Register(() => Classes?.Root);

ClassBuilder.Register(() => IsMultiline && Type == BitTextFieldType.Text
ClassBuilder.Register(() => IsMultiline && Type == BitInputType.Text
? $"bit-tfl-{(IsResizable ? "mln" : "mlf")}"
: string.Empty);

Expand Down Expand Up @@ -252,18 +252,18 @@ protected override bool TryParseValueFromString(string? value, [MaybeNullWhen(fa

private void SetElementType()
{
_elementType = Type is BitTextFieldType.Password && CanRevealPassword && _isPasswordRevealed
? BitTextFieldType.Text
_elementType = Type is BitInputType.Password && CanRevealPassword && _isPasswordRevealed
? BitInputType.Text
: Type;

_inputType = _elementType switch
{
BitTextFieldType.Text => "text",
BitTextFieldType.Password => "password",
BitTextFieldType.Number => "number",
BitTextFieldType.Email => "email",
BitTextFieldType.Tel => "tel",
BitTextFieldType.Url => "url",
BitInputType.Text => "text",
BitInputType.Number => "number",
BitInputType.Password => "password",
BitInputType.Email => "email",
BitInputType.Tel => "tel",
BitInputType.Url => "url",
_ => string.Empty,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@
</ExamplePreview>
</ComponentExampleBox>

<ComponentExampleBox Title="InputType" RazorCode="@example2RazorCode" Id="example2">
<ComponentExampleBox Title="Type" RazorCode="@example2RazorCode" Id="example2">
<ExamplePreview>
<div>Set different input types for the OTP input: text, number, and password.</div>
<br />
<div class="example-box">
<div class="lbl">Text</div>
<BitOtpInput InputType="BitOtpInputType.Text" />
<BitOtpInput Type="BitInputType.Text" />
<br />
<div class="lbl">Number</div>
<BitOtpInput InputType="BitOtpInputType.Number" />
<BitOtpInput Type="BitInputType.Number" />
<br />
<div class="lbl">Password</div>
<BitOtpInput InputType="BitOtpInputType.Password" />
<BitOtpInput Type="BitInputType.Password" />
</div>
</ExamplePreview>
</ComponentExampleBox>
Expand Down
Loading

0 comments on commit d34284e

Please sign in to comment.