Skip to content

Commit

Permalink
fix: fire change event after value-changed (#3531)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan committed Mar 9, 2022
1 parent 247ba84 commit 8e44e1e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
23 changes: 19 additions & 4 deletions packages/date-picker/src/vaadin-date-picker-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,25 @@ export const DatePickerMixin = (subclass) =>
this.opened = false;
}

/**
* Override Polymer lifecycle callback to dispatch `change` event if needed.
* This is necessary to ensure `change` is fired after `value-changed`.
*
* @param {!Object} currentProps Current accessor values
* @param {?Object} changedProps Properties changed since the last call
* @param {?Object} oldProps Previous values for each changed property
* @protected
* @override
*/
_propertiesChanged(currentProps, changedProps, oldProps) {
super._propertiesChanged(currentProps, changedProps, oldProps);

if ('value' in changedProps && this.__dispatchChange) {
this.dispatchEvent(new CustomEvent('change', { bubbles: true }));
this.__dispatchChange = false;
}
}

/**
* Opens the dropdown.
*/
Expand Down Expand Up @@ -691,10 +710,6 @@ export const DatePickerMixin = (subclass) =>

/** @private */
_valueChanged(value, oldValue) {
if (this.__dispatchChange) {
this.dispatchEvent(new CustomEvent('change', { bubbles: true }));
this.__dispatchChange = false;
}
this._handleDateChange('_selectedDate', value, oldValue);

this._toggleHasValue(!!value);
Expand Down
11 changes: 11 additions & 0 deletions packages/date-picker/test/keyboard-input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,17 @@ describe('keyboard', () => {
expect(spy.called).to.be.true;
});

it('should fire change after value-changed event', async () => {
const valueChangedSpy = sinon.spy();
datepicker.addEventListener('value-changed', valueChangedSpy);

await sendKeys({ type: '1/2/2000' });
await sendKeys({ press: 'Enter' });

expect(valueChangedSpy.calledOnce).to.be.true;
expect(spy.calledAfter(valueChangedSpy)).to.be.true;
});

it('should fire change on selecting date with Enter', async () => {
// Open the overlay
await sendKeys({ press: 'ArrowDown' });
Expand Down

0 comments on commit 8e44e1e

Please sign in to comment.