ant-design-blazor/components/input/Input.cs

341 lines
11 KiB
C#
Raw Normal View History

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