Skip to content

Commit eba92bd

Browse files
committed
Add the render props to customize appearance
1 parent 182c2b6 commit eba92bd

File tree

8 files changed

+112
-42
lines changed

8 files changed

+112
-42
lines changed

DateTime.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ var Datetime = React.createClass({
139139
;
140140
return function( e ){
141141
me.setState({
142-
viewDate: me.state.viewDate.clone()[ type ]( e.target.innerHTML ).startOf( type ),
142+
viewDate: me.state.viewDate.clone()[ type ]( parseInt(e.target.dataset.value) ).startOf( type ),
143143
currentView: nextViews[ type ]
144144
});
145145
};
@@ -206,7 +206,7 @@ var Datetime = React.createClass({
206206

207207
date = this.state.viewDate.clone()
208208
.month( this.state.viewDate.month() + modifier )
209-
.date( parseInt( target.innerHTML ) )
209+
.date( parseInt( target.dataset.value ) )
210210
.hours( currentDate.hours() )
211211
.minutes( currentDate.minutes() )
212212
.seconds( currentDate.seconds() )
@@ -237,7 +237,7 @@ var Datetime = React.createClass({
237237
},
238238

239239
componentProps: {
240-
fromProps: ['viewMode', 'minDate', 'maxDate'],
240+
fromProps: ['viewMode', 'minDate', 'maxDate', 'renderDay', 'renderMonth', 'renderYear'],
241241
fromState: ['viewDate', 'selectedDate' ],
242242
fromThis: ['setDate', 'setTime', 'showView', 'addTime', 'subtractTime', 'updateDate', 'localMoment']
243243
},

README.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ react-datetime
22
===============================
33
A date and time picker in the same React.js component. It can be used as a datepicker, timepicker or both at the same time.
44

5+
It allows to edit even date's milliseconds.
6+
57
This project started as a fork of https://github.com/quri/react-bootstrap-datetimepicker but the code and the API has changed a lot.
68

79
Usage
@@ -30,15 +32,18 @@ API
3032
| Name | Type | Default | Description |
3133
| ------------ | ------- | ------- | ----------- |
3234
| **date** | Date | new Date() | Represents the inital dateTime, this string is then parsed by moment.js |
33-
| **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. |
34-
| **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+
| **dateFormat** | string | Locale default | Defines the format moment.js should use to parse and output the date. The default is only set if there is not `timeFormat` defined. |
36+
| **timeFormat** | string | Locale default | Defines the format moment.js should use to parse and output the time. The default is only set if there is not `dateFormat` defined. |
3537
| **input** | boolean | true | Wether to show an input field to edit the date manually. |
3638
| **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).
3739
| **onChange** | function | x => console.log(x) | Callback trigger when the date changes |
3840
| **viewMode** | string or number | 'days' | The default view to display when the picker is shown. ('years', 'months', 'days', 'time') |
3941
| **inputProps** | object | undefined | Defines additional attributes for the input element of the component. |
4042
| **minDate** | moment | undefined | The earliest date allowed for entry in the calendar view. |
4143
| **maxDate** | moment | undefined | The latest date allowed for entry in the calendar view. |
44+
| **renderDay** | function | DOM.td( day ) | Customize the way that the days are shown in the day picker. The accepted function has the `selectedDate`, the current date and the default calculated `props` for the cell, and must return a React component. See [appearance customization](#appearance_customization) |
45+
| **renderMonth** | function | DOM.td( month ) | Customize the way that the months are shown in the month picker. The accepted function has the `selectedDate`, the current date and the default calculated `props` for the cell, and must return a React component. See [appearance customization](#appearance_customization) |
46+
| **renderYear** | function | DOM.td( year ) | Customize the way that the years are shown in the year picker. The accepted function has the `selectedDate`, the current date and the default calculated `props` for the cell, and must return a React component. See [appearance customization](#appearance_customization) |
4247

4348
## i18n
4449
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/).
@@ -56,6 +61,34 @@ If there are multiple locales loaded, you can use the prop `locale` to define wh
5661
```
5762
[Here you can see the i18n example working](http://codepen.io/arqex/pen/PqJMQV).
5863

64+
## Appearance customization
65+
It is possible to customize the way that the datetime picker display the days, months and years in the calendar. To adapt the calendar to every need it is possible to use the props `renderDay( props, currentDate, selectedDate )`, `renderMonth( props, month, year, selectedDate )` and `renderYear( props, year, selectedDate )` of react-datetime.
66+
67+
```js
68+
var MyDTPicker = React.createClass({
69+
render: function(){
70+
return <Datetime
71+
renderDay={ this.renderDay }
72+
renderMonth={ this.renderMonth }
73+
renderYear={ this.renderYear }
74+
/>;
75+
},
76+
renderDay: function( selectedDate, currentDate, props ){
77+
return <td {...props}>{ currentDate.date() }</td>;
78+
},
79+
renderMonth: function( selectedDate, currentMonthDate, props ){
80+
return <td {...props}>{ month }</td>;
81+
},
82+
renderDay: function( selectedDate, year, props ){
83+
return <td {...props}>{ currentDate.date() }</td>;
84+
}
85+
});
86+
```
87+
88+
* `props` is the object that react-date picker has calculated for this object. It is convenient to use this object as the props for your custom component, since it knows how to handle the click event and its `className` attribute is used by the default styles.
89+
* `selectedDate` and `currentDate` are Moment.js objects and can be used to change the output depending on the selected date, or the date for the current day.
90+
* `month` and `year` are the numeric representation of the current month and year to be displayed. Notice that the possible `month` values go from `0` to `11`.
91+
5992
Contributions
6093
===============================
6194
Any help is always welcome :)

dist/react-datetime.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-datetime.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-datetime",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"description": "A lightweight but complete datetime picker React.js component.",
55
"homepage": "https://github.com/arqex/react-datetime",
66
"repository": {

src/DaysView.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var DateTimePickerDays = React.createClass({
5353

5454
renderDays: function() {
5555
var date = this.props.viewDate,
56-
selected = this.props.selectedDate,
56+
selected = this.props.selectedDate.clone(),
5757
prevMonth = date.clone().subtract( 1, 'months' ),
5858
currentYear = date.year(),
5959
currentMonth = date.month(),
@@ -62,6 +62,7 @@ var DateTimePickerDays = React.createClass({
6262
maxDate = this.props.maxDate,
6363
weeks = [],
6464
days = [],
65+
renderer = this.props.renderDay || this.renderDay,
6566
classes, disabled, dayProps
6667
;
6768

@@ -87,11 +88,16 @@ var DateTimePickerDays = React.createClass({
8788
if( disabled )
8889
classes += ' disabled';
8990

90-
dayProps = { key: prevMonth.format('M_D'), className: classes };
91+
dayProps = {
92+
key: prevMonth.format('M_D'),
93+
'data-value': prevMonth.date(),
94+
className: classes
95+
};
9196
if( !disabled )
9297
dayProps.onClick = this.props.updateDate;
9398

94-
days.push( DOM.td( dayProps, prevMonth.date() ));
99+
days.push( renderer( dayProps, prevMonth.clone(), selected ) );
100+
95101
if( days.length == 7 ){
96102
weeks.push( DOM.tr( {key: prevMonth.format('M_D')}, days ) );
97103
days = [];
@@ -103,6 +109,10 @@ var DateTimePickerDays = React.createClass({
103109
return weeks;
104110
},
105111

112+
renderDay: function( props, currentDate, selectedDate ){
113+
return DOM.td( props, currentDate.date() );
114+
},
115+
106116
renderFooter: function(){
107117
if( !this.props.timeFormat )
108118
return '';

src/MonthsView.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,42 @@ moment = require('moment')
66

77
var DOM = React.DOM;
88
var DateTimePickerMonths = React.createClass({
9+
render: function() {
10+
return DOM.div({ className: 'rdtMonths' },[
11+
DOM.table({ key: 'a'}, DOM.thead({}, DOM.tr({},[
12+
DOM.th({ key: 'prev', className: 'prev' }, DOM.button({onClick: this.props.subtractTime(1, 'years')}, '‹')),
13+
DOM.th({ key: 'year', className: 'switch', onClick: this.props.showView('years'), colSpan: 2 }, this.props.viewDate.year() ),
14+
DOM.th({ key: 'next', className: 'next' }, DOM.button({onClick: this.props.addTime(1, 'years')}, '›'))
15+
]))),
16+
DOM.table({ key: 'months'}, DOM.tbody({ key: 'b'}, this.renderMonths()))
17+
]);
18+
},
19+
920
renderMonths: function() {
1021
var date = this.props.selectedDate,
1122
month = date.month(),
12-
monthsShort = date.localeData()._monthsShort,
23+
year = this.props.viewDate.year(),
1324
rows = [],
1425
i = 0,
1526
months = [],
16-
classes
27+
renderer = this.props.renderMonth || this.renderMonth,
28+
classes, props
1729
;
1830

1931
while (i < 12) {
2032
classes = "month";
21-
if( i === month && this.props.viewDate.year() === date.year() )
33+
if( i === month && year === date.year() )
2234
classes += " active";
2335

24-
months.push( DOM.td( {key: i, className: classes, onClick: this.props.setDate('month') }, monthsShort[ i ] ));
36+
props = {
37+
key: i,
38+
'data-value': i,
39+
className: classes,
40+
onClick: this.props.setDate('month')
41+
};
42+
43+
months.push( renderer( props, i, year, date.clone() ));
44+
2545
if( months.length == 4 ){
2646
rows.push( DOM.tr({ key: month + '_' + rows.length }, months) );
2747
months = [];
@@ -33,15 +53,8 @@ var DateTimePickerMonths = React.createClass({
3353
return rows;
3454
},
3555

36-
render: function() {
37-
return DOM.div({ className: 'rdtMonths' },[
38-
DOM.table({ key: 'a'}, DOM.thead({}, DOM.tr({},[
39-
DOM.th({ key: 'prev', className: 'prev' }, DOM.button({onClick: this.props.subtractTime(1, 'years')}, '‹')),
40-
DOM.th({ key: 'year', className: 'switch', onClick: this.props.showView('years'), colSpan: 2 }, this.props.viewDate.year() ),
41-
DOM.th({ key: 'next', className: 'next' }, DOM.button({onClick: this.props.addTime(1, 'years')}, '›'))
42-
]))),
43-
DOM.table({ key: 'months'}, DOM.tbody({ key: 'b'}, this.renderMonths()))
44-
]);
56+
renderMonth: function( props, month, year, selectedDate ) {
57+
return DOM.td( props, selectedDate.localeData()._monthsShort[ month ] );
4558
}
4659
});
4760

src/YearsView.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,25 @@ var React = require('react');
44

55
var DOM = React.DOM;
66
var DateTimePickerYears = React.createClass({
7+
render: function() {
8+
var year = parseInt(this.props.viewDate.year() / 10, 10) * 10;
9+
10+
return DOM.div({ className: 'rdtYears' },[
11+
DOM.table({ key: 'a'}, DOM.thead({}, DOM.tr({},[
12+
DOM.th({ key: 'prev', className: 'prev' }, DOM.button({onClick: this.props.subtractTime(10, 'years')}, '‹')),
13+
DOM.th({ key: 'year', className: 'switch', onClick: this.props.showView('years'), colSpan: 2 }, year + '-' + (year + 9) ),
14+
DOM.th({ key: 'next', className: 'next'}, DOM.button({onClick: this.props.addTime(10, 'years')}, '›'))
15+
]))),
16+
DOM.table({ key: 'years'}, DOM.tbody({}, this.renderYears( year )))
17+
]);
18+
},
19+
720
renderYears: function( year ) {
821
var years = [],
922
i = -1,
1023
rows = [],
11-
classes
24+
renderer = this.props.renderYear || this.renderYear,
25+
classes, props
1226
;
1327

1428
year--;
@@ -19,8 +33,15 @@ var DateTimePickerYears = React.createClass({
1933
if( this.props.selectedDate.year() === year )
2034
classes += ' active';
2135

22-
years.push( DOM.td({ key: year, className: classes, onClick: this.props.setDate('year') }, year ));
23-
// years.push(<td key={year} className={ classes } onClick={this.props.setDate('year')}>{year}</td>);
36+
props = {
37+
key: year,
38+
'data-value': year,
39+
className: classes,
40+
onClick: this.props.setDate('year')
41+
};
42+
43+
years.push( renderer( props, year, this.props.selectedDate.clone() ));
44+
2445
if( years.length == 4 ){
2546
rows.push( DOM.tr({ key: i }, years ) );
2647
years = [];
@@ -29,19 +50,12 @@ var DateTimePickerYears = React.createClass({
2950
year++;
3051
i++;
3152
}
53+
3254
return rows;
3355
},
34-
render: function() {
35-
var year = parseInt(this.props.viewDate.year() / 10, 10) * 10;
3656

37-
return DOM.div({ className: 'rdtYears' },[
38-
DOM.table({ key: 'a'}, DOM.thead({}, DOM.tr({},[
39-
DOM.th({ key: 'prev', className: 'prev' }, DOM.button({onClick: this.props.subtractTime(10, 'years')}, '‹')),
40-
DOM.th({ key: 'year', className: 'switch', onClick: this.props.showView('years'), colSpan: 2 }, year + '-' + (year + 9) ),
41-
DOM.th({ key: 'next', className: 'next'}, DOM.button({onClick: this.props.addTime(10, 'years')}, '›'))
42-
]))),
43-
DOM.table({ key: 'years'}, DOM.tbody({}, this.renderYears( year )))
44-
]);
57+
renderYear: function( props, year, selectedDate ){
58+
return DOM.td( props, year );
4559
}
4660
});
4761

0 commit comments

Comments
 (0)