ant-design-blazor/components/input/InputPassword.razor.cs
Andrzej Bakun 2d693740c6 fix(module: input): fix & feat & docs & tests: Input general fixes (#1530)
* docs(module:input): api correction & comments

* fix(module:input): clear icon in sync with antD

* fix(module:input): add parameter bordered

* fix(module:textarea): add parameter ShowCounter

* fix(module:input): Focus with behaviors

* docs: add missing docs, rearrange order

* fix(module:search): update to match new standard look

doc(module:search): add API & demos

* fix(module:inputgroup): override style to remove whitespace\
docs: same as react

* fix(module:inputpassword): support custom icon
docs: same as react

* fix(module:input): add blur method
docs: add blur method

* fix(module:input): add readonly parameter

* tests

* fix

* fix(module:input): sequence correction

* Update components/core/Base/AntComponentBase.cs

Co-authored-by: James Yeung <shunjiey@hotmail.com>

* review fixes

* fix: clean-up

* tests: fix missing mock

* fix: focus

* fix(module:input): bind clear button to _inputValue

* tests: clear button show/hide

* tests: package update

Co-authored-by: James Yeung <shunjiey@hotmail.com>
2021-05-27 18:13:26 +08:00

129 lines
4.2 KiB
C#

using System;
using System.Threading.Tasks;
using AntDesign.Core.Extensions;
using AntDesign.JsInterop;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
namespace AntDesign
{
public partial class InputPassword : Input<string>
{
private bool _visible = false;
private string _eyeIcon;
/// <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>
[Parameter]
public bool VisibilityToggle { get; set; } = true;
protected override void OnInitialized()
{
base.OnInitialized();
Type = "password";
ToggleVisibility(new MouseEventArgs());
}
protected override void SetClasses()
{
base.SetClasses();
//ant-input-password-large ant-input-affix-wrapper ant-input-affix-wrapper-lg
ClassMapper
.If($"{PrefixCls}-password-large", () => Size == InputSize.Large)
.If($"{PrefixCls}-password-small", () => Size == InputSize.Small)
.If($"{PrefixCls}-password-rtl", () => RTL);
AffixWrapperClass = string.Join(" ", AffixWrapperClass, $"{PrefixCls}-password");
if (VisibilityToggle)
{
Suffix = new RenderFragment((builder) =>
{
builder.OpenElement(0, "span");
builder.AddAttribute(1, "class", $"{PrefixCls}-suffix");
if (IconRender is null)
{
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);
await Focus(FocusBehavior.FocusAtLast);
}));
builder.CloseComponent();
}
else
{
builder.AddContent(6, IconRender);
}
builder.CloseElement();
});
}
}
/// <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);
}
}
private void ToggleVisibility(MouseEventArgs args)
{
if (VisibilityToggle)
{
if (_visible)
{
_eyeIcon = "eye";
Type = "text";
}
else
{
_eyeIcon = "eye-invisible";
Type = "password";
}
_visible = !_visible;
}
}
}
}