2020-03-23 09:31:08 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Rendering;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
using System;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
using System.Globalization;
|
2020-03-23 09:31:08 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2020-06-27 01:20:52 +08:00
|
|
|
|
public class Input<TValue> : AntInputComponentBase<TValue>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
protected const string PrefixCls = "ant-input";
|
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
private bool _allowClear;
|
|
|
|
|
protected string AffixWrapperClass { get; set; } = $"{PrefixCls}-affix-wrapper";
|
|
|
|
|
protected string GroupWrapperClass { get; set; } = $"{PrefixCls}-group-wrapper";
|
|
|
|
|
|
|
|
|
|
//protected string ClearIconClass { get; set; }
|
|
|
|
|
protected static readonly EventCallbackFactory CallbackFactory = new EventCallbackFactory();
|
|
|
|
|
|
2020-03-26 10:45:35 +08:00
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public string Type { get; set; } = "text";
|
2020-03-26 10:45:35 +08:00
|
|
|
|
|
2020-03-23 09:31:08 +08:00
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public RenderFragment AddOnBefore { get; set; }
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public RenderFragment AddOnAfter { get; set; }
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public string Placeholder { get; set; }
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-06-02 13:26:27 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool AutoFocus { get; set; }
|
|
|
|
|
|
2020-03-23 09:31:08 +08:00
|
|
|
|
[Parameter]
|
2020-06-27 01:20:52 +08:00
|
|
|
|
public TValue DefaultValue { get; set; }
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public int MaxLength { get; set; } = -1;
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public bool Disabled { get; set; }
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public bool AllowClear { get; set; }
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public RenderFragment Prefix { get; set; }
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public RenderFragment Suffix { get; set; }
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-03-26 10:45:35 +08:00
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public RenderFragment ChildContent { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-06-27 18:24:21 +08:00
|
|
|
|
public EventCallback<TValue> OnChange { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<KeyboardEventArgs> OnPressEnter { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<ChangeEventArgs> OnInput { get; set; }
|
|
|
|
|
|
2020-06-02 13:26:27 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<FocusEventArgs> OnBlur { get; set; }
|
|
|
|
|
|
2020-06-27 01:20:52 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<FocusEventArgs> OnFocus { get; set; }
|
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
public Dictionary<string, object> Attributes { get; set; }
|
2020-03-26 10:45:35 +08:00
|
|
|
|
|
2020-03-23 09:31:08 +08:00
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
2020-06-27 01:20:52 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(DefaultValue?.ToString()) && string.IsNullOrEmpty(Value?.ToString()))
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
Value = DefaultValue;
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetClasses();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void SetClasses()
|
|
|
|
|
{
|
|
|
|
|
ClassMapper.Clear()
|
2020-04-23 17:13:56 +08:00
|
|
|
|
.If($"{PrefixCls}", () => Type != "number")
|
2020-05-20 12:57:16 +08:00
|
|
|
|
.If($"{PrefixCls}-lg", () => Size == InputSize.Large)
|
|
|
|
|
.If($"{PrefixCls}-sm", () => Size == InputSize.Small);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-06-27 01:20:52 +08:00
|
|
|
|
Attributes ??= new Dictionary<string, object>();
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
AffixWrapperClass = $"{PrefixCls}-affix-wrapper";
|
|
|
|
|
GroupWrapperClass = $"{PrefixCls}-group-wrapper";
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (MaxLength >= 0)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
Attributes?.Add("maxlength", MaxLength);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (Disabled)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
// TODO: disable element
|
2020-04-24 18:32:50 +08:00
|
|
|
|
AffixWrapperClass = string.Join(" ", AffixWrapperClass, $"{PrefixCls}-affix-wrapper-disabled");
|
2020-03-23 09:31:08 +08:00
|
|
|
|
ClassMapper.Add($"{PrefixCls}-disabled");
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (AllowClear)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
_allowClear = true;
|
2020-04-24 18:32:50 +08:00
|
|
|
|
//ClearIconClass = $"{PrefixCls}-clear-icon";
|
2020-03-23 09:31:08 +08:00
|
|
|
|
ToggleClearBtn();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 12:57:16 +08:00
|
|
|
|
if (Size == InputSize.Large)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
AffixWrapperClass = string.Join(" ", AffixWrapperClass, $"{PrefixCls}-affix-wrapper-lg");
|
|
|
|
|
GroupWrapperClass = string.Join(" ", GroupWrapperClass, $"{PrefixCls}-group-wrapper-lg");
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
2020-05-20 12:57:16 +08:00
|
|
|
|
else if (Size == InputSize.Small)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
AffixWrapperClass = string.Join(" ", AffixWrapperClass, $"{PrefixCls}-affix-wrapper-sm");
|
|
|
|
|
GroupWrapperClass = string.Join(" ", GroupWrapperClass, $"{PrefixCls}-group-wrapper-sm");
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnParametersSet()
|
|
|
|
|
{
|
|
|
|
|
base.OnParametersSet();
|
|
|
|
|
|
|
|
|
|
SetClasses();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 13:26:27 +08:00
|
|
|
|
public async Task Focus()
|
|
|
|
|
{
|
|
|
|
|
await JsInvokeAsync(JSInteropConstants.focus, Ref);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 09:31:08 +08:00
|
|
|
|
protected async Task OnChangeAsync(ChangeEventArgs args)
|
|
|
|
|
{
|
2020-06-27 01:20:52 +08:00
|
|
|
|
CurrentValueAsString = args.Value?.ToString();
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (OnChange.HasDelegate)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-06-27 18:24:21 +08:00
|
|
|
|
await OnChange.InvokeAsync(Value);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task OnPressEnterAsync(KeyboardEventArgs args)
|
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (args != null && args.Key == "Enter" && OnPressEnter.HasDelegate)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
await OnPressEnter.InvokeAsync(args);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 13:26:27 +08:00
|
|
|
|
private async Task OnBlurAsync(FocusEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (OnBlur.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
await OnBlur.InvokeAsync(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 09:31:08 +08:00
|
|
|
|
private void ToggleClearBtn()
|
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
Suffix = (builder) =>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-06-07 19:41:00 +08:00
|
|
|
|
builder.OpenComponent<Icon>(31);
|
2020-06-01 14:09:28 +08:00
|
|
|
|
builder.AddAttribute(32, "Type", "close-circle");
|
2020-06-27 01:20:52 +08:00
|
|
|
|
if (string.IsNullOrEmpty(Value?.ToString()))
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-06-01 14:09:28 +08:00
|
|
|
|
builder.AddAttribute(33, "Style", "visibility: hidden;");
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-06-01 14:09:28 +08:00
|
|
|
|
builder.AddAttribute(33, "Style", "visibility: visible;");
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
2020-04-24 18:32:50 +08:00
|
|
|
|
builder.AddAttribute(34, "onclick", CallbackFactory.Create<MouseEventArgs>(this, (args) =>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-06-27 01:20:52 +08:00
|
|
|
|
Value = default;//string.Empty;
|
2020-06-29 00:26:11 +08:00
|
|
|
|
if (OnChange.HasDelegate)
|
|
|
|
|
OnChange.InvokeAsync(Value);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
ToggleClearBtn();
|
|
|
|
|
}));
|
|
|
|
|
builder.CloseComponent();
|
2020-04-24 18:32:50 +08:00
|
|
|
|
};
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 13:26:27 +08:00
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
|
|
|
{
|
|
|
|
|
await base.OnAfterRenderAsync(firstRender);
|
|
|
|
|
|
|
|
|
|
if (this.AutoFocus)
|
|
|
|
|
{
|
|
|
|
|
await this.Focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 09:31:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when user add/remove content
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="args"></param>
|
|
|
|
|
/// <returns></returns>
|
2020-04-24 18:32:50 +08:00
|
|
|
|
protected virtual async void OnInputAsync(ChangeEventArgs args)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-06-27 01:20:52 +08:00
|
|
|
|
bool flag = !(!string.IsNullOrEmpty(Value?.ToString()) && args != null && !string.IsNullOrEmpty(args.Value.ToString()));
|
2020-04-24 18:32:50 +08:00
|
|
|
|
|
2020-07-06 14:33:28 +08:00
|
|
|
|
CurrentValueAsString = args?.Value?.ToString();
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
2020-03-23 09:31:08 +08:00
|
|
|
|
if (_allowClear && flag)
|
|
|
|
|
{
|
|
|
|
|
ToggleClearBtn();
|
|
|
|
|
}
|
2020-03-26 10:45:35 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (OnInput.HasDelegate)
|
2020-03-26 10:45:35 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
await OnInput.InvokeAsync(args);
|
2020-03-26 10:45:35 +08:00
|
|
|
|
}
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void BuildRenderTree(RenderTreeBuilder builder)
|
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (builder != null)
|
|
|
|
|
{
|
|
|
|
|
base.BuildRenderTree(builder);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
string container = "input";
|
2020-06-02 13:26:27 +08:00
|
|
|
|
int i = 0;
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (AddOnBefore != null || AddOnAfter != null)
|
|
|
|
|
{
|
|
|
|
|
container = "groupWrapper";
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.OpenElement(i++, "span");
|
|
|
|
|
builder.AddAttribute(i++, "class", GroupWrapperClass);
|
|
|
|
|
builder.AddAttribute(i++, "style", Style);
|
|
|
|
|
builder.OpenElement(i++, "span");
|
|
|
|
|
builder.AddAttribute(i++, "class", $"{PrefixCls}-wrapper {PrefixCls}-group");
|
2020-04-24 18:32:50 +08:00
|
|
|
|
}
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (AddOnBefore != null)
|
|
|
|
|
{
|
|
|
|
|
// addOnBefore
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.OpenElement(i++, "span");
|
|
|
|
|
builder.AddAttribute(i++, "class", $"{PrefixCls}-group-addon");
|
|
|
|
|
builder.AddContent(i++, AddOnBefore);
|
2020-04-24 18:32:50 +08:00
|
|
|
|
builder.CloseElement();
|
|
|
|
|
}
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (Prefix != null || Suffix != null)
|
|
|
|
|
{
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.OpenElement(i++, "span");
|
|
|
|
|
builder.AddAttribute(i++, "class", AffixWrapperClass);
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (container == "input")
|
|
|
|
|
{
|
|
|
|
|
container = "affixWrapper";
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.AddAttribute(i++, "style", Style);
|
2020-04-24 18:32:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Prefix != null)
|
|
|
|
|
{
|
|
|
|
|
// prefix
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.OpenElement(i++, "span");
|
|
|
|
|
builder.AddAttribute(i++, "class", $"{PrefixCls}-prefix");
|
|
|
|
|
builder.AddContent(i++, Prefix);
|
2020-04-24 18:32:50 +08:00
|
|
|
|
builder.CloseElement();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// input
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.OpenElement(i++, "input");
|
|
|
|
|
builder.AddAttribute(i++, "class", ClassMapper.Class);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
if (container == "input")
|
|
|
|
|
{
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.AddAttribute(i++, "style", Style);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (Attributes != null)
|
|
|
|
|
{
|
2020-06-27 01:20:52 +08:00
|
|
|
|
builder.AddMultipleAttributes(i++, Attributes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (AdditionalAttributes != null)
|
|
|
|
|
{
|
|
|
|
|
builder.AddMultipleAttributes(i++, AdditionalAttributes);
|
2020-04-24 18:32:50 +08:00
|
|
|
|
}
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.AddAttribute(i++, "Id", Id);
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (Type != "number")
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.AddAttribute(i++, "type", Type);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.AddAttribute(i++, "placeholder", Placeholder);
|
2020-06-27 01:20:52 +08:00
|
|
|
|
builder.AddAttribute(i++, "value", CurrentValueAsString);
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.AddAttribute(i++, "onchange", CallbackFactory.Create(this, OnChangeAsync));
|
|
|
|
|
builder.AddAttribute(i++, "onkeypress", CallbackFactory.Create(this, OnPressEnterAsync));
|
|
|
|
|
builder.AddAttribute(i++, "oninput", CallbackFactory.Create(this, OnInputAsync));
|
|
|
|
|
builder.AddAttribute(i++, "onblur", CallbackFactory.Create(this, OnBlurAsync));
|
2020-06-27 01:20:52 +08:00
|
|
|
|
builder.AddAttribute(i++, "onfocus", CallbackFactory.Create(this, OnFocus));
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.AddElementReferenceCapture(i++, r => Ref = r);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
builder.CloseElement();
|
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (Suffix != null)
|
|
|
|
|
{
|
|
|
|
|
// suffix
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.OpenElement(i++, "span");
|
|
|
|
|
builder.AddAttribute(i++, "class", $"{PrefixCls}-suffix");
|
|
|
|
|
builder.AddContent(i++, Suffix);
|
2020-04-24 18:32:50 +08:00
|
|
|
|
builder.CloseElement();
|
|
|
|
|
}
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (Prefix != null || Suffix != null)
|
|
|
|
|
{
|
|
|
|
|
builder.CloseElement();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (AddOnAfter != null)
|
|
|
|
|
{
|
|
|
|
|
// addOnAfter
|
2020-06-02 13:26:27 +08:00
|
|
|
|
builder.OpenElement(i++, "span");
|
|
|
|
|
builder.AddAttribute(i++, "class", $"{PrefixCls}-group-addon");
|
|
|
|
|
builder.AddContent(i++, AddOnAfter);
|
2020-04-24 18:32:50 +08:00
|
|
|
|
builder.CloseElement();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (AddOnBefore != null || AddOnAfter != null)
|
|
|
|
|
{
|
|
|
|
|
builder.CloseElement();
|
|
|
|
|
builder.CloseElement();
|
|
|
|
|
}
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|