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;
|
2021-05-27 18:13:26 +08:00
|
|
|
|
using System;
|
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
|
|
|
|
{
|
2021-05-27 18:13:26 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Search input is rendered with suffix search icon, not as a button.
|
|
|
|
|
/// Will be ignored when EnterButton != false
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool ClassicSearchIcon { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether to show an enter button after input. This property conflicts with the AddonAfter property
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public OneOf<bool, string> EnterButton { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Search box with loading
|
|
|
|
|
/// </summary>
|
2020-11-01 22:38:43 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Loading { get; set; }
|
2020-03-23 09:31:08 +08:00
|
|
|
|
|
2021-05-27 18:13:26 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The callback function triggered when you click on the search-icon, the clear-icon or press the Enter key
|
|
|
|
|
/// </summary>
|
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
|
|
|
|
|
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
|
|
|
|
{
|
2021-05-27 18:13:26 +08:00
|
|
|
|
if (ClassicSearchIcon)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2021-05-27 18:13:26 +08:00
|
|
|
|
Suffix = builder =>
|
2020-11-01 22:38:43 +08:00
|
|
|
|
{
|
2021-05-27 18:13:26 +08:00
|
|
|
|
builder.OpenComponent<Icon>(1);
|
|
|
|
|
builder.AddAttribute(2, "Class", $"{PrefixCls}-search-icon");
|
|
|
|
|
if (Loading)
|
|
|
|
|
{
|
|
|
|
|
builder.AddAttribute(3, "Type", "loading");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
builder.AddAttribute(4, "Type", "search");
|
|
|
|
|
}
|
|
|
|
|
builder.AddAttribute(5, "OnClick", CallbackFactory.Create<MouseEventArgs>(this, HandleSearch));
|
|
|
|
|
builder.CloseComponent();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
AddOnAfter = builder =>
|
2020-11-01 22:38:43 +08:00
|
|
|
|
{
|
2021-05-27 18:13:26 +08:00
|
|
|
|
builder.OpenComponent<Button>(6);
|
|
|
|
|
builder.AddAttribute(7, "Class", $"{PrefixCls}-search-button");
|
|
|
|
|
builder.AddAttribute(8, "Type", "default");
|
|
|
|
|
builder.AddAttribute(9, "Size", Size);
|
|
|
|
|
builder.AddAttribute(10, "Loading", Loading);
|
|
|
|
|
if (!Loading)
|
|
|
|
|
{
|
|
|
|
|
builder.AddAttribute(12, "OnClick", CallbackFactory.Create<MouseEventArgs>(this, HandleSearch));
|
|
|
|
|
}
|
|
|
|
|
builder.AddAttribute(13, "Icon", "search");
|
|
|
|
|
|
|
|
|
|
builder.CloseComponent();
|
|
|
|
|
};
|
|
|
|
|
}
|
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-05-27 18:13:26 +08:00
|
|
|
|
builder.OpenComponent<Button>(11);
|
|
|
|
|
builder.AddAttribute(12, "Class", $"{PrefixCls}-search-button");
|
|
|
|
|
builder.AddAttribute(13, "Type", "primary");
|
|
|
|
|
builder.AddAttribute(14, "Size", Size);
|
|
|
|
|
builder.AddAttribute(15, "Loading", Loading);
|
2021-03-04 12:50:16 +08:00
|
|
|
|
if (!Loading)
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2021-05-27 18:13:26 +08:00
|
|
|
|
builder.AddAttribute(16, "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-05-27 18:13:26 +08:00
|
|
|
|
builder.AddAttribute(17, "Icon", "search");
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
}, str =>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2021-05-27 18:13:26 +08:00
|
|
|
|
builder.AddAttribute(18, "ChildContent", new RenderFragment((b) =>
|
2020-03-23 09:31:08 +08:00
|
|
|
|
{
|
2021-05-27 18:13:26 +08:00
|
|
|
|
b.AddContent(19, 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
|
|
|
|
}
|