Skip to content

Commit

Permalink
fix(vwc-textfield): dispatch focus/blur events programmatically (#697)
Browse files Browse the repository at this point in the history
* fix(vwc-textfield): dispatch focus/blur events programmatically

* test focus & blur

* small adjusts

Co-authored-by: Yuri Guller <gullerya@gmail.com>
  • Loading branch information
yinonov and gullerya committed Mar 11, 2021
1 parent 4d31499 commit ff68295
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
11 changes: 9 additions & 2 deletions components/textfield/src/vwc-textfield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,15 @@ export class VWCTextField extends MWCTextField {
const fe = this.formElement;

// event listeners
fe.onfocus = this.onInputFocus.bind(this);
fe.onblur = this.onInputBlur.bind(this);
fe.onfocus = () => {
this.dispatchEvent(new FocusEvent('focus', { composed: true }));
this.onInputFocus();
};

fe.onblur = () => {
this.dispatchEvent(new FocusEvent('blur', { composed: true }));
this.onInputBlur();
};

// attributes
setAttributeByValue('id', this.id, fe);
Expand Down
22 changes: 22 additions & 0 deletions components/textfield/test/textfield.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ describe('textfield', () => {
assertComputedStyle(te, { pointerEvents: 'none' });
});

describe('events', () => {
it(`should trigger ${COMPONENT_NAME} focus & blur`, async function () {
let count = 0;
const [textfield] = addElement(
textToDomToParent(`<${COMPONENT_NAME}></${COMPONENT_NAME}>`)
);
await waitNextTask();
textfield.addEventListener('focus', () => count++);
textfield.addEventListener('blur', () => count++);
const input = textfield.querySelector('input');
input.focus();
await waitNextTask();
expect(textfield.focused).true;

input.blur();
await waitNextTask();
expect(textfield.focused).false;

expect(count).to.equal(2);
});
});

describe('typography', () => {
typographyTestCases(COMPONENT_NAME);
});
Expand Down

0 comments on commit ff68295

Please sign in to comment.