Skip to content

Commit c6b14dc

Browse files
braia123ArgoZhang
andauthored
feat(AutoFill): support update value by external code (#7058)
* 赋值给Value时更新input显示内容 * 测试用按钮删除 * refactor: 重构代码 --------- Co-authored-by: Argo Zhang <[email protected]>
1 parent eaa2a93 commit c6b14dc

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

src/BootstrapBlazor.Server/Components/Samples/AutoFills.razor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ private AttributeItem[] GetAttributes() =>
213213
Type = "bool",
214214
ValueList = "true/false",
215215
DefaultValue = "false"
216+
},
217+
new()
218+
{
219+
Name=nameof(AutoFill<string>.IsAutoClearWhenInvalid),
220+
Description=Localizer["AttIsAutoClearWhenInvalid"],
221+
Type="bool",
222+
ValueList="true/false",
223+
DefaultValue="false"
216224
}
217225
];
218226
}

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,8 @@
19971997
"IsVirtualizeTitle": "Virtualize",
19981998
"IsVirtualizeIntro": "Set <code>IsVirtualize</code> to <b>true</b> enable virtual scroll for large data",
19991999
"IsVirtualizeDescription": "Component virtual scrolling supports two ways of providing data through <code>Items</code> or <code>OnQueryAsync</code> callback methods",
2000-
"AttrIsVirtualize": "Wether to enable virtualize"
2000+
"AttrIsVirtualize": "Wether to enable virtualize",
2001+
"AttIsAutoClearWhenInvalid": "Whether to automatically clear invalid values when focus leaves the component"
20012002
},
20022003
"BootstrapBlazor.Server.Components.Samples.AutoCompletes": {
20032004
"Title": "AutoComplete",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,8 @@
19971997
"IsVirtualizeTitle": "虚拟滚动",
19981998
"IsVirtualizeIntro": "通过设置 <code>IsVirtualize</code> 参数开启组件虚拟功能特性",
19991999
"IsVirtualizeDescription": "组件虚拟滚动支持两种形式通过 <code>Items</code> 或者 <code>OnQueryAsync</code> 回调方法提供数据",
2000-
"AttrIsVirtualize": "是否开启虚拟滚动"
2000+
"AttrIsVirtualize": "是否开启虚拟滚动",
2001+
"AttIsAutoClearWhenInvalid":"焦点移除组件时是否自动清空无效值"
20012002
},
20022003
"BootstrapBlazor.Server.Components.Samples.AutoCompletes": {
20032004
"Title": "AutoComplete 自动完成",

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public partial class AutoFill<TValue>
143143
[NotNull]
144144
private RenderTemplate? _dropdown = null;
145145

146+
private string? _lastClientValue;
147+
146148
/// <summary>
147149
/// Gets the component style.
148150
/// </summary>
@@ -180,6 +182,30 @@ protected override void OnParametersSet()
180182
Items ??= [];
181183
}
182184

185+
/// <summary>
186+
/// <inheritdoc/>
187+
/// </summary>
188+
/// <param name="firstRender"></param>
189+
/// <returns></returns>
190+
protected override async Task OnAfterRenderAsync(bool firstRender)
191+
{
192+
await base.OnAfterRenderAsync(firstRender);
193+
194+
if (firstRender)
195+
{
196+
_lastClientValue = _clientValue;
197+
}
198+
199+
if (_lastClientValue != _clientValue)
200+
{
201+
_lastClientValue = _clientValue;
202+
_filterItems = null;
203+
204+
_dropdown.Render();
205+
await InvokeVoidAsync("setValue", Id, _clientValue);
206+
}
207+
}
208+
183209
/// <summary>
184210
/// <inheritdoc/>
185211
/// </summary>
@@ -215,6 +241,7 @@ private async Task OnClickItem(TValue val)
215241
{
216242
CurrentValue = val;
217243
_displayText = GetDisplayText(val);
244+
_clientValue = _displayText;
218245

219246
// 使用脚本更新 input 值
220247
await InvokeVoidAsync("setValue", Id, _displayText);

0 commit comments

Comments
 (0)