Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,16 @@ export function init(id, invoke, value, changedEventCallback) {
const handlerKeydown = (ac, e) => {
const key = e.key;
const { el, invoke, menu } = ac;
if (key === 'Enter') {
if (key === 'Enter' || key === 'NumpadEnter') {
const skipEnter = el.getAttribute('data-bb-skip-enter') === 'true';
if (!skipEnter) {
const items = [...menu.querySelectorAll('.dropdown-item')];
if (items.length === 1) {
// 当只有一个候选项时,直接触发点击
items[0].click();
invoke.invokeMethodAsync('EnterCallback');
return;
}
const current = menu.querySelector('.active');
if (current !== null) {
current.click();
Expand Down Expand Up @@ -227,3 +234,11 @@ const scrollIntoView = (el, item) => {
}

export { handleKeyUp, select, selectAllByFocus, selectAllByEnter }

export function getInputValue(id) {
const ac = Data.get(id);
if (ac && ac.input) {
return ac.input.value;
}
return "";
}
3 changes: 2 additions & 1 deletion src/BootstrapBlazor/Components/AutoFill/AutoFill.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
data-bb-auto-dropdown-focus="@ShowDropdownListOnFocusString" data-bb-debounce="@DurationString"
data-bb-skip-esc="@SkipEscString" data-bb-skip-enter="@SkipEnterString"
data-bb-scroll-behavior="@ScrollIntoViewBehaviorString"
placeholder="@PlaceHolder" disabled="@Disabled" />
placeholder="@PlaceHolder" disabled="@Disabled"
onblur="@OnBlur" />
<span class="form-select-append"><i class="@Icon"></i></span>
<span class="form-select-append ac-loading"><i class="@LoadingIcon"></i></span>
@if (GetClearable())
Expand Down
37 changes: 37 additions & 0 deletions src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@
[Parameter]
public float RowHeight { get; set; } = 50f;

/// <summary>
/// 失去焦点时判定输入框内容无效则清空内容
/// </summary>
[Parameter]
public bool IsClearOnInvalid { get; set; } = false;

/// <summary>
/// Gets or sets the overscan count for virtual scrolling. Default is 3.
/// </summary>
Expand Down Expand Up @@ -275,4 +281,35 @@
}
_dropdown.Render();
}


protected virtual async Task OnBlur()

Check warning on line 286 in src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs

View workflow job for this annotation

GitHub Actions / run test

Missing XML comment for publicly visible type or member 'AutoFill<TValue>.OnBlur()'

Check warning on line 286 in src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs

View workflow job for this annotation

GitHub Actions / run test

'AutoFill<TValue>.OnBlur()' hides inherited member 'BootstrapInputBase<TValue>.OnBlur()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
{
if (IsClearOnInvalid)
{
// 获取input的实际值
var inputValue = await GetInputValue();

if (!string.IsNullOrEmpty(inputValue))
{
if (GetDisplayText(Value) != inputValue)
{
// 如果没有匹配项,清空输入和Model
CurrentValue = default;
await InvokeVoidAsync("setValue", Id, "");
}
}
}


if (OnBlurAsync != null)
{
await OnBlurAsync(Value);
}
}
[JSInvokable]
public async Task<string> GetInputValue()

Check warning on line 311 in src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs

View workflow job for this annotation

GitHub Actions / run test

Missing XML comment for publicly visible type or member 'AutoFill<TValue>.GetInputValue()'
{
return await InvokeAsync<string>("getInputValue", Id);

Check warning on line 313 in src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs

View workflow job for this annotation

GitHub Actions / run test

Possible null reference return.
}
}