From 74191c3e92e218d1b7191da61da6c917f69dd206 Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Thu, 16 Mar 2017 16:34:11 +0100 Subject: [PATCH] fix(datetime): update selectedIndex according to ngModel value --- src/components/datetime/datetime.ts | 35 +++++++------------ src/components/datetime/test/issues/main.html | 15 ++++++++ src/components/picker/picker-component.ts | 4 +-- src/components/picker/picker.ts | 4 +++ src/util/datetime-util.ts | 7 ++-- 5 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/components/datetime/datetime.ts b/src/components/datetime/datetime.ts index 41e02ddda15..956cbfa6e00 100644 --- a/src/components/datetime/datetime.ts +++ b/src/components/datetime/datetime.ts @@ -409,7 +409,7 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces */ @Input() pickerOptions: any = {}; - /** + /** * @input {string} The text to display when there's no date selected yet. * Using lowercase to match the input attribute */ @@ -570,18 +570,17 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces }) }; - if (column.options.length) { - // cool, we've loaded up the columns with options - // preselect the option for this column - var selected = column.options.find(opt => opt.value === getValueFromFormat(this._value, format)); - if (selected) { - // set the select index for this column's options - column.selectedIndex = column.options.indexOf(selected); - } - - // add our newly created column to the picker - picker.addColumn(column); + // cool, we've loaded up the columns with options + // preselect the option for this column + var optValue = getValueFromFormat(this._value, format); + var selectedIndex = column.options.findIndex(opt => opt.value === optValue); + if (selectedIndex >= 0) { + // set the select index for this column's options + column.selectedIndex = selectedIndex; } + + // add our newly created column to the picker + picker.addColumn(column); }); const min = this._min; @@ -600,14 +599,6 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces } } - /** - * @private - */ - getColumn(name: string): PickerColumn { - const columns = this._picker.getColumns(); - return columns.find(col => col.name === name); - } - /** * @private */ @@ -615,7 +606,7 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces assert(lowerBounds.length === 5, 'lowerBounds length must be 5'); assert(upperBounds.length === 5, 'upperBounds length must be 5'); - const column = this.getColumn(name); + const column = this._picker.getColumn(name); if (!column) { return 0; } @@ -652,7 +643,7 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces const today = new Date(); const minCompareVal = dateDataSortValue(this._min); const maxCompareVal = dateDataSortValue(this._max); - const yearCol = this.getColumn('year'); + const yearCol = this._picker.getColumn('year'); assert(minCompareVal <= maxCompareVal, 'invalid min/max value'); diff --git a/src/components/datetime/test/issues/main.html b/src/components/datetime/test/issues/main.html index 2730633719f..cf6fb55c52d 100644 --- a/src/components/datetime/test/issues/main.html +++ b/src/components/datetime/test/issues/main.html @@ -68,4 +68,19 @@ > + + + displayFormat="D MMMM YYYY" + pickerFormat="D MMMM YYYY" + min="2017-03-08" + max="2020-12-31" + + + + diff --git a/src/components/picker/picker-component.ts b/src/components/picker/picker-component.ts index 8f72414629a..f313d40f110 100644 --- a/src/components/picker/picker-component.ts +++ b/src/components/picker/picker-component.ts @@ -422,7 +422,7 @@ export class PickerColumnCmp { } } - var selectedIndex = clamp(min, this.col.selectedIndex, max); + const selectedIndex = clamp(min, this.col.selectedIndex, max); if (selectedIndex !== this.col.selectedIndex) { var y = (selectedIndex * this.optHeight) * -1; @@ -512,7 +512,7 @@ export class PickerCmp { if (!isPresent(column.options)) { column.options = []; } - column.selectedIndex = 0; + column.selectedIndex = column.selectedIndex || 0; column.options = column.options.map(inputOpt => { let opt: PickerColumnOption = { text: '', diff --git a/src/components/picker/picker.ts b/src/components/picker/picker.ts index 95c01209b51..ac59310e812 100644 --- a/src/components/picker/picker.ts +++ b/src/components/picker/picker.ts @@ -53,6 +53,10 @@ export class Picker extends ViewController { return this.data.columns; } + getColumn(name: string): PickerColumn { + return this.getColumns().find(column => column.name === name); + } + refresh() { this._cmp && this._cmp.instance.refresh && this._cmp.instance.refresh(); } diff --git a/src/util/datetime-util.ts b/src/util/datetime-util.ts index 08f7dc41fe5..510bbf9643f 100644 --- a/src/util/datetime-util.ts +++ b/src/util/datetime-util.ts @@ -229,7 +229,7 @@ export function parseDate(val: any): DateTimeData { } -export function updateDate(existingData: DateTimeData, newData: any) { +export function updateDate(existingData: DateTimeData, newData: any): boolean { if (isPresent(newData) && newData !== '') { if (isString(newData)) { @@ -239,7 +239,7 @@ export function updateDate(existingData: DateTimeData, newData: any) { if (newData) { // successfully parsed the ISO string to our DateTimeData Object.assign(existingData, newData); - return; + return true; } } else if ((isPresent(newData.year) || isPresent(newData.hour) || isPresent(newData.month) || isPresent(newData.day) || isPresent(newData.minute) || isPresent(newData.second))) { @@ -262,7 +262,7 @@ export function updateDate(existingData: DateTimeData, newData: any) { (existingData)[k] = newData[k].value; } - return; + return true; } // eww, invalid data @@ -274,6 +274,7 @@ export function updateDate(existingData: DateTimeData, newData: any) { delete (existingData)[k]; } } + return false; }