Skip to content

Commit 74191c3

Browse files
committed
fix(datetime): update selectedIndex according to ngModel value
1 parent afd99ba commit 74191c3

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

src/components/datetime/datetime.ts

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces
409409
*/
410410
@Input() pickerOptions: any = {};
411411

412-
/**
412+
/**
413413
* @input {string} The text to display when there's no date selected yet.
414414
* Using lowercase to match the input attribute
415415
*/
@@ -570,18 +570,17 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces
570570
})
571571
};
572572

573-
if (column.options.length) {
574-
// cool, we've loaded up the columns with options
575-
// preselect the option for this column
576-
var selected = column.options.find(opt => opt.value === getValueFromFormat(this._value, format));
577-
if (selected) {
578-
// set the select index for this column's options
579-
column.selectedIndex = column.options.indexOf(selected);
580-
}
581-
582-
// add our newly created column to the picker
583-
picker.addColumn(column);
573+
// cool, we've loaded up the columns with options
574+
// preselect the option for this column
575+
var optValue = getValueFromFormat(this._value, format);
576+
var selectedIndex = column.options.findIndex(opt => opt.value === optValue);
577+
if (selectedIndex >= 0) {
578+
// set the select index for this column's options
579+
column.selectedIndex = selectedIndex;
584580
}
581+
582+
// add our newly created column to the picker
583+
picker.addColumn(column);
585584
});
586585

587586
const min = <any>this._min;
@@ -600,22 +599,14 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces
600599
}
601600
}
602601

603-
/**
604-
* @private
605-
*/
606-
getColumn(name: string): PickerColumn {
607-
const columns = this._picker.getColumns();
608-
return columns.find(col => col.name === name);
609-
}
610-
611602
/**
612603
* @private
613604
*/
614605
validateColumn(name: string, index: number, min: number, max: number, lowerBounds: number[], upperBounds: number[]): number {
615606
assert(lowerBounds.length === 5, 'lowerBounds length must be 5');
616607
assert(upperBounds.length === 5, 'upperBounds length must be 5');
617608

618-
const column = this.getColumn(name);
609+
const column = this._picker.getColumn(name);
619610
if (!column) {
620611
return 0;
621612
}
@@ -652,7 +643,7 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces
652643
const today = new Date();
653644
const minCompareVal = dateDataSortValue(this._min);
654645
const maxCompareVal = dateDataSortValue(this._max);
655-
const yearCol = this.getColumn('year');
646+
const yearCol = this._picker.getColumn('year');
656647

657648
assert(minCompareVal <= maxCompareVal, 'invalid min/max value');
658649

src/components/datetime/test/issues/main.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,19 @@
6868
></ion-datetime>
6969
</ion-item>
7070

71+
<ion-item>
72+
<ion-label>
73+
displayFormat="D MMMM YYYY"
74+
pickerFormat="D MMMM YYYY"
75+
min="2017-03-08"
76+
max="2020-12-31"
77+
</ion-label>
78+
<ion-datetime
79+
displayFormat="D MMMM YYYY"
80+
pickerFormat="D MMMM YYYY"
81+
min="2017-03-08"
82+
max="2020-12-31"
83+
></ion-datetime>
84+
</ion-item>
85+
7186
</ion-content>

src/components/picker/picker-component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ export class PickerColumnCmp {
422422
}
423423
}
424424

425-
var selectedIndex = clamp(min, this.col.selectedIndex, max);
425+
const selectedIndex = clamp(min, this.col.selectedIndex, max);
426426

427427
if (selectedIndex !== this.col.selectedIndex) {
428428
var y = (selectedIndex * this.optHeight) * -1;
@@ -512,7 +512,7 @@ export class PickerCmp {
512512
if (!isPresent(column.options)) {
513513
column.options = [];
514514
}
515-
column.selectedIndex = 0;
515+
column.selectedIndex = column.selectedIndex || 0;
516516
column.options = column.options.map(inputOpt => {
517517
let opt: PickerColumnOption = {
518518
text: '',

src/components/picker/picker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export class Picker extends ViewController {
5353
return this.data.columns;
5454
}
5555

56+
getColumn(name: string): PickerColumn {
57+
return this.getColumns().find(column => column.name === name);
58+
}
59+
5660
refresh() {
5761
this._cmp && this._cmp.instance.refresh && this._cmp.instance.refresh();
5862
}

src/util/datetime-util.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export function parseDate(val: any): DateTimeData {
229229
}
230230

231231

232-
export function updateDate(existingData: DateTimeData, newData: any) {
232+
export function updateDate(existingData: DateTimeData, newData: any): boolean {
233233
if (isPresent(newData) && newData !== '') {
234234

235235
if (isString(newData)) {
@@ -239,7 +239,7 @@ export function updateDate(existingData: DateTimeData, newData: any) {
239239
if (newData) {
240240
// successfully parsed the ISO string to our DateTimeData
241241
Object.assign(existingData, newData);
242-
return;
242+
return true;
243243
}
244244

245245
} 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) {
262262
(<any>existingData)[k] = newData[k].value;
263263
}
264264

265-
return;
265+
return true;
266266
}
267267

268268
// eww, invalid data
@@ -274,6 +274,7 @@ export function updateDate(existingData: DateTimeData, newData: any) {
274274
delete (<any>existingData)[k];
275275
}
276276
}
277+
return false;
277278
}
278279

279280

0 commit comments

Comments
 (0)