33// See the LICENSE file in the project root for more information.
44// Maintainer: Argo Zhang([email protected] ) Website: https://www.blazor.zone 55
6- using Microsoft . AspNetCore . Components . Web ;
76using Microsoft . Extensions . Localization ;
87
98namespace BootstrapBlazor . Components ;
@@ -13,19 +12,10 @@ namespace BootstrapBlazor.Components;
1312/// </summary>
1413public partial class AutoComplete
1514{
16- private bool IsLoading { get ; set ; }
17-
18- /// <summary>
19- /// 获得/设置 当前下拉框是否显示
20- /// </summary>
21- private bool _isShown ;
22-
2315 /// <summary>
2416 /// 获得 组件样式
2517 /// </summary>
2618 protected virtual string ? ClassString => CssBuilder . Default ( "auto-complete" )
27- . AddClass ( "is-loading" , IsLoading )
28- . AddClass ( "show" , _isShown && ! IsPopover )
2919 . Build ( ) ;
3020
3121 /// <summary>
@@ -143,20 +133,6 @@ protected override void OnParametersSet()
143133 LoadingIcon ??= IconTheme . GetIconByKey ( ComponentIcons . LoadingIcon ) ;
144134 }
145135
146- /// <summary>
147- /// <inheritdoc/>
148- /// </summary>
149- protected override async Task OnBlur ( )
150- {
151- CurrentSelectedItem = "" ;
152- _isShown = false ;
153-
154- if ( OnBlurAsync != null )
155- {
156- await OnBlurAsync ( Value ) ;
157- }
158- }
159-
160136 /// <summary>
161137 /// 鼠标点击候选项时回调此方法
162138 /// </summary>
@@ -169,32 +145,6 @@ protected virtual async Task OnClickItem(string val)
169145 }
170146 }
171147
172- /// <summary>
173- /// OnFocus 方法
174- /// </summary>
175- /// <param name="args"></param>
176- /// <returns></returns>
177- protected virtual async Task OnFocus ( FocusEventArgs args )
178- {
179- if ( ShowDropdownListOnFocus )
180- {
181- if ( OnFocusFilter )
182- {
183- await OnKeyUp ( "" ) ;
184- }
185- else
186- {
187- FilterItems = DisplayCount == null ? Items . ToList ( ) : Items . Take ( DisplayCount . Value ) . ToList ( ) ;
188- _isShown = true ;
189-
190- if ( IsPopover )
191- {
192- await InvokeVoidAsync ( "triggerFocus" , Id ) ;
193- }
194- }
195- }
196- }
197-
198148 /// <summary>
199149 /// OnKeyUp 方法
200150 /// </summary>
@@ -203,27 +153,6 @@ protected virtual async Task OnFocus(FocusEventArgs args)
203153 [ JSInvokable ]
204154 public virtual async Task OnKeyUp ( string key )
205155 {
206- if ( ! IsLoading )
207- {
208- IsLoading = true ;
209- if ( OnCustomFilter != null )
210- {
211- var items = await OnCustomFilter ( CurrentValueAsString ) ;
212- FilterItems = items . ToList ( ) ;
213- }
214- else
215- {
216- var comparison = IgnoreCase ? StringComparison . OrdinalIgnoreCase : StringComparison . Ordinal ;
217- var items = IsLikeMatch ?
218- Items . Where ( s => s . Contains ( CurrentValueAsString , comparison ) ) :
219- Items . Where ( s => s . StartsWith ( CurrentValueAsString , comparison ) ) ;
220- FilterItems = DisplayCount == null ? items . ToList ( ) : items . Take ( DisplayCount . Value ) . ToList ( ) ;
221- }
222- IsLoading = false ;
223- }
224-
225- _isShown = true ;
226-
227156 var source = FilterItems ;
228157 if ( source . Count > 0 )
229158 {
@@ -290,8 +219,44 @@ public virtual async Task OnKeyUp(string key)
290219 /// </summary>
291220 /// <param name="val"></param>
292221 [ JSInvokable ]
293- public void TriggerOnChange ( string val )
222+ public async Task TriggerOnChange ( string val )
223+ {
224+ if ( OnCustomFilter != null )
225+ {
226+ var items = await OnCustomFilter ( val ) ;
227+ FilterItems = items . ToList ( ) ;
228+ }
229+ else
230+ {
231+ var comparison = IgnoreCase ? StringComparison . OrdinalIgnoreCase : StringComparison . Ordinal ;
232+ var items = IsLikeMatch
233+ ? Items . Where ( s => s . Contains ( val , comparison ) )
234+ : Items . Where ( s => s . StartsWith ( val , comparison ) ) ;
235+ FilterItems = items . ToList ( ) ;
236+ }
237+
238+ if ( DisplayCount != null )
239+ {
240+ FilterItems = FilterItems . Take ( DisplayCount . Value ) . ToList ( ) ;
241+ }
242+
243+ CurrentValue = val ;
244+ if ( ! ValueChanged . HasDelegate )
245+ {
246+ StateHasChanged ( ) ;
247+ }
248+ }
249+
250+ /// <summary>
251+ /// 出发 OnBlur 回调方法 由 Javascript 触发
252+ /// </summary>
253+ /// <returns></returns>
254+ [ JSInvokable ]
255+ public async Task TriggerBlur ( )
294256 {
295- CurrentValueAsString = val ;
257+ if ( OnBlurAsync != null )
258+ {
259+ await OnBlurAsync ( Value ) ;
260+ }
296261 }
297262}
0 commit comments