Skip to content

Commit 3610e08

Browse files
committed
AutoFill组件变动
input输入Enter时,如候选项唯一,直接选中 当焦点从AutoFill移出时,如GetDisplayText(Value)对应的内容与input中的内容不符则清空Value和Input
1 parent a88529b commit 3610e08

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,16 @@ export function init(id, invoke, value, changedEventCallback) {
150150
const handlerKeydown = (ac, e) => {
151151
const key = e.key;
152152
const { el, invoke, menu } = ac;
153-
if (key === 'Enter') {
153+
if (key === 'Enter' || key === 'NumpadEnter') {
154154
const skipEnter = el.getAttribute('data-bb-skip-enter') === 'true';
155155
if (!skipEnter) {
156+
const items = [...menu.querySelectorAll('.dropdown-item')];
157+
if (items.length === 1) {
158+
// 当只有一个候选项时,直接触发点击
159+
items[0].click();
160+
invoke.invokeMethodAsync('EnterCallback');
161+
return;
162+
}
156163
const current = menu.querySelector('.active');
157164
if (current !== null) {
158165
current.click();
@@ -227,3 +234,11 @@ const scrollIntoView = (el, item) => {
227234
}
228235

229236
export { handleKeyUp, select, selectAllByFocus, selectAllByEnter }
237+
238+
export function getInputValue(id) {
239+
const ac = Data.get(id);
240+
if (ac && ac.input) {
241+
return ac.input.value;
242+
}
243+
return "";
244+
}

src/BootstrapBlazor/Components/AutoFill/AutoFill.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
data-bb-auto-dropdown-focus="@ShowDropdownListOnFocusString" data-bb-debounce="@DurationString"
1515
data-bb-skip-esc="@SkipEscString" data-bb-skip-enter="@SkipEnterString"
1616
data-bb-scroll-behavior="@ScrollIntoViewBehaviorString"
17-
placeholder="@PlaceHolder" disabled="@Disabled" />
17+
placeholder="@PlaceHolder" disabled="@Disabled"
18+
onblur="@OnBlur" />
1819
<span class="form-select-append"><i class="@Icon"></i></span>
1920
<span class="form-select-append ac-loading"><i class="@LoadingIcon"></i></span>
2021
@if (GetClearable())

src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ public partial class AutoFill<TValue>
9999
[Parameter]
100100
public float RowHeight { get; set; } = 50f;
101101

102+
/// <summary>
103+
/// 失去焦点时判定输入框内容无效则清空内容
104+
/// </summary>
105+
[Parameter]
106+
public bool IsClearOnInvalid { get; set; } = false;
107+
102108
/// <summary>
103109
/// Gets or sets the overscan count for virtual scrolling. Default is 3.
104110
/// </summary>
@@ -275,4 +281,35 @@ public async Task TriggerFilter(string val)
275281
}
276282
_dropdown.Render();
277283
}
284+
285+
286+
protected virtual async Task OnBlur()
287+
{
288+
if (IsClearOnInvalid)
289+
{
290+
// 获取input的实际值
291+
var inputValue = await GetInputValue();
292+
293+
if (!string.IsNullOrEmpty(inputValue))
294+
{
295+
if (GetDisplayText(Value) != inputValue)
296+
{
297+
// 如果没有匹配项,清空输入和Model
298+
CurrentValue = default;
299+
await InvokeVoidAsync("setValue", Id, "");
300+
}
301+
}
302+
}
303+
304+
305+
if (OnBlurAsync != null)
306+
{
307+
await OnBlurAsync(Value);
308+
}
309+
}
310+
[JSInvokable]
311+
public async Task<string> GetInputValue()
312+
{
313+
return await InvokeAsync<string>("getInputValue", Id);
314+
}
278315
}

0 commit comments

Comments
 (0)