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-05-20 12:57:16 +08:00
|
|
|
|
public partial class Search : Input
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
private bool _isSearching;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public EventCallback<EventArgs> OnSearch { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public OneOf<bool, string> EnterButton { get; set; }
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
|
|
|
|
private int _sequence = 0;
|
|
|
|
|
|
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (EnterButton.Value == null)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
Suffix = new RenderFragment((builder) =>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
builder.OpenComponent<AntIcon>(35);
|
|
|
|
|
builder.AddAttribute(36, "class", $"{PrefixCls}-search-icon");
|
|
|
|
|
builder.AddAttribute(37, "type", "search");
|
2020-05-20 12:57:16 +08:00
|
|
|
|
builder.AddAttribute(38, "onclick", CallbackFactory.Create<MouseEventArgs>(this, HandleSearch));
|
2020-03-23 09:31:08 +08:00
|
|
|
|
builder.CloseComponent();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
AddOnAfter = new RenderFragment((builder) =>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-05-29 12:55:15 +08:00
|
|
|
|
builder.OpenComponent<Button>(_sequence++);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
builder.AddAttribute(_sequence++, "class", $"{PrefixCls}-search-button");
|
|
|
|
|
builder.AddAttribute(_sequence++, "type", "primary");
|
2020-04-23 17:13:56 +08:00
|
|
|
|
builder.AddAttribute(_sequence++, "size", Size);
|
2020-06-07 00:47:18 +08:00
|
|
|
|
|
2020-03-23 09:31:08 +08:00
|
|
|
|
if (_isSearching)
|
|
|
|
|
{
|
|
|
|
|
builder.AddAttribute(_sequence++, "loading", true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-05-20 12:57:16 +08:00
|
|
|
|
var e = new EventCallbackFactory().Create(this, HandleSearch);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
builder.AddAttribute(_sequence++, "onclick", e);
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
builder.AddAttribute(_sequence++, "icon", "search");
|
|
|
|
|
}
|
|
|
|
|
}, 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();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-05-20 12:57:16 +08:00
|
|
|
|
private async void HandleSearch(MouseEventArgs args)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
_isSearching = true;
|
|
|
|
|
StateHasChanged();
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (OnSearch.HasDelegate)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
await OnSearch.InvokeAsync(EventArgs.Empty);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
_isSearching = false;
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|