Emit editor blur events as if no hidden input existed

This commit is contained in:
Nathan Sobo 2017-03-16 21:03:48 -06:00 committed by Antonio Scandurra
parent b152bfd9c6
commit 369818b475
2 changed files with 25 additions and 1 deletions

View File

@ -322,6 +322,20 @@ describe('TextEditorComponent', () => {
expect(document.activeElement).toBe(component.refs.hiddenInput)
})
it('emits blur events only when focus shifts to something other than the editor itself or its hidden input', () => {
const {element} = buildComponent()
let blurEventCount = 0
element.addEventListener('blur', () => blurEventCount++)
element.focus()
expect(blurEventCount).toBe(0)
element.focus()
expect(blurEventCount).toBe(0)
document.body.focus()
expect(blurEventCount).toBe(1)
})
})
describe('autoscroll on cursor movement', () => {

View File

@ -166,7 +166,10 @@ class TextEditorComponent {
attributes,
style,
tabIndex: -1,
on: {focus: this.didFocus}
on: {
focus: this.didFocus,
blur: this.didBlur
}
},
$.div(
{
@ -707,10 +710,17 @@ class TextEditorComponent {
}
}
didBlur (event) {
if (event.relatedTarget === this.refs.hiddenInput) {
event.stopImmediatePropagation()
}
}
didBlurHiddenInput (event) {
if (this.element !== event.relatedTarget && !this.element.contains(event.relatedTarget)) {
this.focused = false
this.scheduleUpdate()
this.element.dispatchEvent(new FocusEvent(event.type, event))
}
}