@@ -49,6 +49,8 @@ class ReactTags extends React.Component {
49
49
onInput : this . handleInput . bind ( this ) ,
50
50
onKeyDown : this . handleKeyDown . bind ( this )
51
51
}
52
+
53
+ this . container = React . createRef ( )
52
54
}
53
55
54
56
componentWillReceiveProps ( newProps ) {
@@ -148,9 +150,24 @@ class ReactTags extends React.Component {
148
150
}
149
151
}
150
152
151
- handleTagClick ( i ) {
152
- this . deleteTag ( i )
153
- this . input . input . focus ( )
153
+ handleDeleteTag ( index , event ) {
154
+ // Because we'll destroy the element with cursor focus we need to ensure
155
+ // it does not get lost and move it to the next interactive element
156
+ if ( this . container . current ) {
157
+ const interactiveEls = this . container . current . querySelectorAll ( 'a,button,input' )
158
+
159
+ const currentEl = Array . prototype . findIndex . call ( interactiveEls , ( element ) => {
160
+ return element === event . currentTarget
161
+ } )
162
+
163
+ const nextEl = interactiveEls [ currentEl - 1 ] || interactiveEls [ currentEl + 1 ]
164
+
165
+ if ( nextEl ) {
166
+ nextEl . focus ( )
167
+ }
168
+ }
169
+
170
+ this . deleteTag ( index )
154
171
}
155
172
156
173
addTag ( tag ) {
@@ -189,7 +206,7 @@ class ReactTags extends React.Component {
189
206
key = { i }
190
207
tag = { tag }
191
208
classNames = { this . state . classNames }
192
- onDelete = { this . handleTagClick . bind ( this , i ) }
209
+ onDelete = { this . handleDeleteTag . bind ( this , i ) }
193
210
/>
194
211
) )
195
212
@@ -199,7 +216,7 @@ class ReactTags extends React.Component {
199
216
this . state . focused && classNames . push ( this . state . classNames . rootFocused )
200
217
201
218
return (
202
- < div className = { classNames . join ( ' ' ) } onClick = { this . handleClick . bind ( this ) } >
219
+ < div ref = { this . container } className = { classNames . join ( ' ' ) } onClick = { this . handleClick . bind ( this ) } >
203
220
< div className = { this . state . classNames . selected } aria-live = 'polite' aria-relevant = 'additions removals' >
204
221
{ tags }
205
222
</ div >
0 commit comments