Skip to content

Commit 362f71c

Browse files
authored
fix: reset _hasInputValue on programmatic input change (#5625) (#5657)
1 parent d5ec1e0 commit 362f71c

3 files changed

Lines changed: 120 additions & 7 deletions

File tree

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,20 @@ export const DatePickerMixin = (subclass) =>
327327

328328
/** @private */
329329
_overlayContent: Object,
330+
331+
/**
332+
* In date-picker, unlike other components extending `InputMixin`,
333+
* the property indicates true only if the input has been entered by the user.
334+
* In the case of programmatic changes, the property is reset to false.
335+
* Read more about why this workaround is needed:
336+
* https://github.com/vaadin/web-components/issues/5639
337+
*
338+
* @protected
339+
* @override
340+
*/
341+
_hasInputValue: {
342+
type: Boolean,
343+
},
330344
};
331345
}
332346

@@ -352,6 +366,30 @@ export const DatePickerMixin = (subclass) =>
352366
this._boundOverlayRenderer = this._overlayRenderer.bind(this);
353367
}
354368

369+
/**
370+
* @override
371+
* @protected
372+
*/
373+
get _inputElementValue() {
374+
return super._inputElementValue;
375+
}
376+
377+
/**
378+
* The setter is overridden to reset the `_hasInputValue` property
379+
* to false when the input element's value is updated programmatically.
380+
* In date-picker, `_hasInputValue` is supposed to indicate true only
381+
* if the input has been entered by the user.
382+
* Read more about why this workaround is needed:
383+
* https://github.com/vaadin/web-components/issues/5639
384+
*
385+
* @override
386+
* @protected
387+
*/
388+
set _inputElementValue(value) {
389+
super._inputElementValue = value;
390+
this._hasInputValue = false;
391+
}
392+
355393
/**
356394
* Override a getter from `InputControlMixin` to make it optional
357395
* and to prevent warning when a clear button is missing,
@@ -959,8 +997,8 @@ export const DatePickerMixin = (subclass) =>
959997
*/
960998
_onClearButtonClick(event) {
961999
event.preventDefault();
962-
this.value = '';
9631000
this._inputElementValue = '';
1001+
this.value = '';
9641002
this.validate();
9651003
this.dispatchEvent(new CustomEvent('change', { bubbles: true }));
9661004
}

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

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ 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';
7-
import { close, open } from './helpers.js';
6+
import '../src/vaadin-date-picker.js';
7+
import { close, open, waitForScrollToFinish } from './helpers.js';
88

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

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ export const InputMixin = dedupingMixin(
5454
},
5555

5656
/**
57-
* Whether the input element has user input.
58-
*
59-
* Note, the property indicates true only if the input has been entered by the user.
60-
* In the case of programmatic changes, the property must be reset to false.
57+
* Whether the input element has a non-empty value.
6158
*
6259
* @protected
6360
*/

0 commit comments

Comments
 (0)