@@ -4,6 +4,7 @@ import property from "@ui5/webcomponents-base/dist/decorators/property.js";
44import slot from "@ui5/webcomponents-base/dist/decorators/slot.js" ;
55import event from "@ui5/webcomponents-base/dist/decorators/event-strict.js" ;
66import type ValueState from "@ui5/webcomponents-base/dist/types/ValueState.js" ;
7+ import type { ChangeInfo } from "@ui5/webcomponents-base/dist/UI5Element.js" ;
78import ToolbarSelectCss from "./generated/themes/ToolbarSelect.css.js" ;
89import type Select from "./Select.js" ;
910
@@ -91,6 +92,7 @@ class ToolbarSelect extends ToolbarItem {
9192 @slot ( {
9293 "default" : true ,
9394 type : HTMLElement ,
95+ invalidateOnChildChange : true ,
9496 } )
9597 options ! : Array < ToolbarSelectOption > ;
9698
@@ -146,14 +148,19 @@ class ToolbarSelect extends ToolbarItem {
146148 */
147149 @property ( )
148150 set value ( newValue : string ) {
149- if ( this . select && this . select . value !== newValue ) {
150- this . select . value = newValue ;
151- }
152151 this . _value = newValue ;
152+ const selectElement = this . select ;
153+ if ( selectElement && selectElement . value !== newValue ) {
154+ selectElement . value = newValue ;
155+ }
153156 }
154157
155158 get value ( ) : string | undefined {
156- return this . select ? this . select . value : this . _value ;
159+ // Always return _value if it's set, as it represents the source of truth
160+ if ( this . _value !== undefined && this . _value !== "" ) {
161+ return this . _value ;
162+ }
163+ return this . select ? this . select . value : undefined ;
157164 }
158165
159166 get select ( ) : Select | null {
@@ -163,6 +170,30 @@ class ToolbarSelect extends ToolbarItem {
163170 // Internal value storage, in case the composite select is not rendered on the the assignment happens
164171 _value : string = "" ;
165172
173+ onInvalidation ( changeInfo : ChangeInfo ) {
174+ // When a child ToolbarSelectOption's selected property changes, update the value
175+ if ( changeInfo . reason === "childchange" ) {
176+ const selectedOption = this . options . find ( option => option . selected ) ;
177+ if ( selectedOption ) {
178+ const newValue = selectedOption . textContent || "" ;
179+ // Update both internal value and the select component's value
180+ this . _value = newValue ;
181+ // Cache the select reference to avoid multiple DOM queries
182+ const selectElement = this . select ;
183+ if ( selectElement && selectElement . value !== newValue ) {
184+ selectElement . value = newValue ;
185+ }
186+ } else {
187+ // If no option is selected, clear the value
188+ this . _value = "" ;
189+ const selectElement = this . select ;
190+ if ( selectElement ) {
191+ selectElement . value = "" ;
192+ }
193+ }
194+ }
195+ }
196+
166197 onClick ( e : Event ) : void {
167198 e . stopImmediatePropagation ( ) ;
168199 const prevented = ! this . fireDecoratorEvent ( "click" , { targetRef : e . target as HTMLElement } ) ;
@@ -189,12 +220,16 @@ class ToolbarSelect extends ToolbarItem {
189220
190221 onChange ( e : CustomEvent < SelectChangeEventDetail > ) : void {
191222 e . stopImmediatePropagation ( ) ;
223+
224+ // Update internal value BEFORE firing the change event
225+ // so that when event listeners read this.value, they get the updated value
226+ this . _value = e . detail . selectedOption ?. textContent || "" ;
227+ this . _syncOptions ( e . detail . selectedOption ) ;
228+
192229 const prevented = ! this . fireDecoratorEvent ( "change" , { ...e . detail , targetRef : e . target as HTMLElement } ) ;
193230 if ( ! prevented ) {
194231 this . fireDecoratorEvent ( "close-overflow" ) ;
195232 }
196-
197- this . _syncOptions ( e . detail . selectedOption ) ;
198233 }
199234
200235 _syncOptions ( selectedOption : HTMLElement ) : void {
0 commit comments