33// ------------------------------------------------------------------------
44
55using System . Diagnostics . CodeAnalysis ;
6+ using System . Linq . Expressions ;
67using Microsoft . AspNetCore . Components ;
78using Microsoft . AspNetCore . Components . Forms ;
89using Microsoft . AspNetCore . Components . Web ;
@@ -37,7 +38,7 @@ public abstract partial class ListComponentBase<TOption> : FluentInputBase<strin
3738
3839 // We cascade the _internalListContext to descendants, which in turn call it to add themselves to the options list
3940 internal InternalListContext < TOption > _internalListContext ;
40- internal override bool FieldBound => Field is not null || ValueExpression is not null || ValueChanged . HasDelegate || SelectedOptionChanged . HasDelegate || SelectedOptionsChanged . HasDelegate ;
41+ internal override bool FieldBound => Field is not null || ValueExpression is not null || ValueChanged . HasDelegate || SelectedOptionChanged . HasDelegate || SelectedOptionExpression is not null || SelectedOptionsChanged . HasDelegate || SelectedOptionsExpression is not null ;
4142
4243 protected override async Task OnAfterRenderAsync ( bool firstRender )
4344 {
@@ -149,6 +150,13 @@ protected string? InternalValue
149150 [ Parameter ]
150151 public virtual EventCallback < TOption ? > SelectedOptionChanged { get ; set ; }
151152
153+ /// <summary>
154+ /// Gets or sets an expression that identifies the bound selected options.
155+ /// ⚠️ Only available when Multiple = false.
156+ /// </summary>
157+ [ Parameter ]
158+ public Expression < Func < TOption > > ? SelectedOptionExpression { get ; set ; }
159+
152160 /// <summary>
153161 /// If true, the user can select multiple elements.
154162 /// ⚠️ Only available for the FluentSelect and FluentListbox components.
@@ -183,6 +191,13 @@ protected string? InternalValue
183191 [ Parameter ]
184192 public virtual EventCallback < IEnumerable < TOption > ? > SelectedOptionsChanged { get ; set ; }
185193
194+ /// <summary>
195+ /// Gets or sets an expression that identifies the bound selected options.
196+ /// ⚠️ Only available when Multiple = true.
197+ /// </summary>
198+ [ Parameter ]
199+ public Expression < Func < IEnumerable < TOption > > > ? SelectedOptionsExpression { get ; set ; }
200+
186201 /// <summary />
187202 public ListComponentBase ( )
188203 {
@@ -295,11 +310,20 @@ public override async Task SetParametersAsync(ParameterView parameters)
295310 }
296311 }
297312
298- if ( SelectedOptionChanged . HasDelegate )
313+
314+ if ( SelectedOptionExpression is not null )
315+ {
316+ FieldIdentifier = FieldIdentifier . Create ( SelectedOptionExpression ) ;
317+ }
318+ else if ( SelectedOptionChanged . HasDelegate )
299319 {
300320 FieldIdentifier = FieldIdentifier . Create ( ( ) => SelectedOption ) ;
301321 }
302- if ( SelectedOptionsChanged . HasDelegate )
322+ else if ( SelectedOptionsExpression is not null )
323+ {
324+ FieldIdentifier = FieldIdentifier . Create ( SelectedOptionsExpression ) ;
325+ }
326+ else if ( SelectedOptionsChanged . HasDelegate )
303327 {
304328 FieldIdentifier = FieldIdentifier . Create ( ( ) => SelectedOptions ) ;
305329 }
@@ -529,19 +553,8 @@ protected virtual async Task OnSelectedItemChangedHandlerAsync(TOption? item)
529553 {
530554 if ( ! Equals ( item , SelectedOption ) )
531555 {
532- var value = GetOptionValue ( item ) ;
533-
534- if ( this is FluentListbox < TOption > ||
535- this is FluentCombobox < TOption > ||
536- ( this is FluentSelect < TOption > & & Value is null ) )
537- {
538- await base . ChangeHandlerAsync ( new ChangeEventArgs ( ) { Value = value } ) ;
539- }
540-
541556 SelectedOption = item ;
542-
543- //InternalValue = Value = value;
544- InternalValue = value ;
557+ InternalValue = GetOptionValue ( item ) ;
545558 await RaiseChangedEventsAsync ( ) ;
546559 }
547560 }
@@ -564,6 +577,8 @@ protected virtual async Task RaiseChangedEventsAsync()
564577 await SelectedOptionChanged . InvokeAsync ( SelectedOption ) ;
565578 }
566579 }
580+
581+ await base . ChangeHandlerAsync ( new ChangeEventArgs ( ) { Value = InternalValue } ) ;
567582 }
568583
569584 protected virtual async Task OnKeydownHandlerAsync ( KeyboardEventArgs e )
0 commit comments