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;
|
|
|
|
|
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
|
|
|
|
private int _sequence = 0;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2020-06-28 10:54:27 +08:00
|
|
|
|
var i = 0;
|
|
|
|
|
builder.OpenComponent<Icon>(i++);
|
|
|
|
|
builder.AddAttribute(i++, "Class", $"{PrefixCls}-search-icon");
|
2020-11-01 22:38:43 +08:00
|
|
|
|
if (Loading)
|
|
|
|
|
{
|
|
|
|
|
builder.AddAttribute(i++, "Type", "loading");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
builder.AddAttribute(i++, "Type", "search");
|
|
|
|
|
}
|
2020-06-28 10:54:27 +08:00
|
|
|
|
builder.AddAttribute(i++, "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
|
|
|
|
{
|
2020-05-29 12:55:15 +08:00
|
|
|
|
builder.OpenComponent<Button>(_sequence++);
|
2020-06-28 10:54:27 +08:00
|
|
|
|
builder.AddAttribute(_sequence++, "Class", $"{PrefixCls}-search-button");
|
|
|
|
|
builder.AddAttribute(_sequence++, "Type", "primary");
|
|
|
|
|
builder.AddAttribute(_sequence++, "Size", Size);
|
2020-06-07 00:47:18 +08:00
|
|
|
|
|
2020-11-01 22:38:43 +08:00
|
|
|
|
if (Loading)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-06-28 10:54:27 +08:00
|
|
|
|
builder.AddAttribute(_sequence++, "Loading", true);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-01 22:38:43 +08:00
|
|
|
|
builder.AddAttribute(_sequence++, "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)
|
|
|
|
|
{
|
2020-06-28 10:54:27 +08:00
|
|
|
|
builder.AddAttribute(_sequence++, "Icon", "search");
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
}, str =>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
builder.AddAttribute(_sequence++, "ChildContent", new RenderFragment((b) =>
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
b.AddContent(_sequence++, 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
|
|
|
|
}
|