2020-03-23 09:31:08 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
using OneOf;
|
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
|
|
|
|
{
|
2020-06-27 01:20:52 +08:00
|
|
|
|
public partial class Search : Input<string>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-11-01 22:38:43 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Loading { get; set; }
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-11-01 22:38:43 +08:00
|
|
|
|
public EventCallback<string> OnSearch { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-06-28 10:54:27 +08:00
|
|
|
|
public OneOf<bool, string> EnterButton { get; set; } = false;
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2020-07-30 09:06:56 +08:00
|
|
|
|
protected override bool IgnoreOnChangeAndBlur => OnSearch.HasDelegate;
|
|
|
|
|
|
2020-11-01 22:38:43 +08:00
|
|
|
|
protected override bool EnableOnPressEnter => OnSearch.HasDelegate || OnPressEnter.HasDelegate;
|
|
|
|
|
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
2020-06-28 10:54:27 +08:00
|
|
|
|
if (EnterButton.IsT0 && !EnterButton.AsT0)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-06-28 10:54:27 +08:00
|
|
|
|
Suffix = builder =>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2021-01-21 17:20:10 +08:00
|
|
|
|
builder.OpenComponent<Icon>(1);
|
|
|
|
|
builder.AddAttribute(2, "Class", $"{PrefixCls}-search-icon");
|
2020-11-01 22:38:43 +08:00
|
|
|
|
if (Loading)
|
|
|
|
|
{
|
2021-01-21 17:20:10 +08:00
|
|
|
|
builder.AddAttribute(3, "Type", "loading");
|
2020-11-01 22:38:43 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-01-21 17:20:10 +08:00
|
|
|
|
builder.AddAttribute(4, "Type", "search");
|
2020-11-01 22:38:43 +08:00
|
|
|
|
}
|
2021-01-21 17:20:10 +08:00
|
|
|
|
builder.AddAttribute(5, "OnClick", CallbackFactory.Create<MouseEventArgs>(this, HandleSearch));
|
2020-03-23 09:31:08 +08:00
|
|
|
|
builder.CloseComponent();
|
2020-06-28 10:54:27 +08:00
|
|
|
|
};
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-06-28 10:54:27 +08:00
|
|
|
|
AddOnAfter = builder =>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2021-01-21 17:20:10 +08:00
|
|
|
|
builder.OpenComponent<Button>(6);
|
|
|
|
|
builder.AddAttribute(7, "Class", $"{PrefixCls}-search-button");
|
|
|
|
|
builder.AddAttribute(8, "Type", "primary");
|
|
|
|
|
builder.AddAttribute(9, "Size", Size);
|
2021-03-04 12:50:16 +08:00
|
|
|
|
builder.AddAttribute(10, "Loading", Loading);
|
|
|
|
|
if (!Loading)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2021-01-21 17:20:10 +08:00
|
|
|
|
builder.AddAttribute(11, "OnClick", CallbackFactory.Create<MouseEventArgs>(this, HandleSearch));
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
EnterButton.Switch(boolean =>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (boolean)
|
|
|
|
|
{
|
2021-01-21 17:20:10 +08:00
|
|
|
|
builder.AddAttribute(12, "Icon", "search");
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
}, str =>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2021-01-21 17:20:10 +08:00
|
|
|
|
builder.AddAttribute(13, "ChildContent", new RenderFragment((b) =>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2021-01-21 17:20:10 +08:00
|
|
|
|
b.AddContent(14, str);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}));
|
2020-04-23 17:13:56 +08:00
|
|
|
|
});
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
|
|
|
|
builder.CloseComponent();
|
2020-06-28 10:54:27 +08:00
|
|
|
|
};
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void SetClasses()
|
|
|
|
|
{
|
|
|
|
|
base.SetClasses();
|
|
|
|
|
|
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
|
|
|
|
GroupWrapperClass = string.Join(" ", GroupWrapperClass, $"{PrefixCls}-search-large");
|
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
|
|
|
|
GroupWrapperClass = string.Join(" ", GroupWrapperClass, $"{PrefixCls}-search-small");
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
AffixWrapperClass = string.Join(" ", AffixWrapperClass, $"{PrefixCls}-search");
|
|
|
|
|
GroupWrapperClass = string.Join(" ", GroupWrapperClass, $"{PrefixCls}-search");
|
|
|
|
|
GroupWrapperClass = string.Join(" ", GroupWrapperClass, $"{PrefixCls}-search-enter-button");
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 22:38:43 +08:00
|
|
|
|
private async Task HandleSearch(MouseEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
await SearchAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OnPressEnterAsync()
|
|
|
|
|
{
|
|
|
|
|
await SearchAsync();
|
|
|
|
|
await base.OnPressEnterAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SearchAsync()
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-11-01 22:38:43 +08:00
|
|
|
|
Loading = true;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (OnSearch.HasDelegate)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-11-01 22:38:43 +08:00
|
|
|
|
await OnSearch.InvokeAsync(CurrentValue);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
2020-11-01 22:38:43 +08:00
|
|
|
|
Loading = false;
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|