Skip to content

Commit c276ecf

Browse files
Suvish Varghese Thoovamalayilsimeg
Suvish Varghese Thoovamalayil
authored andcommitted
Allow specifying viewDate as a prop, fixes arqex#468
Optionally specify the month that is viewed when opening the date picker without having an explicit value set. The default behavior of `viewDate` is retained as well.
1 parent 06f14d1 commit c276ecf

File tree

6 files changed

+82
-8
lines changed

6 files changed

+82
-8
lines changed

DateTime.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ declare namespace ReactDatetimeClass {
4141
This prop is parsed by moment.js, so it is possible to use a date string or a moment.js date.
4242
*/
4343
defaultValue?: Date | string | Moment;
44+
/*
45+
Represents the month which is viewed on opening the calendar when there is no selected date.
46+
This prop is parsed by Moment.js, so it is possible to use a date `string` or a `moment` object.
47+
*/
48+
viewDate?: Date | string | Moment;
4449
/*
4550
Defines the format for the date. It accepts any moment.js date format.
4651
If true the date will be displayed using the defaults for the current locale.

DateTime.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var Datetime = createClass({
1313
propTypes: {
1414
// value: TYPES.object | TYPES.string,
1515
// defaultValue: TYPES.object | TYPES.string,
16+
// viewDate: TYPES.object | TYPES.string,
1617
onFocus: TYPES.func,
1718
onBlur: TYPES.func,
1819
onChange: TYPES.func,
@@ -43,24 +44,33 @@ var Datetime = createClass({
4344
return state;
4445
},
4546

47+
parseDate: function (date, formats) {
48+
var parsedDate;
49+
50+
if (date && typeof date === 'string')
51+
parsedDate = this.localMoment(date, formats.datetime);
52+
else if (date)
53+
parsedDate = this.localMoment(date);
54+
55+
if (parsedDate && !parsedDate.isValid())
56+
parsedDate = null;
57+
58+
return parsedDate;
59+
},
60+
4661
getStateFromProps: function( props ) {
4762
var formats = this.getFormats( props ),
4863
date = props.value || props.defaultValue,
4964
selectedDate, viewDate, updateOn, inputValue
5065
;
5166

52-
if ( date && typeof date === 'string' )
53-
selectedDate = this.localMoment( date, formats.datetime );
54-
else if ( date )
55-
selectedDate = this.localMoment( date );
67+
selectedDate = this.parseDate(date, formats);
5668

57-
if ( selectedDate && !selectedDate.isValid() )
58-
selectedDate = null;
69+
viewDate = this.parseDate(props.viewDate, formats);
5970

6071
viewDate = selectedDate ?
6172
selectedDate.clone().startOf('month') :
62-
this.localMoment().startOf('month')
63-
;
73+
viewDate ? viewDate.clone().startOf('month') : this.localMoment().startOf('month');
6474

6575
updateOn = this.getUpdateOn(formats);
6676

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ render: function() {
4343
| ------------ | ------- | ------- | ----------- |
4444
| **value** | `Date` | `new Date()` | Represents the selected date by the component, in order to use it as a [controlled component](https://facebook.github.io/react/docs/forms.html#controlled-components). This prop is parsed by Moment.js, so it is possible to use a date `string` or a `moment` object. |
4545
| **defaultValue** | `Date` | `new Date()` | Represents the selected date for the component to use it as a [uncontrolled component](https://facebook.github.io/react/docs/uncontrolled-components.html). This prop is parsed by Moment.js, so it is possible to use a date `string` or a `moment` object. |
46+
| **viewDate** | `Date` | `new Date()` | Represents the month which is viewed on opening the calendar when there is no selected date. This prop is parsed by Moment.js, so it is possible to use a date `string` or a `moment` object. |
4647
| **dateFormat** | `boolean` or `string` | `true` | Defines the format for the date. It accepts any [Moment.js date format](http://momentjs.com/docs/#/displaying/format/) (not in localized format). If `true` the date will be displayed using the defaults for the current locale. If `false` the datepicker is disabled and the component can be used as timepicker, see [available units docs](#specify-available-units). |
4748
| **timeFormat** | `boolean` or `string` | `true` | Defines the format for the time. It accepts any [Moment.js time format](http://momentjs.com/docs/#/displaying/format/) (not in localized format). If `true` the time will be displayed using the defaults for the current locale. If `false` the timepicker is disabled and the component can be used as datepicker, see [available units docs](#specify-available-units). |
4849
| **input** | `boolean` | `true` | Whether to show an input field to edit the date manually. |

react-datetime.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ declare module ReactDatetime {
2222
This prop is parsed by moment.js, so it is possible to use a date string or a moment.js date.
2323
*/
2424
defaultValue?: Date;
25+
/*
26+
Represents the month which is viewed on opening the calendar when there is no selected date.
27+
This prop is parsed by Moment.js, so it is possible to use a date `string` or a `moment` object.
28+
*/
29+
viewDate?: Date;
2530
/*
2631
Defines the format for the date. It accepts any moment.js date format.
2732
If true the date will be displayed using the defaults for the current locale.

test/testUtils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,9 @@ module.exports = {
120120

121121
getInputValue: (datetime) => {
122122
return datetime.find('.rdt > .form-control').getDOMNode().value;
123+
},
124+
125+
getViewDateValue: (datetime) => {
126+
return datetime.find('.rdtSwitch').getDOMNode().innerHTML;
123127
}
124128
};

test/tests.spec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,4 +1174,53 @@ describe('Datetime', () => {
11741174
});
11751175

11761176
});
1177+
1178+
describe('with viewDate', () => {
1179+
it('date value', () => {
1180+
const date = new Date(2000, 0, 15, 2, 2, 2, 2),
1181+
strDate = moment(date).format('MMMM YYYY'),
1182+
component = utils.createDatetime({ viewDate: date });
1183+
expect(utils.getViewDateValue(component)).toEqual(strDate);
1184+
});
1185+
1186+
it('moment value', () => {
1187+
const date = new Date(2000, 0, 15, 2, 2, 2, 2),
1188+
mDate = moment(date),
1189+
strDate = mDate.format('MMMM YYYY'),
1190+
component = utils.createDatetime({ viewDate: mDate });
1191+
expect(utils.getViewDateValue(component)).toEqual(strDate);
1192+
});
1193+
1194+
it('string value', () => {
1195+
const date = new Date(2000, 0, 15, 2, 2, 2, 2),
1196+
mDate = moment(date),
1197+
strDate = mDate.format('L') + ' ' + mDate.format('LT'),
1198+
expectedStrDate = mDate.format('MMMM YYYY'),
1199+
component = utils.createDatetime({ viewDate: strDate });
1200+
expect(utils.getViewDateValue(component)).toEqual(expectedStrDate);
1201+
});
1202+
1203+
it('UTC value from UTC string', () => {
1204+
const date = new Date(2000, 0, 15, 2, 2, 2, 2),
1205+
momentDateUTC = moment.utc(date),
1206+
strDateUTC = momentDateUTC.format('L') + ' ' + momentDateUTC.format('LT'),
1207+
expectedStrDate = momentDateUTC.format('MMMM YYYY'),
1208+
component = utils.createDatetime({ viewDate: strDateUTC, utc: true });
1209+
expect(utils.getViewDateValue(component)).toEqual(expectedStrDate);
1210+
});
1211+
1212+
it('invalid string value', () => {
1213+
const strDate = 'invalid string',
1214+
expectedStrDate = moment().format('MMMM YYYY'),
1215+
component = utils.createDatetime({ viewDate: strDate });
1216+
expect(utils.getViewDateValue(component)).toEqual(expectedStrDate);
1217+
});
1218+
1219+
it('invalid moment object', () => {
1220+
const mDate = moment(null),
1221+
expectedStrDate = moment().format('MMMM YYYY'),
1222+
component = utils.createDatetime({ viewDate: mDate });
1223+
expect(utils.getViewDateValue(component)).toEqual(expectedStrDate);
1224+
});
1225+
});
11771226
});

0 commit comments

Comments
 (0)