Skip to content

Commit

Permalink
FIO-8798: updated shape of the data of day component with hidden fiel…
Browse files Browse the repository at this point in the history
…ds (#5760)

* FIO-8798: updated shape of the data of day component with hidden fields

* FIO-8798: add conditionOperatorsSettings for Day component

* FIO-8798: update core version

* FIO-8798: add yarn.lock

* Revert "FIO-8798: add yarn.lock"

This reverts commit 80c5127.

* Revert "FIO-8798: update core version"

This reverts commit 12a30c3.

* FIO-8798: update validation format

* FIO-8798: update partial day check
  • Loading branch information
Maria-Golomb authored and lane-formio committed Sep 5, 2024
1 parent 08242f1 commit e6711cb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/components/day/Day.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export default class DayComponent extends Field {
required: false
}
},
dayFirst: false
dayFirst: false,
defaultValue: ''
}, ...extend);
}

Expand Down Expand Up @@ -387,20 +388,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('/');
}
Expand Down Expand Up @@ -633,10 +639,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;
}
}
32 changes: 32 additions & 0 deletions src/components/day/Day.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,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';
Expand Down

0 comments on commit e6711cb

Please sign in to comment.