Skip to content

Commit b006c49

Browse files
authored
fix: reset _hasInputValue on programmatic input change (#5625)
1 parent 6eb1e18 commit b006c49

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

‎packages/date-picker/src/vaadin-date-picker-mixin.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,9 @@ export const DatePickerMixin = (subclass) =>
908908
/** @private */
909909
_applyInputValue(date) {
910910
this._inputElementValue = date ? this._getFormattedDate(this.i18n.formatDate, date) : '';
911+
// It is no longer input entered by the user,
912+
// so reset the `hasInputValue` property.
913+
this._hasInputValue = false;
911914
}
912915

913916
/** @private */
@@ -972,8 +975,11 @@ export const DatePickerMixin = (subclass) =>
972975
*/
973976
_onClearButtonClick(event) {
974977
event.preventDefault();
975-
this.value = '';
976978
this._inputElementValue = '';
979+
// It is no longer input entered by the user,
980+
// so reset the `hasInputValue` property.
981+
this._hasInputValue = false;
982+
this.value = '';
977983
this.validate();
978984
this.dispatchEvent(new CustomEvent('change', { bubbles: true }));
979985
}

‎packages/date-picker/test/events.test.js‎

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
33
import { sendKeys } from '@web/test-runner-commands';
44
import sinon from 'sinon';
55
import './not-animated-styles.js';
6-
import '../vaadin-date-picker.js';
6+
import '../src/vaadin-date-picker.js';
77
import { close, open, waitForScrollToFinish } from './helpers.js';
88

99
describe('events', () => {
@@ -110,4 +110,82 @@ describe('events', () => {
110110
expect(changeSpy.called).to.be.false;
111111
});
112112
});
113+
114+
describe('has-input-value-changed event', () => {
115+
let clearButton, hasInputValueChangedSpy, valueChangedSpy;
116+
117+
beforeEach(async () => {
118+
hasInputValueChangedSpy = sinon.spy();
119+
valueChangedSpy = sinon.spy();
120+
datePicker = fixtureSync('<vaadin-date-picker clear-button-visible></vaadin-date-picker>');
121+
clearButton = datePicker.shadowRoot.querySelector('[part=clear-button]');
122+
datePicker.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
123+
datePicker.addEventListener('value-changed', valueChangedSpy);
124+
datePicker.inputElement.focus();
125+
});
126+
127+
describe('without value', () => {
128+
describe('with user input', () => {
129+
beforeEach(async () => {
130+
await sendKeys({ type: '1/1/2022' });
131+
await waitForScrollToFinish(datePicker._overlayContent);
132+
hasInputValueChangedSpy.resetHistory();
133+
});
134+
135+
it('should be fired when clearing the user input with Esc', async () => {
136+
await sendKeys({ press: 'Escape' });
137+
expect(datePicker.inputElement.value).to.be.empty;
138+
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
139+
});
140+
141+
it('should be fired when comitting the user input with Enter', async () => {
142+
await sendKeys({ press: 'Enter' });
143+
expect(valueChangedSpy.calledOnce).to.be.true;
144+
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
145+
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
146+
});
147+
148+
it('should be fired when selecting a date with Space', async () => {
149+
// Move focus inside the dropdown to the typed date.
150+
await sendKeys({ press: 'ArrowDown' });
151+
await waitForScrollToFinish(datePicker._overlayContent);
152+
// Select that date.
153+
await sendKeys({ press: 'Space' });
154+
expect(valueChangedSpy.calledOnce).to.be.true;
155+
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
156+
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
157+
});
158+
});
159+
});
160+
161+
describe('with value', () => {
162+
beforeEach(async () => {
163+
await sendKeys({ type: '1/1/2022' });
164+
await sendKeys({ press: 'Enter' });
165+
valueChangedSpy.resetHistory();
166+
hasInputValueChangedSpy.resetHistory();
167+
});
168+
169+
describe('with user input', () => {
170+
beforeEach(async () => {
171+
await sendKeys({ type: 'foo' });
172+
hasInputValueChangedSpy.resetHistory();
173+
});
174+
175+
it('should be fired on clear button click', () => {
176+
clearButton.click();
177+
expect(datePicker.inputElement.value).to.be.empty;
178+
expect(valueChangedSpy.calledOnce).to.be.true;
179+
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
180+
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
181+
});
182+
183+
it('should be fired when reverting the user input to the value with Esc', async () => {
184+
await sendKeys({ press: 'Escape' });
185+
expect(datePicker.inputElement.value).to.equal('1/1/2022');
186+
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
187+
});
188+
});
189+
});
190+
});
113191
});

0 commit comments

Comments
 (0)