diff --git a/src/components/day/Day.js b/src/components/day/Day.js index 3ab93a91df..5ac35d0e3a 100644 --- a/src/components/day/Day.js +++ b/src/components/day/Day.js @@ -26,7 +26,8 @@ export default class DayComponent extends Field { required: false } }, - dayFirst: false + dayFirst: false, + defaultValue: '' }, ...extend); } @@ -386,20 +387,25 @@ export default class DayComponent extends Field { const [DAY, MONTH, YEAR] = this.component.dayFirst ? [0, 1, 2] : [1, 0, 2]; const defaultValue = this.component.defaultValue ? this.component.defaultValue.split('/') : ''; - const getNextPart = (shouldTake, defaultValue) => - dateParts.push(shouldTake ? valueParts.shift() : defaultValue); + const getNextPart = (shouldTake, defaultValue) => { + // Only push the part if it's not an empty string + const part = shouldTake ? valueParts.shift() : defaultValue; + if (part !== '') { + dateParts.push(part); + } + } if (this.dayFirst) { - getNextPart(this.showDay, defaultValue ? defaultValue[DAY] : '00'); + getNextPart(this.showDay, defaultValue ? defaultValue[DAY] : ''); } - getNextPart(this.showMonth, defaultValue ? defaultValue[MONTH] : '00'); + getNextPart(this.showMonth, defaultValue ? defaultValue[MONTH] : ''); if (!this.dayFirst) { - getNextPart(this.showDay, defaultValue ? defaultValue[DAY] : '00'); + getNextPart(this.showDay, defaultValue ? defaultValue[DAY] : ''); } - getNextPart(this.showYear, defaultValue ? defaultValue[YEAR] : '0000'); + getNextPart(this.showYear, defaultValue ? defaultValue[YEAR] : ''); return dateParts.join('/'); } @@ -628,10 +634,23 @@ export default class DayComponent extends Field { } const [DAY, MONTH, YEAR] = this.component.dayFirst ? [0, 1, 2] : [1, 0, 2]; const values = value.split('/'); + if(values.length < 3){ + return true; + } return (values[DAY] === '00' || values[MONTH] === '00' || values[YEAR] === '0000'); } getValidationFormat() { - return this.dayFirst ? 'DD-MM-YYYY' : 'MM-DD-YYYY'; + let validationFormat = this.dayFirst ? 'DD-MM-YYYY' : 'MM-DD-YYYY'; + if (this.fields?.day?.hide) { + validationFormat = validationFormat.replace('DD-', ''); + } + if (this.fields?.month?.hide) { + validationFormat = validationFormat.replace('MM-', ''); + } + if ( this.fields?.year?.hide ) { + validationFormat = validationFormat.replace('-YYYY', ''); + } + return validationFormat; } } diff --git a/src/components/day/Day.unit.js b/src/components/day/Day.unit.js index bc6d64cb3d..6fe8f7944d 100644 --- a/src/components/day/Day.unit.js +++ b/src/components/day/Day.unit.js @@ -197,6 +197,38 @@ describe('Day Component', () => { }); }); + it('Should set value if the day field is hidden', (done) => { + comp1.dayFirst = false; + comp1.fields.day.hide = true; + Harness.testCreate(DayComponent, comp1).then((component) => { + component.setValue('12/2023'); + assert.equal(component.data.date, '12/2023'); + done(); + }); + comp1.fields.day.hide = false; + }); + + it('Should set value if the month field is hidden', (done) => { + comp1.fields.month.hide = true; + Harness.testCreate(DayComponent, comp1).then((component) => { + component.setValue('12/2023'); + assert.equal(component.data.date, '12/2023'); + done(); + }); + comp1.fields.month.hide = false; + }); + + it('Should set value if the year field is hidden', (done) => { + comp1.fields.year.hide = true; + Harness.testCreate(DayComponent, comp1).then((component) => { + component.setValue('12/21'); + assert.equal(component.data.date, '12/21'); + done(); + }); + comp1.fields.year.hide = false; + }); + + it('Should use the default day value if the day field is hidden', (done) => { comp1.dayFirst = false; comp1.defaultValue = '00/01/0000';