Skip to content

Commit 5f25446

Browse files
committed
Merge branch 'master' into 6.0
2 parents f248a14 + 18aa300 commit 5f25446

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
- Removed `delimiterChars` option
2222
- Updated React dependency to 16.5+
2323

24+
## 5.13.1
25+
26+
- Fixed an issue where cursor focus could be lost after removing a selected tag
27+
2428
## 5.13.0
2529

2630
- Added `ariaLabel` option ([Herdismaria](https://github.com/Herdismaria))

lib/ReactTags.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -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>

spec/ReactTags.spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,11 @@ describe('React Tags', () => {
460460
sinon.assert.calledWith(props.onDelete, sinon.match(0))
461461
})
462462

463+
it('moves focus to the input when a tag is removed', () => {
464+
click($('.react-tags__selected-tag'))
465+
expect(document.activeElement).toEqual($('input'))
466+
})
467+
463468
it('deletes the last selected tag when backspace is pressed and query is empty', () => {
464469
type(''); key('Backspace')
465470

0 commit comments

Comments
 (0)