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
|
|
|
|
{
|
|
|
|
|
private bool _isSearching;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public EventCallback<EventArgs> OnSearch { get; set; }
|
|
|
|
|
|
|
|
|
|
[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-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");
|
|
|
|
|
builder.AddAttribute(i++, "Type", "search");
|
|
|
|
|
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-03-23 09:31:08 +08:00
|
|
|
|
if (_isSearching)
|
|
|
|
|
{
|
2020-06-28 10:54:27 +08:00
|
|
|
|
builder.AddAttribute(_sequence++, "Loading", true);
|
2020-03-23 09:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-05-20 12:57:16 +08:00
|
|
|
|
var e = new EventCallbackFactory().Create(this, HandleSearch);
|
2020-06-28 10:54:27 +08:00
|
|
|
|
builder.AddAttribute(_sequence++, "OnClick", e);
|
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-05-20 12:57:16 +08:00
|
|
|
|
private async void HandleSearch(MouseEventArgs args)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
_isSearching = true;
|
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
|
|
|
|
}
|