Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from
Aug 26, 2024
31 changes: 23 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 @@ -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('/');
}
Expand Down Expand Up @@ -628,7 +634,16 @@ export default class DayComponent extends Field {
}
const [DAY, MONTH, YEAR] = this.component.dayFirst ? [0, 1, 2] : [1, 0, 2];
const values = value.split('/');
return (values[DAY] === '00' || values[MONTH] === '00' || values[YEAR] === '0000');
// return (values[DAY] === '00' || values[MONTH] === '00' || values[YEAR] === '0000');



return (values[DAY] === '00' ||
values[MONTH] === '00' ||
values[YEAR] === '0000' ||
values[DAY] === '' ||
values[MONTH] === '' ||
values[YEAR] === '');
}

getValidationFormat() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this method still work when some part of date is hidden?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Expand Down
32 changes: 32 additions & 0 deletions src/components/day/Day.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading