Anna Kurylo
2018-10-16 99929e8c706aa7f6196ac4e02df1c04449feee7a
commit | author | age
d76f7b 1 'use strict';
M 2
11612b 3 var React = require('react'),
cf3d92 4     createClass = require('create-react-class'),
39b827 5     onClickOutside = require('react-onclickoutside').default
cf3d92 6     ;
d76f7b 7
84755c 8 var DateTimePickerMonths = onClickOutside( createClass({
eba92b 9     render: function() {
a50b2e 10         return React.createElement('div', { className: 'rdtMonths' }, [
GL 11             React.createElement('table', { key: 'a' }, React.createElement('thead', {}, React.createElement('tr', {}, [
12                 React.createElement('th', { key: 'prev', className: 'rdtPrev', onClick: this.props.subtractTime( 1, 'years' )}, React.createElement('span', {}, '‹' )),
99929e 13                 React.createElement('th', { key: 'year', className: 'rdtSwitch', onClick: this.props.showView( 'years' ), colspan: 2, 'data-value': this.props.viewDate.year() }, this.props.viewDate.year() ),
a50b2e 14                 React.createElement('th', { key: 'next', className: 'rdtNext', onClick: this.props.addTime( 1, 'years' )}, React.createElement('span', {}, '›' ))
eba92b 15             ]))),
a50b2e 16             React.createElement('table', { key: 'months' }, React.createElement('tbody', { key: 'b' }, this.renderMonths()))
eba92b 17         ]);
M 18     },
19
d76f7b 20     renderMonths: function() {
c37f80 21         var date = this.props.selectedDate,
62fd2f 22             month = this.props.viewDate.month(),
eba92b 23             year = this.props.viewDate.year(),
d76f7b 24             rows = [],
M 25             i = 0,
26             months = [],
eba92b 27             renderer = this.props.renderMonth || this.renderMonth,
bdad6c 28             isValid = this.props.isValidDate || this.alwaysValidDate,
f0ec5a 29             classes, props, currentMonth, isDisabled, noOfDaysInMonth, daysInMonth, validDay,
SE 30             // Date is irrelevant because we're only interested in month
31             irrelevantDate = 1
cf3d92 32             ;
d76f7b 33
M 34         while (i < 12) {
462115 35             classes = 'rdtMonth';
b8112d 36             currentMonth =
bdad6c 37                 this.props.viewDate.clone().set({ year: year, month: i, date: irrelevantDate });
c306f2 38
bdad6c 39             noOfDaysInMonth = currentMonth.endOf( 'month' ).format( 'D' );
SE 40             daysInMonth = Array.from({ length: noOfDaysInMonth }, function( e, i ) {
41                 return i + 1;
42             });
43
44             validDay = daysInMonth.find(function( d ) {
45                 var day = currentMonth.clone().set( 'date', d );
46                 return isValid( day );
47             });
48
49             isDisabled = ( validDay === undefined );
50
51             if ( isDisabled )
c306f2 52                 classes += ' rdtDisabled';
R 53
c987d1 54             if ( date && i === date.month() && year === date.year() )
462115 55                 classes += ' rdtActive';
d76f7b 56
eba92b 57             props = {
M 58                 key: i,
59                 'data-value': i,
c306f2 60                 className: classes
eba92b 61             };
c306f2 62
bdad6c 63             if ( !isDisabled )
SE 64                 props.onClick = ( this.props.updateOn === 'months' ?
f0ec5a 65                     this.updateSelectedMonth : this.props.setDate( 'month' ) );
eba92b 66
f0ec5a 67             months.push( renderer( props, i, year, date && date.clone() ) );
eba92b 68
bdad6c 69             if ( months.length === 4 ) {
a50b2e 70                 rows.push( React.createElement('tr', { key: month + '_' + rows.length }, months ) );
d76f7b 71                 months = [];
M 72             }
73
74             i++;
75         }
76
77         return rows;
78     },
79
f99908 80     updateSelectedMonth: function( event ) {
11612b 81         this.props.updateSelectedDate( event );
f99908 82     },
SA 83
462115 84     renderMonth: function( props, month ) {
98310d 85         var localMoment = this.props.viewDate;
f0ec5a 86         var monthStr = localMoment.localeData().monthsShort( localMoment.month( month ) );
98310d 87         var strLength = 3;
SE 88         // Because some months are up to 5 characters long, we want to
89         // use a fixed string length for consistency
f0ec5a 90         var monthStrFixedLength = monthStr.substring( 0, strLength );
a50b2e 91         return React.createElement('td', props, capitalize( monthStrFixedLength ) );
c306f2 92     },
b8112d 93
bdad6c 94     alwaysValidDate: function() {
c306f2 95         return 1;
11612b 96     },
JM 97
cf3d92 98     handleClickOutside: function() {
SE 99         this.props.handleClickOutside();
100     }
11612b 101 }));
d76f7b 102
f0ec5a 103 function capitalize( str ) {
SE 104     return str.charAt( 0 ).toUpperCase() + str.slice( 1 );
ce7cf5 105 }
GK 106
d76f7b 107 module.exports = DateTimePickerMonths;