Skip to content

Commit 559f4d3

Browse files
committed
feat(datetime): default to now or max. Fixes #9846
1 parent f1a676e commit 559f4d3

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/components/datetime/datetime.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ export class DateTime extends BaseInput<DateTimeData> implements AfterContentIni
510510
if (this.isFocus() || this._disabled) {
511511
return;
512512
}
513+
513514
console.debug('datetime, open picker');
514515

515516
// the user may have assigned some options specifically for the alert
@@ -548,6 +549,11 @@ export class DateTime extends BaseInput<DateTimeData> implements AfterContentIni
548549
* @hidden
549550
*/
550551
generate() {
552+
// If the date doesn't have a value yet, set it to now or the max value
553+
if (Object.keys(this._value).length === 0) {
554+
this._value = this.getDefaultValue();
555+
}
556+
551557
const picker = this._picker;
552558
// if a picker format wasn't provided, then fallback
553559
// to use the display format
@@ -838,6 +844,17 @@ export class DateTime extends BaseInput<DateTimeData> implements AfterContentIni
838844
}
839845
}
840846

847+
/**
848+
* Get the default value to show when none is provided,
849+
* and within the bounds of the max value
850+
* @private
851+
*/
852+
getDefaultValue() {
853+
if (this.max) {
854+
return parseDate(this.max);
855+
}
856+
return parseDate((new Date).toISOString());
857+
}
841858
}
842859

843860
/**

src/components/datetime/test/datetime.spec.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DateTime } from '../datetime';
44
import { Form } from '../../../util/form';
55
import { Picker } from '../../picker/picker';
66
import { PickerController } from '../../picker/picker-controller';
7-
import * as datetime from '../../../util/datetime-util';
7+
import * as datetimeUtil from '../../../util/datetime-util';
88
import { mockApp, mockConfig, mockElementRef, mockRenderer } from '../../../util/mock-providers';
99

1010

@@ -14,6 +14,26 @@ describe('DateTime', () => {
1414

1515
describe('validate', () => {
1616

17+
fit('should default to now if no initial value or bounds supplied', () => {
18+
const now = datetimeUtil.parseDate(new Date().toISOString());
19+
datetime.generate();
20+
const val = datetime.getValue();
21+
expect(val.year).toEqual(now.year);
22+
expect(val.month).toEqual(now.month);
23+
expect(val.day).toEqual(now.day);
24+
expect(val.hour).toEqual(now.hour);
25+
expect(val.minute).toEqual(now.minute);
26+
});
27+
28+
fit('should default to max if no initial value supplied but max specified', () => {
29+
datetime.max = '1987-10-19';
30+
datetime.generate();
31+
const val = datetime.getValue();
32+
expect(val.year).toEqual(1987);
33+
expect(val.month).toEqual(10);
34+
expect(val.day).toEqual(19);
35+
});
36+
1737
it('should restrict January 1-14, 2000 from selection, then allow it, and restrict December 15-31, 2001', () => {
1838
datetime.max = '2001-12-15';
1939
datetime.min = '2000-01-15';
@@ -683,7 +703,7 @@ describe('DateTime', () => {
683703
console.warn = function(){};
684704

685705
// pt-br
686-
var customLocale: datetime.LocaleData = {
706+
var customLocale: datetimeUtil.LocaleData = {
687707
dayNames: [
688708
'domingo',
689709
'segunda-feira',

0 commit comments

Comments
 (0)