@@ -364,6 +364,8 @@ class Tokenizer extends UI5Element implements IFormInputElement {
364364 _previousToken : Token | null = null ;
365365 _focusedElementBeforeOpen ?: HTMLElement | null ;
366366 _deletedDialogItems ! : Token [ ] ;
367+ _lastFocusedToken : Token | null = null ;
368+ _isFocusSetInternally : boolean = false ;
367369 /**
368370 * Scroll to end when tokenizer is expanded
369371 * @private
@@ -496,7 +498,7 @@ class Tokenizer extends UI5Element implements IFormInputElement {
496498
497499 this . _nMoreCount = this . overflownTokens . length ;
498500
499- if ( firstToken && ! this . disabled && ! this . preventInitialFocus && ! this . _skipTabIndex ) {
501+ if ( firstToken && ! this . disabled && ! this . preventInitialFocus && ! this . _skipTabIndex && ! this . _isFocusSetInternally ) {
500502 firstToken . forcedTabIndex = "0" ;
501503 }
502504
@@ -946,6 +948,7 @@ class Tokenizer extends UI5Element implements IFormInputElement {
946948
947949 _onfocusin ( e : FocusEvent ) {
948950 const target = e . target as Token ;
951+ this . _lastFocusedToken = target ;
949952
950953 if ( target && target . toBeDeleted ) {
951954 this . _tokenDeleting = true ;
@@ -975,6 +978,7 @@ class Tokenizer extends UI5Element implements IFormInputElement {
975978
976979 if ( ! this . contains ( relatedTarget ) ) {
977980 this . _tokens [ 0 ] . forcedTabIndex = "0" ;
981+ this . _isFocusSetInternally = false ;
978982 this . _skipTabIndex = false ;
979983 }
980984
@@ -984,6 +988,22 @@ class Tokenizer extends UI5Element implements IFormInputElement {
984988 }
985989 }
986990
991+ /**
992+ * Determines the DOM element to focus when the Tokenizer receives focus.
993+ * If the last-focused token is not overflown, focus is restored to it.
994+ * Otherwise, the focus defaults to the first visible token.
995+ */
996+ getFocusDomRef ( ) : HTMLElement | undefined {
997+ if ( this . _lastFocusedToken && ! this . overflownTokens . includes ( this . _lastFocusedToken ) ) {
998+ this . _itemNav . _currentIndex = this . tokens . indexOf ( this . _lastFocusedToken ) ;
999+ this . _isFocusSetInternally = true ;
1000+ this . tokens [ 0 ] . forcedTabIndex = "-1" ;
1001+ } else {
1002+ this . _itemNav . _currentIndex = 0 ;
1003+ }
1004+ return this . _itemNav . _getCurrentItem ( ) ;
1005+ }
1006+
9871007 _toggleTokenSelection ( tokens : Array < Token > ) {
9881008 if ( ! tokens || ! tokens . length ) {
9891009 return ;
0 commit comments