Skip to content

Commit c37f80e

Browse files
committed
Locale support added
1 parent a3a33b2 commit c37f80e

File tree

6 files changed

+90
-37
lines changed

6 files changed

+90
-37
lines changed

DateTime.js

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var assign = require('object-assign'),
99
moment = require('moment')
1010
;
1111

12+
var TYPES = React.PropTypes;
1213
var Datetime = React.createClass({
1314
mixins: [
1415
require('react-onclickoutside')
@@ -20,15 +21,16 @@ var Datetime = React.createClass({
2021
time: TimeView
2122
},
2223
propTypes: {
23-
date: React.PropTypes.object,
24-
onChange: React.PropTypes.func,
25-
input: React.PropTypes.bool,
26-
dateFormat: React.PropTypes.string,
27-
timeFormat: React.PropTypes.string,
28-
inputProps: React.PropTypes.object,
29-
viewMode: React.PropTypes.oneOf(['years', 'months', 'days', 'time']),
30-
minDate: React.PropTypes.object,
31-
maxDate: React.PropTypes.object
24+
date: TYPES.object,
25+
onChange: TYPES.func,
26+
locale: TYPES.string,
27+
input: TYPES.bool,
28+
dateFormat: TYPES.string,
29+
timeFormat: TYPES.string,
30+
inputProps: TYPES.object,
31+
viewMode: TYPES.oneOf(['years', 'months', 'days', 'time']),
32+
minDate: TYPES.object,
33+
maxDate: TYPES.object
3234
},
3335
getDefaultProps: function() {
3436

@@ -50,18 +52,20 @@ var Datetime = React.createClass({
5052
currentView: this.props.viewMode,
5153
open: !this.props.input,
5254
inputFormat: formats.datetime,
53-
viewDate: moment(date).startOf("month"),
54-
selectedDate: moment(date),
55-
inputValue: moment(date).format( formats.datetime )
55+
viewDate: this.localMoment(date).startOf("month"),
56+
selectedDate: this.localMoment(date),
57+
inputValue: this.localMoment(date).format( formats.datetime )
5658
};
5759
},
5860

5961
getFormats: function( props ){
6062
var formats = {
61-
date: '',
62-
time: '',
63-
datetime: ''
64-
};
63+
date: '',
64+
time: '',
65+
datetime: ''
66+
},
67+
locale = this.localMoment( props.date ).localeData()
68+
;
6569

6670
if( props.dateFormat ){
6771
formats.date = props.dateFormat;
@@ -71,9 +75,9 @@ var Datetime = React.createClass({
7175
}
7276

7377
if( !formats.date && !formats.time ){
74-
formats.date = 'MM/DD/YY';
75-
formats.time = 'H:mm';
76-
formats.datetime = 'MM/DD/YY H:mm';
78+
formats.date = locale.longDateFormat('L');
79+
formats.time = locale.longDateFormat('LT');
80+
formats.datetime = formats.date + ' ' + formats.time;
7781
}
7882
else {
7983
if( props.dateFormat ){
@@ -101,18 +105,21 @@ var Datetime = React.createClass({
101105
},
102106

103107
onChange: function(event) {
104-
var value = event.target == null ? event : event.target.value;
105-
if (moment(value).isValid()) {
108+
var value = event.target == null ? event : event.target.value,
109+
localMoment = this.localMoment( date )
110+
;
111+
112+
if (localMoment.isValid()) {
106113
this.setState({
107-
selectedDate: moment(value),
108-
viewDate: moment(value).startOf("month")
114+
selectedDate: localMoment,
115+
viewDate: localMoment.clone().startOf("month")
109116
});
110117
}
111118

112119
return this.setState({
113120
inputValue: value
114121
}, function() {
115-
return this.props.onChange(moment(this.state.inputValue, this.state.inputFormat, true).format( this.state.inputFormat ));
122+
return this.props.onChange( localMoment.toDate() );
116123
});
117124
},
118125

@@ -187,9 +194,9 @@ var Datetime = React.createClass({
187194

188195
updateDate: function( e ) {
189196
var target = e.target,
190-
modifier = 0,
191-
currentDate = this.state.selectedDate,
192-
date
197+
modifier = 0,
198+
currentDate = this.state.selectedDate,
199+
date
193200
;
194201

195202
if(target.className.indexOf("new") != -1)
@@ -222,10 +229,17 @@ var Datetime = React.createClass({
222229
this.setState({ open: false });
223230
},
224231

232+
localMoment: function( date ){
233+
var m = moment( date );
234+
if( this.props.locale )
235+
m.locale( this.props.locale );
236+
return m;
237+
},
238+
225239
componentProps: {
226240
fromProps: ['viewMode', 'minDate', 'maxDate'],
227241
fromState: ['viewDate', 'selectedDate' ],
228-
fromThis: ['setDate', 'setTime', 'showView', 'addTime', 'subtractTime', 'updateDate']
242+
fromThis: ['setDate', 'setTime', 'showView', 'addTime', 'subtractTime', 'updateDate', 'localMoment']
229243
},
230244

231245
getComponentProps: function(){

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,30 @@ API
3232
| **date** | Date | new Date() | Represents the inital dateTime, this string is then parsed by moment.js |
3333
| **dateFormat** | string | "MM/DD/YY" | Defines the format moment.js should use to parse and output the date. The default is only set if there is not `timeFormat` defined. |
3434
| **timeFormat** | string | "MM/DD/YY" | Defines the format moment.js should use to parse and output the time. The default is only set if there is not `dateFormat` defined. |
35+
| **input** | boolean | true | Wether to show an input field to edit the date manually. |
36+
| **locale** | string | null | Manually set the locale for the react-datetime instance. Moment.js locale needs to be loaded to be used, see [i18n docs](#i18n).
3537
| **onChange** | function | x => console.log(x) | Callback trigger when the date changes |
3638
| **viewMode** | string or number | 'days' | The default view to display when the picker is shown. ('years', 'months', 'days', 'time') |
3739
| **inputProps** | object | undefined | Defines additional attributes for the input element of the component. |
3840
| **minDate** | moment | undefined | The earliest date allowed for entry in the calendar view. |
3941
| **maxDate** | moment | undefined | The latest date allowed for entry in the calendar view. |
4042

43+
## i18n
44+
Different language and date formats are supported by react-datetime. React uses [moment.js](http://momentjs.com/) to format the dates, and the easiest way of changing the language of the calendar is [changing the moment.js locale](http://momentjs.com/docs/#/i18n/changing-locale/).
45+
46+
```js
47+
var moment = require('moment');
48+
require('moment/locale/fr');
49+
// Now react-datetime will be in french
50+
```
51+
52+
If there are multiple locales loaded, you can use the prop `locale` to define what language should be used by the instance:
53+
```js
54+
<Datetime locale="fr" />
55+
<Datetime locale="de" />
56+
```
57+
[Here you can see the i18n example working](http://codepen.io/arqex/pen/PqJMQV).
58+
4159
Contributions
4260
===============================
4361
Any help is always welcome :)

0 commit comments

Comments
 (0)