Skip to content

Commit 1db8cfc

Browse files
[DatePicker] add proporty needed for Intl
1 parent ecab894 commit 1db8cfc

File tree

7 files changed

+88
-116
lines changed

7 files changed

+88
-116
lines changed

docs/src/app/components/pages/components/date-picker.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ export default class DatePickerPage extends React.Component {
2929
{
3030
name: 'Props',
3131
infoArray: [
32+
{
33+
name: 'DateTimeFormat',
34+
type: 'func',
35+
header: 'default: custom one that only support en-US locale',
36+
desc: 'Constructor for time formatting. Follow this specificaction: ' +
37+
'ECMAScript Internationalization API 1.0 (ECMA-402).',
38+
},
39+
{
40+
name: 'locale',
41+
type: 'string',
42+
header: 'default: en-US',
43+
desc: 'Locale used for formating date. If you are not using the default value, ' +
44+
'you have to provide a DateTimeFormat that support it. You can use Intl.DateTimeFormat' +
45+
' if it\'s supported by your environment. https://github.com/andyearnshaw/Intl.js is a good polyfill.',
46+
},
47+
{
48+
name: 'wording',
49+
type: 'object',
50+
header: 'default: {ok: \'OK\', cancel: \'Cancel\' }',
51+
desc: 'Wording used inside the button of the dialog.',
52+
},
3253
{
3354
name: 'autoOk',
3455
type: 'bool',

src/date-picker/calendar-toolbar.jsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const React = require('react');
2-
const DateTime = require('../utils/date-time');
32
const IconButton = require('../icon-button');
43
const Toolbar = require('../toolbar/toolbar');
54
const ToolbarGroup = require('../toolbar/toolbar-group');
@@ -44,6 +43,8 @@ const CalendarToolbar = React.createClass({
4443
},
4544

4645
propTypes: {
46+
DateTimeFormat: React.PropTypes.func.isRequired,
47+
locale: React.PropTypes.string.isRequired,
4748
displayDate: React.PropTypes.object.isRequired,
4849
nextMonth: React.PropTypes.bool,
4950
onMonthChange: React.PropTypes.func,
@@ -81,10 +82,16 @@ const CalendarToolbar = React.createClass({
8182
},
8283

8384
render() {
84-
const dateTimeFormated = new DateTime.DateTimeFormat('en-US', {
85+
const {
86+
DateTimeFormat,
87+
locale,
88+
displayDate,
89+
} = this.props
90+
91+
const dateTimeFormated = new DateTimeFormat(locale, {
8592
month: 'long',
8693
year: 'numeric',
87-
}).format(this.props.displayDate);
94+
}).format(displayDate);
8895

8996
const nextButtonIcon = this.state.muiTheme.isRtl ? <NavigationChevronRight /> : <NavigationChevronLeft />;
9097
const prevButtonIcon = this.state.muiTheme.isRtl ? <NavigationChevronLeft /> : <NavigationChevronRight />;

src/date-picker/calendar.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const Calendar = React.createClass({
3434
},
3535

3636
propTypes: {
37+
DateTimeFormat: React.PropTypes.func.isRequired,
38+
locale: React.PropTypes.string.isRequired,
3739
disableYearSelection: React.PropTypes.bool,
3840
initialDate: React.PropTypes.object,
3941
isActive: React.PropTypes.bool,
@@ -133,10 +135,16 @@ const Calendar = React.createClass({
133135
};
134136

135137
const weekTitleDayStyle = this.prepareStyles(styles.weekTitleDay);
138+
const {
139+
DateTimeFormat,
140+
locale,
141+
} = this.props;
136142

137143
return (
138144
<ClearFix style={this.mergeStyles(styles.root)}>
139145
<DateDisplay
146+
DateTimeFormat={DateTimeFormat}
147+
locale={locale}
140148
disableYearSelection={this.props.disableYearSelection}
141149
style={styles.dateDisplay}
142150
selectedDate={this.state.selectedDate}
@@ -148,6 +156,8 @@ const Calendar = React.createClass({
148156
{this.state.displayMonthDay &&
149157
<div style={this.prepareStyles(styles.calendarContainer)}>
150158
<CalendarToolbar
159+
DateTimeFormat={DateTimeFormat}
160+
locale={locale}
151161
displayDate={this.state.displayDate}
152162
onMonthChange={this._handleMonthChange}
153163
prevMonth={toolbarInteractions.prevMonth}

src/date-picker/date-display.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const React = require('react');
22
const StylePropable = require('../mixins/style-propable');
3-
const DateTime = require('../utils/date-time');
43
const Transitions = require('../styles/transitions');
5-
const AutoPrefix = require('../styles/auto-prefix');
64
const SlideInTransitionGroup = require('../transition-groups/slide-in');
75
const DefaultRawTheme = require('../styles/raw-themes/light-raw-theme');
86
const ThemeManager = require('../styles/theme-manager');
@@ -16,6 +14,8 @@ const DateDisplay = React.createClass({
1614
},
1715

1816
propTypes: {
17+
DateTimeFormat: React.PropTypes.func.isRequired,
18+
locale: React.PropTypes.string.isRequired,
1919
disableYearSelection: React.PropTypes.bool,
2020
monthDaySelected: React.PropTypes.bool,
2121
selectedDate: React.PropTypes.object.isRequired,
@@ -125,14 +125,16 @@ const DateDisplay = React.createClass({
125125

126126
render() {
127127
let {
128+
DateTimeFormat,
129+
locale,
128130
selectedDate,
129131
style,
130132
...other,
131133
} = this.props;
132134
const year = this.props.selectedDate.getFullYear();
133135
const styles = this.getStyles();
134136

135-
const dateTimeFormated = new DateTime.DateTimeFormat('en-US', {
137+
const dateTimeFormated = new DateTimeFormat(locale, {
136138
month: 'short',
137139
weekday: 'short',
138140
day: '2-digit',

src/date-picker/date-picker-dialog.jsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const Dialog = require('../dialog');
1010
const FlatButton = require('../flat-button');
1111
const DefaultRawTheme = require('../styles/raw-themes/light-raw-theme');
1212
const ThemeManager = require('../styles/theme-manager');
13+
const DateTime = require('../utils/date-time');
1314

1415
const DatePickerDialog = React.createClass({
1516

@@ -38,6 +39,9 @@ const DatePickerDialog = React.createClass({
3839
},
3940

4041
propTypes: {
42+
DateTimeFormat: React.PropTypes.func,
43+
locale: React.PropTypes.string,
44+
wordings: React.PropTypes.object,
4145
disableYearSelection: React.PropTypes.bool,
4246
initialDate: React.PropTypes.object,
4347
maxDate: React.PropTypes.object,
@@ -61,6 +65,17 @@ const DatePickerDialog = React.createClass({
6165
};
6266
},
6367

68+
getDefaultProps: function() {
69+
return {
70+
DateTimeFormat: DateTime.DateTimeFormat,
71+
locale: 'en-US',
72+
wordings: {
73+
ok: 'OK',
74+
cancel: 'Cancel',
75+
},
76+
};
77+
},
78+
6479
windowListeners: {
6580
keyup: '_handleWindowKeyUp',
6681
},
@@ -81,6 +96,9 @@ const DatePickerDialog = React.createClass({
8196

8297
render() {
8398
let {
99+
DateTimeFormat,
100+
locale,
101+
wordings,
84102
initialDate,
85103
onAccept,
86104
style,
@@ -113,7 +131,7 @@ const DatePickerDialog = React.createClass({
113131
let actions = [
114132
<FlatButton
115133
key={0}
116-
label="Cancel"
134+
label={wordings.cancel}
117135
secondary={true}
118136
style={styles.actions}
119137
onTouchTap={this._handleCancelTouchTap} />,
@@ -123,7 +141,7 @@ const DatePickerDialog = React.createClass({
123141
actions.push(
124142
<FlatButton
125143
key={1}
126-
label="OK"
144+
label={wordings.ok}
127145
secondary={true}
128146
disabled={this.refs.calendar !== undefined && this.refs.calendar.isSelectedDateDisabled()}
129147
style={styles.actions}
@@ -143,6 +161,8 @@ const DatePickerDialog = React.createClass({
143161
onClickAway={this._handleDialogClickAway}
144162
repositionOnUpdate={false}>
145163
<Calendar
164+
DateTimeFormat={DateTimeFormat}
165+
locale={locale}
146166
ref="calendar"
147167
onDayTouchTap={this._onDayTouchTap}
148168
initialDate={this.props.initialDate}

src/date-picker/date-picker.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const DatePicker = React.createClass({
2828
},
2929

3030
propTypes: {
31+
DateTimeFormat: React.PropTypes.func,
32+
locale: React.PropTypes.string,
33+
wordings: React.PropTypes.object,
3134
autoOk: React.PropTypes.bool,
3235
defaultDate: React.PropTypes.object,
3336
formatDate: React.PropTypes.func,
@@ -80,6 +83,9 @@ const DatePicker = React.createClass({
8083

8184
render() {
8285
let {
86+
DateTimeFormat,
87+
locale,
88+
wordings,
8389
autoOk,
8490
defaultDate,
8591
formatDate,
@@ -108,6 +114,9 @@ const DatePicker = React.createClass({
108114
onTouchTap={this._handleInputTouchTap}/>
109115
<DatePickerDialog
110116
ref="dialogWindow"
117+
DateTimeFormat={DateTimeFormat}
118+
locale={locale}
119+
wordings={wordings}
111120
mode={mode}
112121
initialDate={this.state.dialogDate}
113122
onAccept={this._handleDialogAccept}

src/utils/date-time.js

Lines changed: 11 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11

2+
const dayList = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
3+
const monthList = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
4+
'Oct', 'Nov', 'Dec'];
5+
const monthLongList = ['January', 'February', 'March', 'April', 'May', 'June',
6+
'July', 'August', 'September', 'October', 'November', 'December'];
7+
28
function DateTimeFormat(locale, options) {
39
this.options = options;
410

511
if (process.env.NODE_ENV !== 'production' && locale !== 'en-US') {
6-
console.warn('Wrong usage of DateTimeFormat');
12+
console.warn('Wrong usage of DateTimeFormat. The ' + locale +' locale is not supported.');
713
}
814

915
this.format = function(date) {
@@ -13,116 +19,13 @@ function DateTimeFormat(locale, options) {
1319
options.weekday === 'short' &&
1420
options.day === '2-digit') {
1521

16-
const day = date.getDay();
17-
switch (day) {
18-
case 0:
19-
output = 'Sun';
20-
break;
21-
case 1:
22-
output = 'Mon';
23-
break;
24-
case 2:
25-
output = 'Tue';
26-
break;
27-
case 3:
28-
output = 'Wed';
29-
break;
30-
case 4:
31-
output = 'Thu';
32-
break;
33-
case 5:
34-
output = 'Fri';
35-
break;
36-
case 6:
37-
output = 'Sat';
38-
break;
39-
}
40-
41-
output += ', ';
42-
43-
const month = date.getMonth();
44-
switch (month) {
45-
case 0:
46-
output += 'Jan';
47-
break;
48-
case 1:
49-
output += 'Feb';
50-
break;
51-
case 2:
52-
output += 'Mar';
53-
break;
54-
case 3:
55-
output += 'Apr';
56-
break;
57-
case 4:
58-
output += 'May';
59-
break;
60-
case 5:
61-
output += 'Jun';
62-
break;
63-
case 6:
64-
output += 'Jul';
65-
break;
66-
case 7:
67-
output += 'Aug';
68-
break;
69-
case 8:
70-
output += 'Sep';
71-
break;
72-
case 9:
73-
output += 'Oct';
74-
break;
75-
case 10:
76-
output += 'Nov';
77-
break;
78-
case 11:
79-
output += 'Dec';
80-
break;
81-
}
82-
83-
output += ' ' + date.getDate()
22+
output = dayList[date.getDay()] + ', ';
23+
output += monthList[date.getMonth()] + ' ';
24+
output += date.getDate();
8425
} else if (options.month === 'long'
8526
&& options.year === 'numeric') {
8627

87-
switch (date.getMonth()) {
88-
case 0:
89-
output = 'January';
90-
break;
91-
case 1:
92-
output = 'February';
93-
break;
94-
case 2:
95-
output = 'March';
96-
break;
97-
case 3:
98-
output = 'April';
99-
break;
100-
case 4:
101-
output = 'May';
102-
break;
103-
case 5:
104-
output = 'June';
105-
break;
106-
case 6:
107-
output = 'July';
108-
break;
109-
case 7:
110-
output = 'August';
111-
break;
112-
case 8:
113-
output = 'September';
114-
break;
115-
case 9:
116-
output = 'October';
117-
break;
118-
case 10:
119-
output = 'November';
120-
break;
121-
case 11:
122-
output = 'December';
123-
break;
124-
}
125-
28+
output = monthLongList[date.getMonth()];
12629
output += ' ' + date.getFullYear();
12730
} else if (process.env.NODE_ENV !== 'production') {
12831
console.warn('Wrong usage of DateTimeFormat');

0 commit comments

Comments
 (0)