2021-09-07 13:46:34 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2021-04-18 16:06:06 +08:00
|
|
|
|
using AntDesign.Core.Extensions;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2020-03-23 09:31:08 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-06-27 01:20:52 +08:00
|
|
|
|
public partial class InputPassword : Input<string>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
private bool _visible = false;
|
|
|
|
|
private string _eyeIcon;
|
|
|
|
|
|
2021-05-27 18:13:26 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Custom icon render
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment IconRender { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether to show password
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool ShowPassword
|
|
|
|
|
{
|
|
|
|
|
get => _visible;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_visible = value;
|
|
|
|
|
if (_visible)
|
|
|
|
|
Type = "text";
|
|
|
|
|
else
|
|
|
|
|
Type = "password";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether show toggle button
|
|
|
|
|
/// </summary>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public bool VisibilityToggle { get; set; } = true;
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
Type = "password";
|
2020-03-23 09:31:08 +08:00
|
|
|
|
ToggleVisibility(new MouseEventArgs());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void SetClasses()
|
|
|
|
|
{
|
|
|
|
|
base.SetClasses();
|
|
|
|
|
//ant-input-password-large ant-input-affix-wrapper ant-input-affix-wrapper-lg
|
|
|
|
|
ClassMapper
|
2020-05-20 12:57:16 +08:00
|
|
|
|
.If($"{PrefixCls}-password-large", () => Size == InputSize.Large)
|
2021-03-12 17:02:11 +08:00
|
|
|
|
.If($"{PrefixCls}-password-small", () => Size == InputSize.Small)
|
|
|
|
|
.If($"{PrefixCls}-password-rtl", () => RTL);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2021-05-27 18:13:26 +08:00
|
|
|
|
AffixWrapperClass = string.Join(" ", AffixWrapperClass, $"{PrefixCls}-password");
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (VisibilityToggle)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
Suffix = new RenderFragment((builder) =>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2021-05-27 18:13:26 +08:00
|
|
|
|
builder.OpenElement(0, "span");
|
|
|
|
|
builder.AddAttribute(1, "class", $"{PrefixCls}-suffix");
|
|
|
|
|
if (IconRender is null)
|
2021-04-18 16:06:06 +08:00
|
|
|
|
{
|
2021-05-27 18:13:26 +08:00
|
|
|
|
builder.OpenComponent<Icon>(2);
|
|
|
|
|
builder.AddAttribute(3, "class", $"{PrefixCls}-password-icon");
|
|
|
|
|
builder.AddAttribute(4, "type", _eyeIcon);
|
|
|
|
|
builder.AddAttribute(5, "onclick", CallbackFactory.Create<MouseEventArgs>(this, async args =>
|
|
|
|
|
{
|
|
|
|
|
ToggleVisibility(args);
|
2021-04-18 16:06:06 +08:00
|
|
|
|
|
2021-05-27 18:13:26 +08:00
|
|
|
|
await Focus(FocusBehavior.FocusAtLast);
|
|
|
|
|
}));
|
|
|
|
|
builder.CloseComponent();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
builder.AddContent(6, IconRender);
|
|
|
|
|
}
|
2020-03-23 09:31:08 +08:00
|
|
|
|
builder.CloseElement();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 18:13:26 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Focus behavior for InputPassword component with optional behaviors.
|
|
|
|
|
/// Special behavior required for wasm.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="behavior">enum: AntDesign.FocusBehavior</param>
|
|
|
|
|
/// <param name="preventScroll">When true, element receiving focus will not be scrolled to.</param>
|
|
|
|
|
public override async Task Focus(FocusBehavior behavior = FocusBehavior.FocusAtLast, bool preventScroll = false)
|
|
|
|
|
{
|
|
|
|
|
await base.Focus(behavior, preventScroll);
|
|
|
|
|
//delay enforces focus - it counters the js blur that is called on button pressed
|
|
|
|
|
await Task.Delay(5);
|
|
|
|
|
//An ugly solution for InputPassword in wasm to receive focus and keep
|
|
|
|
|
//cursor at the last character. Any improvements are very welcome.
|
|
|
|
|
if (Js.IsBrowser())
|
|
|
|
|
{
|
|
|
|
|
await base.Focus(behavior, preventScroll);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 09:31:08 +08:00
|
|
|
|
private void ToggleVisibility(MouseEventArgs args)
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (VisibilityToggle)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
if (_visible)
|
|
|
|
|
{
|
|
|
|
|
_eyeIcon = "eye";
|
2020-04-23 17:13:56 +08:00
|
|
|
|
Type = "text";
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_eyeIcon = "eye-invisible";
|
2020-04-23 17:13:56 +08:00
|
|
|
|
Type = "password";
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_visible = !_visible;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|