Skip to content

Commit 59ebc5a

Browse files
authored
fix: re-dispatch focus and blur events (#2390)
1 parent fbbc70b commit 59ebc5a

8 files changed

Lines changed: 146 additions & 55 deletions

‎packages/field-base/src/delegate-focus-mixin.d.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ interface DelegateFocusMixin extends DisabledMixin, FocusMixin {
2222
autofocus: boolean;
2323

2424
/**
25-
* Any element extending this mixin is required to implement this getter.
26-
* It returns the actual focusable element in the component.
25+
* A reference to the focusable element controlled by the mixin.
26+
* It can be an input, textarea, button or any element with tabindex > -1.
27+
*
28+
* Any component implementing this mixin is expected to provide it
29+
* by using `this._setFocusElement(input)` Polymer API.
2730
*/
2831
readonly focusElement: Element | null | undefined;
2932
}

‎packages/field-base/src/delegate-focus-mixin.js‎

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,31 @@ const DelegateFocusMixinImplementation = (superclass) =>
1616
*/
1717
autofocus: {
1818
type: Boolean
19+
},
20+
21+
/**
22+
* A reference to the focusable element controlled by the mixin.
23+
* It can be an input, textarea, button or any element with tabindex > -1.
24+
*
25+
* Any component implementing this mixin is expected to provide it
26+
* by using `this._setFocusElement(input)` Polymer API.
27+
*
28+
* @protected
29+
* @type {!HTMLElement}
30+
*/
31+
focusElement: {
32+
type: Object,
33+
readOnly: true,
34+
observer: '_focusElementChanged'
1935
}
2036
};
2137
}
2238

23-
/**
24-
* Any element extending this mixin is required to implement this getter.
25-
* It returns the actual focusable element in the component.
26-
* @return {Element | null | undefined}
27-
*/
28-
get focusElement() {
29-
console.warn(`Please implement the 'focusElement' property in <${this.localName}>`);
30-
return null;
39+
constructor() {
40+
super();
41+
42+
this._boundOnBlur = this._onBlur.bind(this);
43+
this._boundOnFocus = this._onFocus.bind(this);
3144
}
3245

3346
/** @protected */
@@ -65,10 +78,62 @@ const DelegateFocusMixinImplementation = (superclass) =>
6578
}
6679
}
6780

81+
/** @protected */
82+
_focusElementChanged(element, oldElement) {
83+
if (element) {
84+
this._addFocusListeners(element);
85+
} else if (oldElement) {
86+
this._removeFocusListeners(oldElement);
87+
}
88+
}
89+
90+
/**
91+
* @param {HTMLElement} element
92+
* @protected
93+
*/
94+
_addFocusListeners(element) {
95+
element.addEventListener('blur', this._boundOnBlur);
96+
element.addEventListener('focus', this._boundOnFocus);
97+
}
98+
99+
/**
100+
* @param {HTMLElement} element
101+
* @protected
102+
*/
103+
_removeFocusListeners(element) {
104+
element.removeEventListener('blur', this._boundOnBlur);
105+
element.removeEventListener('focus', this._boundOnFocus);
106+
}
107+
108+
/**
109+
* Focus event does not bubble, so we dispatch it manually
110+
* on the host element to support adding focus listeners
111+
* when the focusable element is placed in light DOM.
112+
* @param {FocusEvent} event
113+
* @protected
114+
*/
115+
_onFocus(event) {
116+
event.stopPropagation();
117+
this.dispatchEvent(new Event('focus'));
118+
}
119+
120+
/**
121+
* Blur event does not bubble, so we dispatch it manually
122+
* on the host element to support adding blur listeners
123+
* when the focusable element is placed in light DOM.
124+
* @param {FocusEvent} event
125+
* @protected
126+
*/
127+
_onBlur(event) {
128+
event.stopPropagation();
129+
this.dispatchEvent(new Event('blur'));
130+
}
131+
68132
/**
69133
* @param {Event} event
70134
* @return {boolean}
71135
* @protected
136+
* @override
72137
*/
73138
_shouldSetFocus(event) {
74139
return event.target === this.focusElement;

‎packages/field-base/src/input-field-mixin.js‎

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,6 @@ const InputFieldMixinImplementation = (superclass) =>
7777
return this.inputElement;
7878
}
7979

80-
/**
81-
* Element used by `DelegatesFocusMixin` to handle focus.
82-
* @return {!HTMLInputElement}
83-
*/
84-
get focusElement() {
85-
return this.inputElement;
86-
}
87-
88-
constructor() {
89-
super();
90-
91-
this._boundOnBlur = this._onBlur.bind(this);
92-
this._boundOnFocus = this._onFocus.bind(this);
93-
}
94-
9580
/** @protected */
9681
connectedCallback() {
9782
super.connectedCallback();
@@ -133,36 +118,28 @@ const InputFieldMixinImplementation = (superclass) =>
133118
}
134119

135120
/**
136-
* @param {HTMLInputElement} input
121+
* Override an event listener from `DelegatesFocusMixin`.
122+
* @param {FocusEvent} event
137123
* @protected
124+
* @override
138125
*/
139-
_addInputListeners(input) {
140-
super._addInputListeners(input);
126+
_onFocus(event) {
127+
super._onFocus(event);
141128

142-
input.addEventListener('blur', this._boundOnBlur);
143-
input.addEventListener('focus', this._boundOnFocus);
129+
if (this.autoselect && this.inputElement) {
130+
this.inputElement.select();
131+
}
144132
}
145133

146134
/**
147-
* @param {HTMLInputElement} input
135+
* Override an event listener from `DelegatesFocusMixin`.
136+
* @param {FocusEvent} event
148137
* @protected
138+
* @override
149139
*/
150-
_removeInputListeners(input) {
151-
super._addInputListeners(input);
152-
153-
input.removeEventListener('blur', this._boundOnBlur);
154-
input.removeEventListener('focus', this._boundOnFocus);
155-
}
140+
_onBlur(event) {
141+
super._onBlur(event);
156142

157-
/** @private */
158-
_onFocus() {
159-
if (this.autoselect && this.inputElement) {
160-
this.inputElement.select();
161-
}
162-
}
163-
164-
/** @private */
165-
_onBlur() {
166143
this.validate();
167144
}
168145

‎packages/field-base/src/input-slot-mixin.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (c) 2021 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6+
import { DelegatesFocusMixin } from './delegate-focus-mixin.js';
67
import { SlotMixin } from './slot-mixin.js';
78
import { InputMixin } from './input-mixin.js';
89

@@ -15,7 +16,7 @@ interface InputSlotMixinConstructor {
1516
new (...args: any[]): InputSlotMixin;
1617
}
1718

18-
interface InputSlotMixin extends InputMixin, SlotMixin {
19+
interface InputSlotMixin extends DelegatesFocusMixin, InputMixin, SlotMixin {
1920
/**
2021
* String used to define input type.
2122
*/

‎packages/field-base/src/input-slot-mixin.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
7+
import { DelegateFocusMixin } from './delegate-focus-mixin.js';
78
import { InputMixin } from './input-mixin.js';
89
import { SlotMixin } from './slot-mixin.js';
910

1011
const InputSlotMixinImplementation = (superclass) =>
11-
class InputSlotMixinClass extends InputMixin(SlotMixin(superclass)) {
12+
class InputSlotMixinClass extends DelegateFocusMixin(InputMixin(SlotMixin(superclass))) {
1213
static get properties() {
1314
/**
1415
* String used to define input type.
@@ -59,6 +60,7 @@ const InputSlotMixinImplementation = (superclass) =>
5960
inputNode.id = this._inputId;
6061

6162
this._setInputElement(inputNode);
63+
this._setFocusElement(inputNode);
6264
}
6365
}
6466
};

‎packages/field-base/src/text-area-slot-mixin.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (c) 2021 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6+
import { DelegatesFocusMixin } from './delegate-focus-mixin.js';
67
import { SlotMixin } from './slot-mixin.js';
78
import { InputMixin } from './input-mixin.js';
89

@@ -15,6 +16,6 @@ interface TextAreaSlotMixinConstructor {
1516
new (...args: any[]): TextAreaSlotMixin;
1617
}
1718

18-
interface TextAreaSlotMixin extends InputMixin, SlotMixin {}
19+
interface TextAreaSlotMixin extends DelegatesFocusMixin, InputMixin, SlotMixin {}
1920

2021
export { TextAreaSlotMixinConstructor, TextAreaSlotMixin };

‎packages/field-base/src/text-area-slot-mixin.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
7+
import { DelegateFocusMixin } from './delegate-focus-mixin.js';
78
import { InputMixin } from './input-mixin.js';
89
import { SlotMixin } from './slot-mixin.js';
910

1011
const TextAreaSlotMixinImplementation = (superclass) =>
11-
class TextAreaSlotMixinClass extends InputMixin(SlotMixin(superclass)) {
12+
class TextAreaSlotMixinClass extends DelegateFocusMixin(InputMixin(SlotMixin(superclass))) {
1213
get slots() {
1314
return {
1415
...super.slots,
@@ -44,6 +45,7 @@ const TextAreaSlotMixinImplementation = (superclass) =>
4445
textArea.id = this._textareaId;
4546

4647
this._setInputElement(textArea);
48+
this._setFocusElement(textArea);
4749
}
4850
}
4951
};

‎packages/field-base/test/delegate-focus-mixin.test.js‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ import { InputSlotMixin } from '../src/input-slot-mixin.js';
77

88
customElements.define(
99
'delegate-focus-mixin-element',
10-
class extends DelegateFocusMixin(InputSlotMixin(PolymerElement)) {
10+
class extends InputSlotMixin(DelegateFocusMixin(PolymerElement)) {
1111
static get template() {
1212
return html`<slot name="input"></slot>`;
1313
}
14-
15-
get focusElement() {
16-
return this.inputElement;
17-
}
1814
}
1915
);
2016

@@ -92,6 +88,50 @@ describe('delegate-focus-mixin', () => {
9288
});
9389
});
9490

91+
describe('events', () => {
92+
let spy;
93+
94+
beforeEach(() => {
95+
element = fixtureSync(`<delegate-focus-mixin-element></delegate-focus-mixin-element>`);
96+
input = element.querySelector('input');
97+
spy = sinon.spy();
98+
});
99+
100+
describe('focus', () => {
101+
beforeEach(() => {
102+
element.addEventListener('focus', spy);
103+
});
104+
105+
it('should re-dispatch focus event on the host element', () => {
106+
input.dispatchEvent(new Event('focus'));
107+
expect(spy.calledOnce).to.be.true;
108+
});
109+
110+
it('should not re-dispatch focus when focusElement is removed', () => {
111+
element._setFocusElement(null);
112+
input.dispatchEvent(new Event('focus'));
113+
expect(spy.calledOnce).to.be.false;
114+
});
115+
});
116+
117+
describe('blur', () => {
118+
beforeEach(() => {
119+
element.addEventListener('blur', spy);
120+
});
121+
122+
it('should re-dispatch blur event on the host element', () => {
123+
input.dispatchEvent(new Event('blur'));
124+
expect(spy.calledOnce).to.be.true;
125+
});
126+
127+
it('should not re-dispatch blur when focusElement is removed', () => {
128+
element._setFocusElement(null);
129+
input.dispatchEvent(new Event('blur'));
130+
expect(spy.calledOnce).to.be.false;
131+
});
132+
});
133+
});
134+
95135
describe('autofocus', () => {
96136
beforeEach(() => {
97137
element = document.createElement('delegate-focus-mixin-element');

0 commit comments

Comments
 (0)