Skip to content

Commit 90b6daa

Browse files
authored
fix: prevent initial validation for date-picker when no constraints (#4169)
1 parent e97a739 commit 90b6daa

2 files changed

Lines changed: 64 additions & 6 deletions

File tree

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ export const DatePickerMixin = (subclass) =>
4646
*/
4747
value: {
4848
type: String,
49-
observer: '_valueChanged',
5049
notify: true,
5150
value: '',
5251
},
@@ -688,21 +687,36 @@ export const DatePickerMixin = (subclass) =>
688687
}
689688
}
690689

691-
/** @private */
690+
/**
691+
* Override the value observer from `InputMixin` to implement custom
692+
* handling of the `value` property. The date-picker doesn't forward
693+
* the value directly to the input like the default implementation of `InputMixin`.
694+
* Instead, it parses the value into a date, puts it in `_selectedDate` which
695+
* is then displayed in the input with respect to the specified date format.
696+
*
697+
* @param {string | undefined} value
698+
* @param {string | undefined} oldValue
699+
* @protected
700+
* @override
701+
*/
692702
_valueChanged(value, oldValue) {
693703
const newDate = this._parseDate(value);
694704

695-
// The new value cannot be parsed, revert the old value.
696705
if (value && !newDate) {
706+
// The new value cannot be parsed, revert the old value.
697707
this.value = oldValue;
698708
return;
699709
}
700710

701711
if (value) {
702-
// Only update the date instance if the date has actually changed.
703712
if (!dateEquals(this._selectedDate, newDate)) {
713+
// Update the date instance only if the date has actually changed.
704714
this._selectedDate = newDate;
705-
this.validate();
715+
716+
if (oldValue !== undefined) {
717+
// Validate only if `value` changes after initialization.
718+
this.validate();
719+
}
706720
}
707721
} else {
708722
this._selectedDate = null;

‎packages/date-picker/test/form-input.test.js‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync } from '@vaadin/testing-helpers';
2+
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
44
import './not-animated-styles.js';
55
import { DatePicker } from '../vaadin-date-picker.js';
@@ -16,6 +16,40 @@ customElements.define('vaadin-date-picker-2016', DatePicker2016);
1616
describe('form input', () => {
1717
let datePicker;
1818

19+
describe('initial', () => {
20+
let validateSpy;
21+
22+
beforeEach(() => {
23+
datePicker = document.createElement('vaadin-date-picker');
24+
validateSpy = sinon.spy(datePicker, 'validate');
25+
});
26+
27+
afterEach(() => {
28+
datePicker.remove();
29+
});
30+
31+
it('should not validate by default', async () => {
32+
document.body.appendChild(datePicker);
33+
await nextRender();
34+
expect(validateSpy.called).to.be.false;
35+
});
36+
37+
it('should not validate when the field has an initial value', async () => {
38+
datePicker.value = '2020-01-01';
39+
document.body.appendChild(datePicker);
40+
await nextRender();
41+
expect(validateSpy.called).to.be.false;
42+
});
43+
44+
it('should not validate when the field has an initial value and invalid', async () => {
45+
datePicker.value = '2020-01-01';
46+
datePicker.invalid = true;
47+
document.body.appendChild(datePicker);
48+
await nextRender();
49+
expect(validateSpy.called).to.be.false;
50+
});
51+
});
52+
1953
describe('basic', () => {
2054
let validateSpy;
2155

@@ -47,13 +81,23 @@ describe('form input', () => {
4781
expect(validateSpy.calledOnce).to.be.true;
4882
});
4983

84+
it('should not validate on min change when no value is provided', () => {
85+
datePicker.min = '2020-01-01';
86+
expect(validateSpy.called).to.be.false;
87+
});
88+
5089
it('should validate on min change when a value is provided', () => {
5190
datePicker.value = '2020-01-01';
5291
validateSpy.resetHistory();
5392
datePicker.min = '2020-01-01';
5493
expect(validateSpy.calledOnce).to.be.true;
5594
});
5695

96+
it('should not validate on max change when no value is provided', () => {
97+
datePicker.max = '2020-01-01';
98+
expect(validateSpy.called).to.be.false;
99+
});
100+
57101
it('should validate on max change when a value is provided', () => {
58102
datePicker.value = '2020-01-01';
59103
validateSpy.resetHistory();

0 commit comments

Comments
 (0)