fix(datetime): not always disabling day values when dayValues set

This commit is contained in:
Felix Livni
2017-03-03 19:00:22 -08:00
committed by Manu Mtz.-Almeida
parent 53feb3f699
commit eff420f4c7
2 changed files with 29 additions and 22 deletions

View File

@ -623,6 +623,23 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces
} }
} }
// create sort values for the min/max datetimes
let minCompareVal = dateDataSortValue(this._min);
let maxCompareVal = dateDataSortValue(this._max);
if (monthCol) {
// enable/disable which months are valid
// to show within the min/max date range
for (let i = 0; i < monthCol.options.length; i++) {
monthOpt = monthCol.options[i];
// loop through each month and see if it
// is within the min/max date range
monthOpt.disabled = (dateSortValue(selectedYear, monthOpt.value, 31) < minCompareVal ||
dateSortValue(selectedYear, monthOpt.value, 1) > maxCompareVal);
}
}
// default to assuming this month has 31 days // default to assuming this month has 31 days
let numDaysInMonth = 31; let numDaysInMonth = 31;
let selectedMonth: number; let selectedMonth: number;
@ -637,28 +654,11 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces
} }
} }
// create sort values for the min/max datetimes
let minCompareVal = dateDataSortValue(this._min);
let maxCompareVal = dateDataSortValue(this._max);
if (monthCol) {
// enable/disable which months are valid
// to show within the min/max date range
for (i = 0; i < monthCol.options.length; i++) {
monthOpt = monthCol.options[i];
// loop through each month and see if it
// is within the min/max date range
monthOpt.disabled = (dateSortValue(selectedYear, monthOpt.value, 31) < minCompareVal ||
dateSortValue(selectedYear, monthOpt.value, 1) > maxCompareVal);
}
}
if (dayCol) { if (dayCol) {
if (isPresent(selectedMonth)) { if (isPresent(selectedMonth)) {
// enable/disable which days are valid // enable/disable which days are valid
// to show within the min/max date range // to show within the min/max date range
for (i = 0; i < dayCol.options.length; i++) { for (let i = 0; i < dayCol.options.length; i++) {
dayOpt = dayCol.options[i]; dayOpt = dayCol.options[i];
// loop through each day and see if it // loop through each day and see if it
@ -667,13 +667,14 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces
dayOpt.disabled = (compareVal < minCompareVal || dayOpt.disabled = (compareVal < minCompareVal ||
compareVal > maxCompareVal || compareVal > maxCompareVal ||
numDaysInMonth <= i); numDaysInMonth < dayOpt.value);
} }
} else { } else {
// enable/disable which numbers of days to show in this month // enable/disable which numbers of days to show in this month
for (i = 0; i < dayCol.options.length; i++) { for (let i = 0; i < dayCol.options.length; i++) {
dayCol.options[i].disabled = (numDaysInMonth <= i); dayOpt = dayCol.options[i];
dayOpt.disabled = (numDaysInMonth < dayOpt.value);
} }
} }
} }

View File

@ -107,7 +107,7 @@ describe('DateTime', () => {
it('should enable all of the values given', () => { it('should enable all of the values given', () => {
datetime.monthValues = '6,7,8'; datetime.monthValues = '6,7,8';
datetime.dayValues = '01,02,03,04,05,06,08,09,10, 11, 12, 13, 14'; datetime.dayValues = '01,02,03,04,05,06,08,09,10, 11, 12, 13, 31';
datetime.yearValues = '2014,2015'; datetime.yearValues = '2014,2015';
datetime.pickerFormat = 'MM DD YYYY'; datetime.pickerFormat = 'MM DD YYYY';
@ -121,6 +121,7 @@ describe('DateTime', () => {
expect(columns[1].options.length).toEqual(13); // days expect(columns[1].options.length).toEqual(13); // days
expect(columns[2].options.length).toEqual(2); // years expect(columns[2].options.length).toEqual(2); // years
columns[0].selectedIndex = 1; // July
datetime.validate(picker); datetime.validate(picker);
// Months // Months
@ -132,6 +133,11 @@ describe('DateTime', () => {
for (var i = 0; i < columns[1].options.length; i++) { for (var i = 0; i < columns[1].options.length; i++) {
expect(columns[1].options[i].disabled).toEqual(false); expect(columns[1].options[i].disabled).toEqual(false);
} }
columns[0].selectedIndex = 0; // June
datetime.validate(picker);
expect(columns[1].options[12].disabled).toEqual(true);
}); });
}); });