@@ -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,6 +150,26 @@ class ReactTags extends React.Component {
148
150
}
149
151
}
150
152
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 )
171
+ }
172
+
151
173
addTag ( tag ) {
152
174
if ( tag . disabled ) {
153
175
return
@@ -184,7 +206,7 @@ class ReactTags extends React.Component {
184
206
key = { i }
185
207
tag = { tag }
186
208
classNames = { this . state . classNames }
187
- onDelete = { this . deleteTag . bind ( this , i ) }
209
+ onDelete = { this . handleDeleteTag . bind ( this , i ) }
188
210
/>
189
211
) )
190
212
@@ -194,7 +216,7 @@ class ReactTags extends React.Component {
194
216
this . state . focused && classNames . push ( this . state . classNames . rootFocused )
195
217
196
218
return (
197
- < div className = { classNames . join ( ' ' ) } onClick = { this . handleClick . bind ( this ) } >
219
+ < div ref = { this . container } className = { classNames . join ( ' ' ) } onClick = { this . handleClick . bind ( this ) } >
198
220
< div className = { this . state . classNames . selected } aria-live = 'polite' aria-relevant = 'additions removals' >
199
221
{ tags }
200
222
</ div >
0 commit comments