Skip to content

fix(datetime):change default year when max value is defined #7933

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/components/datetime/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,27 @@ export class DateTime implements AfterContentInit, ControlValueAccessor, OnDestr
let yearOpt: PickerColumnOption;
let monthOpt: PickerColumnOption;
let dayOpt: PickerColumnOption;

// default to max date (if set) to be sure to have a proper date

let selectedYear = this._max ? this._max.year : today.getFullYear();

if (this._max && monthCol.selectedIndex == undefined) {
for (i = 0; i < monthCol.options.length; i++) {
if (monthCol.options[i].value ==this._max.month ) {
monthCol.selectedIndex = i
}
}
}

if (this._max && dayCol.selectedIndex == undefined) {
for (i = 0; i < dayCol.options.length; i++) {
if (dayCol.options[i].value ==this._max.day ) {
dayCol.selectedIndex = i
}
}
}

// default to assuming today's year
let selectedYear = today.getFullYear();
if (yearCol) {
yearOpt = yearCol.options[yearCol.selectedIndex];
if (yearOpt) {
Expand Down
11 changes: 11 additions & 0 deletions src/components/datetime/test/datetime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ describe('DateTime', () => {
expect(columns[1].options[30].disabled).toEqual(true);
});

it('should generate correct default date when max year <> today year', () => {
datetime.max = '2010-12-31';
var picker = new Picker();
datetime.generate(picker);
datetime.validate(picker);
var columns = picker.getColumns();
expect(columns[0].options[0].disabled).toEqual(false);
});

});

describe('generate', () => {
Expand Down Expand Up @@ -204,6 +213,8 @@ describe('DateTime', () => {
expect(columns[0].options[10].value).toEqual(2000);
});



});

describe('calcMinMax', () => {
Expand Down