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"> <div class="col-12 col-sm-6">
<BootstrapInputGroup> <BootstrapInputGroup>
<BootstrapInputGroupLabel DisplayText="AutoFill" /> <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> <Template>
<div class="d-flex"> <div class="d-flex">
<div> <div>

View File

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

View File

@ -7,13 +7,13 @@
<BootstrapLabel required="@Required" for="@InputId" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" /> <BootstrapLabel required="@Required" for="@InputId" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
} }
<div class="@ClassString" id="@Id"> <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"><i class="@Icon"></i></span>
<span class="form-select-append ac-loading"><i class="@LoadingIcon"></i></span> <span class="form-select-append ac-loading"><i class="@LoadingIcon"></i></span>
<ul class="dropdown-menu"> <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)"> <li @key="@item" class="dropdown-item" @onmousedown="() => OnClickItem(item)">
@if (Template != null) @if (Template != null)

View File

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