@@ -105,6 +105,7 @@ class ReactTags extends React.Component {
105
105
onKeyDown : this . onKeyDown . bind ( this )
106
106
}
107
107
108
+ this . container = React . createRef ( )
108
109
this . input = React . createRef ( )
109
110
this . suggestions = React . createRef ( )
110
111
}
@@ -180,6 +181,26 @@ class ReactTags extends React.Component {
180
181
}
181
182
}
182
183
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
+
183
204
addTag ( tag ) {
184
205
if ( tag . disabled ) {
185
206
return
@@ -214,7 +235,7 @@ class ReactTags extends React.Component {
214
235
this . state . focused && classNames . push ( this . props . classNames . rootFocused )
215
236
216
237
return (
217
- < div className = { classNames . join ( ' ' ) } onClick = { this . onClick . bind ( this ) } >
238
+ < div ref = { this . container } className = { classNames . join ( ' ' ) } onClick = { this . onClick . bind ( this ) } >
218
239
< div
219
240
className = { this . props . classNames . selected }
220
241
aria-relevant = 'additions removals'
@@ -226,7 +247,7 @@ class ReactTags extends React.Component {
226
247
tag = { tag }
227
248
removeButtonText = { this . props . removeButtonText }
228
249
classNames = { this . props . classNames }
229
- onDelete = { this . deleteTag . bind ( this , i ) }
250
+ onDelete = { this . onDeleteTag . bind ( this , i ) }
230
251
/>
231
252
) ) }
232
253
</ div >
0 commit comments