refactor(AutoFill): update private property to field (#2037)

* refactor: 重构 OnKeyUp 处理方法

* refactor: 重构内部变量

* doc: 更新示例

* refactor: 删除冗余代码

* chore: 更新打包文件
This commit is contained in:
Argo Zhang 2023-08-31 23:36:49 +08:00 committed by GitHub
parent c15781dcbb
commit 0536431f72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 27 deletions

View File

@ -61,7 +61,7 @@
<div class="col-12 col-sm-6">
<BootstrapInputGroup>
<BootstrapInputGroupLabel DisplayText="AutoFill" />
<AutoFill TValue="Foo" Value="Model" Items="AufoFillItems" IsLikeMatch="true" OnGetDisplayText="@(foo => foo.Name ?? "")">
<AutoFill TValue="Foo" Items="AufoFillItems" IsLikeMatch="true" OnGetDisplayText="@(foo => foo.Name ?? "")">
<Template>
<div class="d-flex">
<div>

View File

@ -25,7 +25,7 @@ public partial class InputGroups
private IEnumerable<Foo>? AufoFillItems { get; set; }
[NotNull]
private List<SelectedItem>? Items2 => new()
private static List<SelectedItem>? Items2 => new()
{
new ("Beijing", "北京"),
new ("Shanghai", "上海"),

View File

@ -7,13 +7,13 @@
<BootstrapLabel required="@Required" for="@InputId" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
}
<div class="@ClassString" id="@Id">
<input @attributes="AdditionalAttributes" id="@InputId" class="@ClassName" autocomplete="off" type="@Type" data-bs-toggle="@ToggleString" data-bs-placement="@PlacementString" data-bs-offset="@OffsetString" data-bs-custom-class="@CustomClassString" placeholder="@PlaceHolder" data-bb-debounce="@DurationString" @bind-value="@InputString" @bind-value:event="oninput" @onblur="OnBlur" @onfocus="OnFocus" @ref="FocusElement" />
<input @attributes="AdditionalAttributes" id="@InputId" class="@ClassName" autocomplete="off" type="@Type" data-bs-toggle="@ToggleString" data-bs-placement="@PlacementString" data-bs-offset="@OffsetString" data-bs-custom-class="@CustomClassString" placeholder="@PlaceHolder" data-bb-debounce="@DurationString" @bind-value="@_inputString" @bind-value:event="oninput" @onblur="OnBlur" @onfocus="OnFocus" @ref="FocusElement" />
<span class="form-select-append"><i class="@Icon"></i></span>
<span class="form-select-append ac-loading"><i class="@LoadingIcon"></i></span>
<ul class="dropdown-menu">
@if (FilterItems.Any())
@if (_filterItems.Any())
{
foreach (var item in FilterItems)
foreach (var item in _filterItems)
{
<li @key="@item" class="dropdown-item" @onmousedown="() => OnClickItem(item)">
@if (Template != null)

View File

@ -26,8 +26,7 @@ public partial class AutoFill<TValue>
/// <summary>
/// 获得 最终候选数据源
/// </summary>
[NotNull]
protected List<TValue>? FilterItems { get; private set; }
private List<TValue> _filterItems = new();
/// <summary>
/// 获得/设置 组件数据集合
@ -108,7 +107,7 @@ public partial class AutoFill<TValue>
[NotNull]
private IStringLocalizer<AutoComplete>? Localizer { get; set; }
private string InputString { get; set; } = "";
private string _inputString = "";
private TValue? ActiveSelectedItem { get; set; }
@ -122,7 +121,6 @@ public partial class AutoFill<TValue>
NoDataTip ??= Localizer[nameof(NoDataTip)];
PlaceHolder ??= Localizer[nameof(PlaceHolder)];
Items ??= Enumerable.Empty<TValue>();
FilterItems ??= new List<TValue>();
}
/// <summary>
@ -136,7 +134,7 @@ public partial class AutoFill<TValue>
LoadingIcon ??= IconTheme.GetIconByKey(ComponentIcons.LoadingIcon);
OnGetDisplayText ??= v => v?.ToString() ?? "";
InputString = OnGetDisplayText(Value);
_inputString = Value == null ? string.Empty : OnGetDisplayText(Value);
}
/// <summary>
@ -159,7 +157,7 @@ public partial class AutoFill<TValue>
{
_isShown = false;
CurrentValue = val;
InputString = OnGetDisplayText(val);
_inputString = OnGetDisplayText(val);
ActiveSelectedItem = default;
if (OnSelectedItemChanged != null)
{
@ -172,7 +170,7 @@ public partial class AutoFill<TValue>
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
protected virtual async Task OnFocus(FocusEventArgs args)
private async Task OnFocus(FocusEventArgs args)
{
if (ShowDropdownListOnFocus)
{
@ -193,19 +191,18 @@ public partial class AutoFill<TValue>
_isLoading = true;
if (OnCustomFilter != null)
{
var items = await OnCustomFilter(InputString);
FilterItems = items.ToList();
var items = await OnCustomFilter(_inputString);
_filterItems = items.ToList();
}
else
{
var items = FindItem();
FilterItems = DisplayCount == null ? items.ToList() : items.Take(DisplayCount.Value).ToList();
_filterItems = DisplayCount == null ? items.ToList() : items.Take(DisplayCount.Value).ToList();
}
_isLoading = false;
}
var source = FilterItems;
if (source.Any())
if (_filterItems.Any())
{
_isShown = true;
// 键盘向上选择
@ -214,13 +211,13 @@ public partial class AutoFill<TValue>
var index = 0;
if (ActiveSelectedItem != null)
{
index = source.IndexOf(ActiveSelectedItem) - 1;
index = _filterItems.IndexOf(ActiveSelectedItem) - 1;
if (index < 0)
{
index = source.Count - 1;
index = _filterItems.Count - 1;
}
}
ActiveSelectedItem = source[index];
ActiveSelectedItem = _filterItems[index];
CurrentItemIndex = index;
}
else if (key == "ArrowDown")
@ -228,13 +225,13 @@ public partial class AutoFill<TValue>
var index = 0;
if (ActiveSelectedItem != null)
{
index = source.IndexOf(ActiveSelectedItem) + 1;
if (index > source.Count - 1)
index = _filterItems.IndexOf(ActiveSelectedItem) + 1;
if (index > _filterItems.Count - 1)
{
index = 0;
}
}
ActiveSelectedItem = source[index];
ActiveSelectedItem = _filterItems[index];
CurrentItemIndex = index;
}
else if (key == "Escape")
@ -250,7 +247,7 @@ public partial class AutoFill<TValue>
ActiveSelectedItem ??= FindItem().FirstOrDefault();
if (ActiveSelectedItem != null)
{
InputString = OnGetDisplayText(ActiveSelectedItem);
_inputString = OnGetDisplayText(ActiveSelectedItem);
}
await OnBlur();
if (!SkipEnter && OnEnterAsync != null)
@ -265,8 +262,8 @@ public partial class AutoFill<TValue>
{
var comparison = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
return IsLikeMatch ?
Items.Where(s => OnGetDisplayText(s).Contains(InputString, comparison)) :
Items.Where(s => OnGetDisplayText(s).StartsWith(InputString, comparison));
Items.Where(s => OnGetDisplayText(s).Contains(_inputString, comparison)) :
Items.Where(s => OnGetDisplayText(s).StartsWith(_inputString, comparison));
}
}
@ -277,6 +274,6 @@ public partial class AutoFill<TValue>
[JSInvokable]
public void TriggerOnChange(string val)
{
InputString = val;
_inputString = val;
}
}