Skip to content

Commit 87b9af9

Browse files
vaadin-botvursen
andauthored
fix: invoke setFocused only on events triggered by focusElement (#6060) (#6068)
Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
1 parent 9b6ea2a commit 87b9af9

2 files changed

Lines changed: 53 additions & 20 deletions

File tree

packages/component-base/src/delegate-focus-mixin.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export const DelegateFocusMixin = dedupingMixin(
166166
}
167167

168168
/**
169-
* @param {Event} event
169+
* @param {FocusEvent} event
170170
* @return {boolean}
171171
* @protected
172172
* @override
@@ -175,6 +175,16 @@ export const DelegateFocusMixin = dedupingMixin(
175175
return event.target === this.focusElement;
176176
}
177177

178+
/**
179+
* @param {FocusEvent} event
180+
* @return {boolean}
181+
* @protected
182+
* @override
183+
*/
184+
_shouldRemoveFocus(event) {
185+
return event.target === this.focusElement;
186+
}
187+
178188
/**
179189
* @param {boolean} disabled
180190
* @param {boolean} oldDisabled

packages/component-base/test/delegate-focus-mixin.test.js

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync, nextFrame } from '@vaadin/testing-helpers';
2+
import { fixtureSync, focusin, focusout, nextFrame } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
44
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
55
import { DelegateFocusMixin } from '../src/delegate-focus-mixin.js';
66

7-
customElements.define(
8-
'delegate-focus-mixin-element',
9-
class extends DelegateFocusMixin(PolymerElement) {
10-
static get template() {
11-
return html`<slot name="input"></slot>`;
12-
}
13-
14-
/** @protected */
15-
ready() {
16-
super.ready();
17-
18-
const input = this.querySelector('input');
19-
this._setFocusElement(input);
20-
}
21-
},
22-
);
23-
247
describe('delegate-focus-mixin', () => {
25-
let element, input;
8+
let element, input, setFocusedSpy;
9+
10+
customElements.define(
11+
'delegate-focus-mixin-element',
12+
class extends DelegateFocusMixin(PolymerElement) {
13+
static get template() {
14+
return html`
15+
<slot name="input"></slot>
16+
<slot name="suffix"></slot>
17+
`;
18+
}
19+
20+
/** @protected */
21+
ready() {
22+
super.ready();
23+
24+
const input = this.querySelector('input');
25+
this._setFocusElement(input);
26+
}
27+
28+
_setFocused(focused) {
29+
super._setFocused(focused);
30+
setFocusedSpy?.(focused);
31+
}
32+
},
33+
);
2634

2735
describe('default', () => {
36+
let button;
37+
2838
beforeEach(() => {
39+
setFocusedSpy = sinon.spy();
2940
element = fixtureSync(`
3041
<delegate-focus-mixin-element>
3142
<input slot="input" />
43+
<button slot="suffix"></button>
3244
</delegate-focus-mixin-element>
3345
`);
3446
input = element.querySelector('input');
47+
button = element.querySelector('button');
3548
});
3649

3750
it('should focus the input on focus call', () => {
@@ -113,6 +126,16 @@ describe('delegate-focus-mixin', () => {
113126
element._setFocusElement(target);
114127
expect(target.disabled).to.be.false;
115128
});
129+
130+
it('should not invoke setFocused when focusin is not triggered by focusElement', () => {
131+
focusin(button);
132+
expect(setFocusedSpy.called).to.be.false;
133+
});
134+
135+
it('should not invoke setFocused when focusout is not triggered by focusElement', () => {
136+
focusout(button);
137+
expect(setFocusedSpy.called).to.be.false;
138+
});
116139
});
117140

118141
describe('events', () => {

0 commit comments

Comments
 (0)