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

feat(datetime): Use current date/time in datetime picker if it's empty #11555

Closed
wants to merge 1 commit 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
8 changes: 6 additions & 2 deletions src/components/datetime/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Form } from '../../util/form';
import { BaseInput } from '../../util/base-input';
import { Item } from '../item/item';
import { deepCopy, isBlank, isPresent, isArray, isObject, isString, assert, clamp } from '../../util/util';
import { dateValueRange, renderDateTime, renderTextFormat, convertDataToISO, convertFormatToKey, getValueFromFormat, parseTemplate, parseDate, updateDate, DateTimeData, daysInMonth, dateSortValue, dateDataSortValue, LocaleData } from '../../util/datetime-util';
import { dateValueRange, renderDateTime, renderTextFormat, convertDataToISO, convertFormatToKey, getValueFromFormat, parseTemplate, parseDate, updateDate, DateTimeData, daysInMonth, dateSortValue, dateDataSortValue, LocaleData, nowDateTimeData } from '../../util/datetime-util';

/**
* @name DateTime
Expand Down Expand Up @@ -584,7 +584,11 @@ export class DateTime extends BaseInput<DateTimeData> implements AfterContentIni

// cool, we've loaded up the columns with options
// preselect the option for this column
const optValue = getValueFromFormat(this.getValue(), format);
let optValue = getValueFromFormat(this.getValue(), format);
if (!isPresent(optValue)) {
// Default to current date/time
optValue = getValueFromFormat(nowDateTimeData(), format);
}
const selectedIndex = column.options.findIndex(opt => opt.value === optValue);
if (selectedIndex >= 0) {
// set the select index for this column's options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,9 @@
<ion-datetime [(ngModel)]="noFormatDate"></ion-datetime>
</ion-item>

<ion-item>
<ion-label>Default selected values in picker</ion-label>
<ion-datetime displayFormat="MMM DD, YYYY HH:mm"></ion-datetime>
</ion-item>

</ion-content>
16 changes: 16 additions & 0 deletions src/components/datetime/test/datetime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,22 @@ describe('DateTime', () => {
expect(columns[0].options[10].value).toEqual(2000);
});

it('should default to current date/time if there is no selected value', () => {
datetime.max = '2100-01-01';
datetime.min = '2000-01-01';
datetime.pickerFormat = 'YYYY MM DD HH mm';

datetime.generate();
var columns = picker.getColumns();

var now = new Date();
expect(columns[0].options[columns[0].selectedIndex].value).toEqual(now.getFullYear());
expect(columns[1].options[columns[1].selectedIndex].value).toEqual(now.getMonth() + 1);
expect(columns[2].options[columns[2].selectedIndex].value).toEqual(now.getDate());
expect(columns[3].options[columns[3].selectedIndex].value).toEqual(now.getHours());
expect(columns[4].options[columns[4].selectedIndex].value).toEqual(now.getMinutes());
});

});

describe('calcMinMax', () => {
Expand Down
14 changes: 14 additions & 0 deletions src/util/datetime-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,20 @@ export function convertDataToISO(data: DateTimeData): string {
return rtn;
}

export function nowDateTimeData(): DateTimeData {
const now = new Date();
return {
year: now.getFullYear(),
month: now.getMonth() + 1,
day: now.getDate(),
hour: now.getHours(),
minute: now.getMinutes(),
second: now.getSeconds(),
millisecond: now.getMilliseconds(),
tzOffset: now.getTimezoneOffset(),
};
}

function twoDigit(val: number): string {
return ('0' + (isPresent(val) ? Math.abs(val) : '0')).slice(-2);
}
Expand Down