2020-06-27 18:24:21 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-09-09 00:52:14 +08:00
|
|
|
|
using System.Data;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-08-27 16:38:44 +08:00
|
|
|
|
using AntDesign.Core.JsInterop.ObservableApi;
|
2020-11-13 00:22:41 +08:00
|
|
|
|
using AntDesign.Internal;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
using AntDesign.JsInterop;
|
2020-09-09 00:52:14 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
2020-08-26 22:37:08 +08:00
|
|
|
|
using OneOf;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
2020-11-02 23:05:17 +08:00
|
|
|
|
public partial class AutoComplete<TOption> : AntInputComponentBase<string>, IAutoCompleteRef
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2020-08-26 22:37:08 +08:00
|
|
|
|
#region Parameters
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-10-09 16:32:40 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string Placeholder { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Disabled { get; set; }
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool DefaultActiveFirstOption { get; set; } = true;
|
2021-04-17 22:14:01 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Backfill { get; set; } = false;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2023-01-06 17:01:09 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public int DebounceMilliseconds { get; set; } = 250;
|
|
|
|
|
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 列表对象集合
|
2021-08-27 00:44:47 +08:00
|
|
|
|
/// List object collection
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
private List<AutoCompleteOption> AutoCompleteOptions { get; set; } = new List<AutoCompleteOption>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 列表数据集合
|
2021-08-27 00:44:47 +08:00
|
|
|
|
/// List data collection
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
private List<AutoCompleteDataItem<TOption>> _optionDataItems = new List<AutoCompleteDataItem<TOption>>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 列表绑定数据源集合
|
2021-08-27 00:44:47 +08:00
|
|
|
|
/// List bound data source collection
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
private IEnumerable<TOption> _options;
|
2021-04-17 22:14:01 +08:00
|
|
|
|
|
2020-11-02 23:05:17 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public IEnumerable<TOption> Options
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _options;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_options = value;
|
|
|
|
|
_optionDataItems = _options?.Select(x => new AutoCompleteDataItem<TOption>(x, x.ToString())).ToList() ?? new List<AutoCompleteDataItem<TOption>>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 绑定列表数据项格式的数据源
|
2021-08-27 00:44:47 +08:00
|
|
|
|
/// Bind the data source of the list data item format
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// </summary>
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
2020-11-02 23:05:17 +08:00
|
|
|
|
public IEnumerable<AutoCompleteDataItem<TOption>> OptionDataItems
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _optionDataItems;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_optionDataItems = value.ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<AutoCompleteOption> OnSelectionChange { get; set; }
|
2021-04-17 22:14:01 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<AutoCompleteOption> OnActiveChange { get; set; }
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<ChangeEventArgs> OnInput { get; set; }
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-10-09 16:32:40 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<bool> OnPanelVisibleChange { get; set; }
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment ChildContent { get; set; }
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 选项模板
|
2021-08-27 00:44:47 +08:00
|
|
|
|
/// Option template
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment<AutoCompleteDataItem<TOption>> OptionTemplate { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 格式化选项,可以自定义显示格式
|
2021-08-27 00:44:47 +08:00
|
|
|
|
/// Formatting options, you can customize the display format
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<AutoCompleteDataItem<TOption>, string> OptionFormat { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 所有选项模板
|
2021-08-27 00:44:47 +08:00
|
|
|
|
/// All option templates
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// </summary>
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
2020-11-02 23:05:17 +08:00
|
|
|
|
public RenderFragment OverlayTemplate { get; set; }
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 对比,用于两个对象比较是否相同
|
2021-08-27 00:44:47 +08:00
|
|
|
|
/// Contrast, used to compare whether two objects are the same
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// </summary>
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<object, object, bool> CompareWith { get; set; } = (o1, o2) => o1?.ToString() == o2?.ToString();
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 过滤表达式
|
2021-08-27 00:44:47 +08:00
|
|
|
|
/// Filter expression
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<AutoCompleteDataItem<TOption>, string, bool> FilterExpression { get; set; } = (option, value) => string.IsNullOrEmpty(value) ? true : option.Label.Contains(value, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 允许过滤
|
2021-08-27 00:44:47 +08:00
|
|
|
|
/// Allow filtering
|
2020-11-02 23:05:17 +08:00
|
|
|
|
/// </summary>
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
2020-11-02 23:05:17 +08:00
|
|
|
|
public bool AllowFilter { get; set; } = true;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public OneOf<int?, string> Width { get; set; }
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string OverlayClassName { get; set; }
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string OverlayStyle { get; set; }
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-09-17 10:37:47 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string PopupContainerSelector { get; set; } = "body";
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
#endregion Parameters
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2021-01-21 17:20:10 +08:00
|
|
|
|
//private ElementReference _divRef;
|
2020-11-13 00:22:41 +08:00
|
|
|
|
private OverlayTrigger _overlayTrigger;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-11-02 23:05:17 +08:00
|
|
|
|
public object SelectedValue { get; set; }
|
2021-04-17 22:14:01 +08:00
|
|
|
|
|
2020-06-27 18:24:21 +08:00
|
|
|
|
/// <summary>
|
2020-08-26 22:37:08 +08:00
|
|
|
|
/// 选择的项
|
2020-06-27 18:24:21 +08:00
|
|
|
|
/// </summary>
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public AutoCompleteOption SelectedItem { get; set; }
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-08-26 22:37:08 +08:00
|
|
|
|
/// 高亮的项目
|
2020-06-27 18:24:21 +08:00
|
|
|
|
/// </summary>
|
2020-11-02 23:05:17 +08:00
|
|
|
|
public object ActiveValue { get; set; }
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2021-09-10 19:06:50 +08:00
|
|
|
|
/// <summary>
|
2023-01-06 17:01:09 +08:00
|
|
|
|
/// Overlay adjustment strategy (when for example browser resize is happening). Check
|
2021-09-10 19:06:50 +08:00
|
|
|
|
/// enum for details.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public TriggerBoundaryAdjustMode BoundaryAdjustMode { get; set; } = TriggerBoundaryAdjustMode.InView;
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool ShowPanel { get; set; } = false;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2023-08-27 16:38:44 +08:00
|
|
|
|
[Inject] private IDomEventListener DomEventListener { get; set; }
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
private bool _isOptionsZero = true;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
private IAutoCompleteInput _inputComponent;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2023-01-08 00:41:13 +08:00
|
|
|
|
private IList<AutoCompleteDataItem<TOption>> _filteredOptions;
|
|
|
|
|
|
2023-08-27 16:38:44 +08:00
|
|
|
|
private string _minWidth = "";
|
|
|
|
|
private bool _parPanelVisible = false;
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
public void SetInputComponent(IAutoCompleteInput input)
|
2020-07-31 12:43:46 +08:00
|
|
|
|
{
|
2020-08-26 22:37:08 +08:00
|
|
|
|
_inputComponent = input;
|
2020-07-31 12:43:46 +08:00
|
|
|
|
}
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2021-08-27 00:44:47 +08:00
|
|
|
|
#region 子控件触发事件 / Child controls trigger events
|
2021-04-17 22:14:01 +08:00
|
|
|
|
|
2023-01-06 17:01:09 +08:00
|
|
|
|
public Task InputFocus(FocusEventArgs e)
|
2020-08-26 22:37:08 +08:00
|
|
|
|
{
|
2020-11-13 00:22:41 +08:00
|
|
|
|
if (!_isOptionsZero)
|
|
|
|
|
{
|
|
|
|
|
this.OpenPanel();
|
|
|
|
|
}
|
2023-01-06 17:01:09 +08:00
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2020-08-26 22:37:08 +08:00
|
|
|
|
}
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
public async Task InputInput(ChangeEventArgs args)
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2020-08-26 22:37:08 +08:00
|
|
|
|
if (OnInput.HasDelegate) await OnInput.InvokeAsync(args);
|
2023-01-06 17:01:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task InputValueChange(string value)
|
|
|
|
|
{
|
|
|
|
|
SelectedValue = value;
|
|
|
|
|
ResetActiveItem();
|
|
|
|
|
return Task.CompletedTask;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
public async Task InputKeyDown(KeyboardEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
var key = args.Key;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
if (this.ShowPanel)
|
|
|
|
|
{
|
|
|
|
|
if (key == "Escape" || key == "Tab")
|
|
|
|
|
{
|
|
|
|
|
this.ClosePanel();
|
|
|
|
|
}
|
|
|
|
|
else if (key == "Enter" && this.ActiveValue != null)
|
|
|
|
|
{
|
|
|
|
|
await SetSelectedItem(GetActiveItem());
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-13 00:22:41 +08:00
|
|
|
|
else if (!_isOptionsZero)
|
2020-08-26 22:37:08 +08:00
|
|
|
|
{
|
|
|
|
|
this.OpenPanel();
|
|
|
|
|
}
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
if (key == "ArrowUp")
|
|
|
|
|
{
|
|
|
|
|
this.SetPreviousItemActive();
|
|
|
|
|
}
|
|
|
|
|
else if (key == "ArrowDown")
|
2020-07-31 12:43:46 +08:00
|
|
|
|
{
|
2020-08-26 22:37:08 +08:00
|
|
|
|
this.SetNextItemActive();
|
2020-07-31 12:43:46 +08:00
|
|
|
|
}
|
2020-08-26 22:37:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 00:44:47 +08:00
|
|
|
|
#endregion 子控件触发事件 / Child controls trigger events
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
protected override void OnParametersSet()
|
|
|
|
|
{
|
|
|
|
|
base.OnParametersSet();
|
2023-01-08 00:41:13 +08:00
|
|
|
|
UpdateFilteredOptions();
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
protected override async Task OnFirstAfterRenderAsync()
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2020-08-26 22:37:08 +08:00
|
|
|
|
await SetOverlayWidth();
|
2023-08-27 16:38:44 +08:00
|
|
|
|
await DomEventListener.AddResizeObserver(_overlayTrigger.RefBack.Current, UpdateWidth);
|
2020-08-26 22:37:08 +08:00
|
|
|
|
await base.OnFirstAfterRenderAsync();
|
|
|
|
|
}
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
public void AddOption(AutoCompleteOption option)
|
|
|
|
|
{
|
2020-11-02 23:05:17 +08:00
|
|
|
|
AutoCompleteOptions.Add(option);
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
public void RemoveOption(AutoCompleteOption option)
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2020-11-02 23:05:17 +08:00
|
|
|
|
if (AutoCompleteOptions?.Contains(option) == true)
|
|
|
|
|
AutoCompleteOptions?.Remove(option);
|
2020-08-26 22:37:08 +08:00
|
|
|
|
}
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-11-02 23:05:17 +08:00
|
|
|
|
public IList<AutoCompleteDataItem<TOption>> GetOptionItems()
|
2020-08-26 22:37:08 +08:00
|
|
|
|
{
|
2020-11-02 23:05:17 +08:00
|
|
|
|
if (_optionDataItems != null)
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2020-11-02 23:05:17 +08:00
|
|
|
|
if (FilterExpression != null && AllowFilter == true && SelectedValue != null)
|
|
|
|
|
return _optionDataItems.Where(x => FilterExpression(x, SelectedValue?.ToString())).ToList();
|
2020-08-26 22:37:08 +08:00
|
|
|
|
else
|
2020-11-02 23:05:17 +08:00
|
|
|
|
return _optionDataItems;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
2020-08-26 22:37:08 +08:00
|
|
|
|
else
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2020-11-02 23:05:17 +08:00
|
|
|
|
return new List<AutoCompleteDataItem<TOption>>();
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 打开面板
|
2021-08-27 00:44:47 +08:00
|
|
|
|
/// Open panel
|
2020-08-26 22:37:08 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public void OpenPanel()
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2020-08-26 22:37:08 +08:00
|
|
|
|
if (this.ShowPanel == false)
|
|
|
|
|
{
|
|
|
|
|
this.ShowPanel = true;
|
2020-11-13 00:22:41 +08:00
|
|
|
|
|
|
|
|
|
_overlayTrigger.Show();
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
ResetActiveItem();
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 关闭面板
|
2021-08-27 00:44:47 +08:00
|
|
|
|
/// Close panel
|
2020-08-26 22:37:08 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public void ClosePanel()
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2020-08-26 22:37:08 +08:00
|
|
|
|
if (this.ShowPanel == true)
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2020-08-26 22:37:08 +08:00
|
|
|
|
this.ShowPanel = false;
|
2020-11-13 00:22:41 +08:00
|
|
|
|
|
|
|
|
|
_overlayTrigger.Close();
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
StateHasChanged();
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
2020-08-26 22:37:08 +08:00
|
|
|
|
}
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
public AutoCompleteOption GetActiveItem()
|
|
|
|
|
{
|
2020-11-02 23:05:17 +08:00
|
|
|
|
return AutoCompleteOptions.FirstOrDefault(x => CompareWith(x.Value, this.ActiveValue));
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
//设置高亮的对象
|
2021-08-27 00:44:47 +08:00
|
|
|
|
//Set the highlighted object
|
2020-08-26 22:37:08 +08:00
|
|
|
|
public void SetActiveItem(AutoCompleteOption item)
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2020-11-02 23:05:17 +08:00
|
|
|
|
this.ActiveValue = item == null ? default(TOption) : item.Value;
|
2020-08-26 22:37:08 +08:00
|
|
|
|
if (OnActiveChange.HasDelegate) OnActiveChange.InvokeAsync(item);
|
|
|
|
|
StateHasChanged();
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
//设置下一个激活
|
2021-08-27 00:44:47 +08:00
|
|
|
|
//Set the next activation
|
2020-08-26 22:37:08 +08:00
|
|
|
|
public void SetNextItemActive()
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2020-11-02 23:05:17 +08:00
|
|
|
|
var opts = AutoCompleteOptions.Where(x => x.Disabled == false).ToList();
|
2020-08-26 22:37:08 +08:00
|
|
|
|
var nextItem = opts.IndexOf(GetActiveItem());
|
|
|
|
|
if (nextItem == -1 || nextItem == opts.Count - 1)
|
|
|
|
|
SetActiveItem(opts.FirstOrDefault());
|
|
|
|
|
else
|
|
|
|
|
SetActiveItem(opts[nextItem + 1]);
|
|
|
|
|
|
|
|
|
|
if (Backfill)
|
|
|
|
|
_inputComponent.SetValue(this.ActiveValue);
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
//设置上一个激活
|
2021-08-27 00:44:47 +08:00
|
|
|
|
//Set last activation
|
2020-08-26 22:37:08 +08:00
|
|
|
|
public void SetPreviousItemActive()
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2020-11-02 23:05:17 +08:00
|
|
|
|
var opts = AutoCompleteOptions.Where(x => x.Disabled == false).ToList();
|
2020-08-26 22:37:08 +08:00
|
|
|
|
var nextItem = opts.IndexOf(GetActiveItem());
|
|
|
|
|
if (nextItem == -1 || nextItem == 0)
|
|
|
|
|
SetActiveItem(opts.LastOrDefault());
|
|
|
|
|
else
|
|
|
|
|
SetActiveItem(opts[nextItem - 1]);
|
|
|
|
|
|
|
|
|
|
if (Backfill)
|
|
|
|
|
_inputComponent.SetValue(this.ActiveValue);
|
|
|
|
|
}
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2023-01-08 00:41:13 +08:00
|
|
|
|
public void UpdateFilteredOptions()
|
|
|
|
|
{
|
|
|
|
|
_filteredOptions = GetOptionItems();
|
|
|
|
|
_isOptionsZero = _filteredOptions.Count == 0 && Options != null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
private void ResetActiveItem()
|
|
|
|
|
{
|
2023-01-08 00:41:13 +08:00
|
|
|
|
var items = _filteredOptions;
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
if (items.Any(x => CompareWith(x.Value, this.ActiveValue)) == false)
|
2020-11-13 00:22:41 +08:00
|
|
|
|
{
|
|
|
|
|
// 如果当前激活项找在列表中不存在,那么我们需要做一些处理
|
2021-08-27 00:44:47 +08:00
|
|
|
|
// If the current activation item does not exist in the list, then we need to do some processing
|
2020-08-26 22:37:08 +08:00
|
|
|
|
if (items.Any(x => CompareWith(x.Value, this.SelectedValue)))
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2020-08-26 22:37:08 +08:00
|
|
|
|
this.ActiveValue = this.SelectedValue;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
2020-08-26 22:37:08 +08:00
|
|
|
|
else if (DefaultActiveFirstOption == true && items.Count > 0)
|
2020-07-31 12:43:46 +08:00
|
|
|
|
{
|
2023-01-06 17:01:09 +08:00
|
|
|
|
this.ActiveValue = items.FirstOrDefault()?.Value.ToString();
|
2020-07-31 12:43:46 +08:00
|
|
|
|
}
|
2020-08-26 22:37:08 +08:00
|
|
|
|
else
|
2020-07-31 12:43:46 +08:00
|
|
|
|
{
|
2020-08-26 22:37:08 +08:00
|
|
|
|
this.ActiveValue = null;
|
2020-07-31 12:43:46 +08:00
|
|
|
|
}
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
2020-11-13 00:22:41 +08:00
|
|
|
|
|
2022-10-13 14:46:43 +08:00
|
|
|
|
if (_overlayTrigger != null)
|
2020-11-13 00:22:41 +08:00
|
|
|
|
{
|
|
|
|
|
// if options count == 0 then close overlay
|
|
|
|
|
if (_isOptionsZero && _overlayTrigger.IsOverlayShow())
|
|
|
|
|
{
|
|
|
|
|
_overlayTrigger.Close();
|
|
|
|
|
}
|
|
|
|
|
// if options count > 0 then open overlay
|
|
|
|
|
else if (!_isOptionsZero && !_overlayTrigger.IsOverlayShow())
|
|
|
|
|
{
|
|
|
|
|
_overlayTrigger.Show();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
public async Task SetSelectedItem(AutoCompleteOption item)
|
|
|
|
|
{
|
|
|
|
|
if (item != null)
|
|
|
|
|
{
|
2020-11-02 23:05:17 +08:00
|
|
|
|
this.SelectedValue = item.Value;
|
2020-08-26 22:37:08 +08:00
|
|
|
|
this.SelectedItem = item;
|
2023-05-08 07:19:34 +08:00
|
|
|
|
_inputComponent?.SetValue(this.SelectedItem.Label);
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
if (OnSelectionChange.HasDelegate) await OnSelectionChange.InvokeAsync(this.SelectedItem);
|
|
|
|
|
}
|
|
|
|
|
this.ClosePanel();
|
|
|
|
|
}
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-10-09 16:32:40 +08:00
|
|
|
|
private async void OnOverlayTriggerVisibleChange(bool visible)
|
2020-09-27 23:05:23 +08:00
|
|
|
|
{
|
2020-10-09 16:32:40 +08:00
|
|
|
|
if (OnPanelVisibleChange.HasDelegate && _parPanelVisible != visible)
|
|
|
|
|
{
|
|
|
|
|
_parPanelVisible = visible;
|
|
|
|
|
await OnPanelVisibleChange.InvokeAsync(visible);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.ShowPanel != visible)
|
|
|
|
|
{
|
|
|
|
|
this.ShowPanel = visible;
|
|
|
|
|
}
|
2020-09-27 23:05:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-27 16:38:44 +08:00
|
|
|
|
private async void UpdateWidth(List<ResizeObserverEntry> entry)
|
|
|
|
|
{
|
|
|
|
|
await SetOverlayWidth();
|
|
|
|
|
InvokeStateHasChanged();
|
|
|
|
|
}
|
2021-04-17 22:14:01 +08:00
|
|
|
|
|
2020-08-26 22:37:08 +08:00
|
|
|
|
private async Task SetOverlayWidth()
|
2020-06-27 18:24:21 +08:00
|
|
|
|
{
|
2023-08-27 16:38:44 +08:00
|
|
|
|
HtmlElement element = await JsInvokeAsync<HtmlElement>(JSInteropConstants.GetDomInfo, _overlayTrigger.RefBack.Current);
|
|
|
|
|
_minWidth = $"min-width:{element.ClientWidth}px";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
DomEventListener.RemoveResizeObserver(_overlayTrigger.RefBack.Current, UpdateWidth);
|
|
|
|
|
base.Dispose(disposing);
|
2020-08-26 22:37:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-11-02 23:05:17 +08:00
|
|
|
|
public class AutoCompleteDataItem<TOption>
|
2020-08-26 22:37:08 +08:00
|
|
|
|
{
|
2021-04-17 22:14:01 +08:00
|
|
|
|
public AutoCompleteDataItem()
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-06-27 18:24:21 +08:00
|
|
|
|
|
2020-11-02 23:05:17 +08:00
|
|
|
|
public AutoCompleteDataItem(TOption value, string label)
|
2020-08-26 22:37:08 +08:00
|
|
|
|
{
|
|
|
|
|
Value = value;
|
|
|
|
|
Label = label;
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-02 23:05:17 +08:00
|
|
|
|
public TOption Value { get; set; }
|
2020-08-26 22:37:08 +08:00
|
|
|
|
public string Label { get; set; }
|
|
|
|
|
|
|
|
|
|
public bool IsDisabled { get; set; }
|
2020-06-27 18:24:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|