@@ -105,6 +105,7 @@ class ReactTags extends React.Component {
105105 onKeyDown : this . onKeyDown . bind ( this )
106106 }
107107
108+ this . container = React . createRef ( )
108109 this . input = React . createRef ( )
109110 this . suggestions = React . createRef ( )
110111 }
@@ -180,6 +181,26 @@ class ReactTags extends React.Component {
180181 }
181182 }
182183
184+ onDeleteTag ( index , event ) {
185+ // Because we'll destroy the element with cursor focus we need to ensure
186+ // it does not get lost and move it to the next interactive element
187+ if ( this . container . current ) {
188+ const interactiveEls = this . container . current . querySelectorAll ( 'a,button,input' )
189+
190+ const currentEl = Array . prototype . findIndex . call ( interactiveEls , ( element ) => {
191+ return element === event . currentTarget
192+ } )
193+
194+ const nextEl = interactiveEls [ currentEl - 1 ] || interactiveEls [ currentEl + 1 ]
195+
196+ if ( nextEl ) {
197+ nextEl . focus ( )
198+ }
199+ }
200+
201+ this . deleteTag ( index )
202+ }
203+
183204 addTag ( tag ) {
184205 if ( tag . disabled ) {
185206 return
@@ -214,7 +235,7 @@ class ReactTags extends React.Component {
214235 this . state . focused && classNames . push ( this . props . classNames . rootFocused )
215236
216237 return (
217- < div className = { classNames . join ( ' ' ) } onClick = { this . onClick . bind ( this ) } >
238+ < div ref = { this . container } className = { classNames . join ( ' ' ) } onClick = { this . onClick . bind ( this ) } >
218239 < div
219240 className = { this . props . classNames . selected }
220241 aria-relevant = 'additions removals'
@@ -226,7 +247,7 @@ class ReactTags extends React.Component {
226247 tag = { tag }
227248 removeButtonText = { this . props . removeButtonText }
228249 classNames = { this . props . classNames }
229- onDelete = { this . deleteTag . bind ( this , i ) }
250+ onDelete = { this . onDeleteTag . bind ( this , i ) }
230251 />
231252 ) ) }
232253 </ div >
0 commit comments