1+ 'use strict' ;
2+
3+ Object . defineProperty ( exports , '__esModule' , {
4+ value : true
5+ } ) ;
6+
7+ var _extends = Object . assign || function ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] ; for ( var key in source ) { if ( Object . prototype . hasOwnProperty . call ( source , key ) ) { target [ key ] = source [ key ] ; } } } return target ; } ;
8+
9+ var _createClass = ( function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( 'value' in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ) ( ) ;
10+
11+ var _get = function get ( _x , _x2 , _x3 ) { var _again = true ; _function: while ( _again ) { var object = _x , property = _x2 , receiver = _x3 ; desc = parent = getter = undefined ; _again = false ; if ( object === null ) object = Function . prototype ; var desc = Object . getOwnPropertyDescriptor ( object , property ) ; if ( desc === undefined ) { var parent = Object . getPrototypeOf ( object ) ; if ( parent === null ) { return undefined ; } else { _x = parent ; _x2 = property ; _x3 = receiver ; _again = true ; continue _function; } } else if ( 'value' in desc ) { return desc . value ; } else { var getter = desc . get ; if ( getter === undefined ) { return undefined ; } return getter . call ( receiver ) ; } } } ;
12+
13+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { 'default' : obj } ; }
14+
15+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( 'Cannot call a class as a function' ) ; } }
16+
17+ function _inherits ( subClass , superClass ) { if ( typeof superClass !== 'function' && superClass !== null ) { throw new TypeError ( 'Super expression must either be null or a function, not ' + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ; }
18+
19+ var _react = require ( 'react' ) ;
20+
21+ var _react2 = _interopRequireDefault ( _react ) ;
22+
23+ var _moment = require ( 'moment' ) ;
24+
25+ var _moment2 = _interopRequireDefault ( _moment ) ;
26+
27+ var _utilsParseInputJs = require ( './utils/parseInput.js' ) ;
28+
29+ var _utilsParseInputJs2 = _interopRequireDefault ( _utilsParseInputJs ) ;
30+
31+ var _DayCellJs = require ( './DayCell.js' ) ;
32+
33+ var _DayCellJs2 = _interopRequireDefault ( _DayCellJs ) ;
34+
35+ var _stylesJs = require ( './styles.js' ) ;
36+
37+ var _stylesJs2 = _interopRequireDefault ( _stylesJs ) ;
38+
39+ function checkRange ( dayMoment , range ) {
40+ return dayMoment . isBetween ( range [ 'startDate' ] , range [ 'endDate' ] ) || dayMoment . isBetween ( range [ 'endDate' ] , range [ 'startDate' ] ) ;
41+ }
42+
43+ function checkEdges ( dayMoment , range ) {
44+ var endDate = range . endDate ;
45+ var startDate = range . startDate ;
46+
47+ return dayMoment . isSame ( endDate ) || dayMoment . isSame ( startDate ) ;
48+ }
49+
50+ var Calendar = ( function ( _Component ) {
51+ _inherits ( Calendar , _Component ) ;
52+
53+ function Calendar ( props , context ) {
54+ _classCallCheck ( this , Calendar ) ;
55+
56+ _get ( Object . getPrototypeOf ( Calendar . prototype ) , 'constructor' , this ) . call ( this , props , context ) ;
57+
58+ var format = props . format ;
59+ var range = props . range ;
60+ var theme = props . theme ;
61+ var offset = props . offset ;
62+
63+ var date = ( 0 , _utilsParseInputJs2 [ 'default' ] ) ( props . date , format ) ;
64+
65+ var state = {
66+ date : date ,
67+ shownDate : ( range && range [ 'endDate' ] || date ) . clone ( ) . add ( offset , 'months' )
68+ } ;
69+
70+ this . state = state ;
71+ this . styles = ( 0 , _stylesJs2 [ 'default' ] ) ( theme ) ;
72+ }
73+
74+ _createClass ( Calendar , [ {
75+ key : 'componentDidMount' ,
76+ value : function componentDidMount ( ) {
77+ var onInit = this . props . onInit ;
78+
79+ onInit && onInit ( this . state . date ) ;
80+ }
81+ } , {
82+ key : 'getShownDate' ,
83+ value : function getShownDate ( ) {
84+ var _props = this . props ;
85+ var link = _props . link ;
86+ var offset = _props . offset ;
87+
88+ var shownDate = link ? link . clone ( ) . add ( offset , 'months' ) : this . state . shownDate ;
89+
90+ return shownDate ;
91+ }
92+ } , {
93+ key : 'handleSelect' ,
94+ value : function handleSelect ( newDate ) {
95+ var _props2 = this . props ;
96+ var link = _props2 . link ;
97+ var onChange = _props2 . onChange ;
98+ var date = this . state . date ;
99+
100+ onChange && onChange ( newDate ) ;
101+
102+ if ( ! link ) {
103+ this . setState ( { date : newDate } ) ;
104+ }
105+ }
106+ } , {
107+ key : 'changeMonth' ,
108+ value : function changeMonth ( direction ) {
109+ var _props3 = this . props ;
110+ var link = _props3 . link ;
111+ var linkCB = _props3 . linkCB ;
112+
113+ if ( link && linkCB ) {
114+ return linkCB ( direction ) ;
115+ }
116+
117+ var current = this . state . shownDate . month ( ) ;
118+ var newMonth = this . state . shownDate . clone ( ) . add ( direction , 'months' ) ;
119+
120+ this . setState ( {
121+ shownDate : newMonth
122+ } ) ;
123+ }
124+ } , {
125+ key : 'renderMonthAndYear' ,
126+ value : function renderMonthAndYear ( ) {
127+ var shownDate = this . getShownDate ( ) ;
128+ var month = _moment2 [ 'default' ] . months ( shownDate . month ( ) ) ;
129+ var year = shownDate . year ( ) ;
130+ var styles = this . styles ;
131+
132+ return _react2 [ 'default' ] . createElement (
133+ 'div' ,
134+ { style : styles [ 'MonthAndYear' ] , className : 'rdr-MonthAndYear-innerWrapper' } ,
135+ _react2 [ 'default' ] . createElement (
136+ 'button' ,
137+ {
138+ style : _extends ( { } , styles [ 'MonthButton' ] , { float : 'left' } ) ,
139+ className : 'rdr-MonthAndYear-button prev' ,
140+ onClick : this . changeMonth . bind ( this , - 1 , false ) } ,
141+ _react2 [ 'default' ] . createElement ( 'i' , { style : _extends ( { } , styles [ 'MonthArrow' ] , styles [ 'MonthArrowPrev' ] ) } )
142+ ) ,
143+ _react2 [ 'default' ] . createElement (
144+ 'span' ,
145+ null ,
146+ _react2 [ 'default' ] . createElement (
147+ 'span' ,
148+ { className : 'rdr-MonthAndYear-month' } ,
149+ month
150+ ) ,
151+ _react2 [ 'default' ] . createElement (
152+ 'span' ,
153+ { className : 'rdr-MonthAndYear-divider' } ,
154+ ' - '
155+ ) ,
156+ _react2 [ 'default' ] . createElement (
157+ 'span' ,
158+ { className : 'rdr-MonthAndYear-year' } ,
159+ year
160+ )
161+ ) ,
162+ _react2 [ 'default' ] . createElement (
163+ 'button' ,
164+ {
165+ style : _extends ( { } , styles [ 'MonthButton' ] , { float : 'right' } ) ,
166+ className : 'rdr-MonthAndYear-button next' ,
167+ onClick : this . changeMonth . bind ( this , + 1 , false ) } ,
168+ _react2 [ 'default' ] . createElement ( 'i' , { style : _extends ( { } , styles [ 'MonthArrow' ] , styles [ 'MonthArrowNext' ] ) } )
169+ )
170+ ) ;
171+ }
172+ } , {
173+ key : 'renderWeekdays' ,
174+ value : function renderWeekdays ( ) {
175+ var dow = _moment2 [ 'default' ] . localeData ( ) . firstDayOfWeek ( ) ;
176+ var weekdays = [ ] ;
177+ var styles = this . styles ;
178+
179+ for ( var i = dow ; i < 7 + dow ; i ++ ) {
180+ var day = _moment2 [ 'default' ] . weekdaysMin ( i ) ;
181+
182+ weekdays . push ( _react2 [ 'default' ] . createElement (
183+ 'span' ,
184+ { style : styles [ 'Weekday' ] , className : 'rdr-WeekDay' , key : day } ,
185+ day
186+ ) ) ;
187+ }
188+
189+ return weekdays ;
190+ }
191+ } , {
192+ key : 'renderDays' ,
193+ value : function renderDays ( ) {
194+ var _this = this ;
195+
196+ // TODO: Split this logic into smaller chunks
197+ var styles = this . styles ;
198+ var range = this . props . range ;
199+
200+ var shownDate = this . getShownDate ( ) ;
201+ var date = this . state . date ;
202+
203+ var dateUnix = date . unix ( ) ;
204+
205+ var monthNumber = shownDate . month ( ) ;
206+ var dayCount = shownDate . daysInMonth ( ) ;
207+ var startOfMonth = shownDate . clone ( ) . startOf ( 'month' ) . weekday ( ) ;
208+
209+ var lastMonth = shownDate . clone ( ) . month ( monthNumber - 1 ) ;
210+ var lastMonthNumber = lastMonth . month ( ) ;
211+ var lastMonthDayCount = lastMonth . daysInMonth ( ) ;
212+
213+ var nextMonth = shownDate . clone ( ) . month ( monthNumber + 1 ) ;
214+ var nextMonthNumber = nextMonth . month ( ) ;
215+
216+ var days = [ ] ;
217+
218+ // Previous month's days
219+ for ( var i = startOfMonth ; i >= 1 ; i -- ) {
220+ var dayMoment = lastMonth . clone ( ) . date ( lastMonthDayCount + 1 - i ) ;
221+ days . push ( { dayMoment : dayMoment , isPassive : true } ) ;
222+ }
223+
224+ // Current month's days
225+ for ( var i = 1 ; i <= dayCount ; i ++ ) {
226+ var dayMoment = shownDate . clone ( ) . date ( i ) ;
227+ days . push ( { dayMoment : dayMoment } ) ;
228+ }
229+
230+ // Next month's days
231+ var remainingCells = 42 - days . length ; // 42cells = 7days * 6rows
232+ for ( var i = 1 ; i <= remainingCells ; i ++ ) {
233+ var dayMoment = nextMonth . clone ( ) . date ( i ) ;
234+ days . push ( { dayMoment : dayMoment , isPassive : true } ) ;
235+ }
236+
237+ return days . map ( function ( data , index ) {
238+ var dayMoment = data . dayMoment ;
239+ var isPassive = data . isPassive ;
240+
241+ var isSelected = ! range && dayMoment . unix ( ) === dateUnix ;
242+ var isInRange = range && checkRange ( dayMoment , range ) ;
243+ var isEdge = range && checkEdges ( dayMoment , range ) ;
244+
245+ return _react2 [ 'default' ] . createElement ( _DayCellJs2 [ 'default' ] , _extends ( {
246+ onSelect : _this . handleSelect . bind ( _this )
247+ } , data , {
248+ theme : styles ,
249+ isSelected : isSelected || isEdge ,
250+ isInRange : isInRange ,
251+ key : index
252+ } ) ) ;
253+ } ) ;
254+ }
255+ } , {
256+ key : 'render' ,
257+ value : function render ( ) {
258+ var styles = this . styles ;
259+
260+ return _react2 [ 'default' ] . createElement (
261+ 'div' ,
262+ { style : _extends ( { } , styles [ 'Calendar' ] , this . props . style ) , className : 'rdr-Calendar' } ,
263+ _react2 [ 'default' ] . createElement (
264+ 'div' ,
265+ { className : 'rdr-MonthAndYear' } ,
266+ this . renderMonthAndYear ( )
267+ ) ,
268+ _react2 [ 'default' ] . createElement (
269+ 'div' ,
270+ { className : 'rdr-WeekDays' } ,
271+ this . renderWeekdays ( )
272+ ) ,
273+ _react2 [ 'default' ] . createElement (
274+ 'div' ,
275+ { className : 'rdr-Days' } ,
276+ this . renderDays ( )
277+ )
278+ ) ;
279+ }
280+ } ] ) ;
281+
282+ return Calendar ;
283+ } ) ( _react . Component ) ;
284+
285+ Calendar . defaultProps = {
286+ format : 'DD/MM/YYYY' ,
287+ theme : { }
288+ } ;
289+
290+ Calendar . propTypes = {
291+ sets : _react . PropTypes . string ,
292+ range : _react . PropTypes . shape ( {
293+ startDate : _react . PropTypes . object ,
294+ endDate : _react . PropTypes . object
295+ } ) ,
296+ date : _react . PropTypes . oneOfType ( [ _react . PropTypes . object , _react . PropTypes . string , _react . PropTypes . func ] ) ,
297+ format : _react . PropTypes . string . isRequired ,
298+ onChange : _react . PropTypes . func ,
299+ onInit : _react . PropTypes . func ,
300+ link : _react . PropTypes . oneOfType ( [ _react . PropTypes . shape ( {
301+ startDate : _react . PropTypes . object ,
302+ endDate : _react . PropTypes . object
303+ } ) , _react . PropTypes . bool ] ) ,
304+ linkCB : _react . PropTypes . func ,
305+ theme : _react . PropTypes . object
306+ } ;
307+
308+ exports [ 'default' ] = Calendar ;
309+ module . exports = exports [ 'default' ] ;
0 commit comments