2021-04-18 16:06:06 +08:00
|
|
|
|
using System;
|
|
|
|
|
using AntDesign.Core.Extensions;
|
|
|
|
|
using AntDesign.JsInterop;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
[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
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
AffixWrapperClass = string.Join(" ", AffixWrapperClass, $"{PrefixCls}-password");
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
builder.OpenElement(i++, "span");
|
|
|
|
|
builder.AddAttribute(i++, "class", $"{PrefixCls}-suffix");
|
2020-06-07 19:41:00 +08:00
|
|
|
|
builder.OpenComponent<Icon>(i++);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
builder.AddAttribute(i++, "class", $"{PrefixCls}-password-icon");
|
|
|
|
|
builder.AddAttribute(i++, "type", _eyeIcon);
|
2021-04-18 16:06:06 +08:00
|
|
|
|
builder.AddAttribute(i++, "onclick", CallbackFactory.Create<MouseEventArgs>(this, async args =>
|
|
|
|
|
{
|
|
|
|
|
var element = await JsInvokeAsync<HtmlElement>(JSInteropConstants.GetDomInfo, Ref);
|
|
|
|
|
|
|
|
|
|
IsFocused = true;
|
|
|
|
|
await this.FocusAsync(Ref);
|
|
|
|
|
|
|
|
|
|
ToggleVisibility(args);
|
|
|
|
|
|
|
|
|
|
if (element.SelectionStart != 0)
|
|
|
|
|
await Js.SetSelectionStartAsync(Ref, element.SelectionStart);
|
|
|
|
|
}));
|
2020-03-23 09:31:08 +08:00
|
|
|
|
builder.CloseComponent();
|
|
|
|
|
builder.CloseElement();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|