@@ -7,42 +7,35 @@ var React = require('react')
7
7
, Century = require ( './century' )
8
8
, cx = require ( '../util/cx' )
9
9
, setter = require ( '../util/stateSetter' )
10
+ , controlledInput = require ( '../util/controlledInput' )
10
11
, SlideTransition = require ( '../common/slide-transition' )
11
12
, dates = require ( '../util/dates' )
12
13
, mergeIntoProps = require ( '../util/transferProps' ) . mergeIntoProps
14
+ , constants = require ( '../util/constants' )
13
15
, _ = require ( 'lodash' ) ;
14
16
15
- var RIGHT = 'right'
16
- , LEFT = 'left'
17
- , UP = 'up'
18
- , DOWN = 'down'
17
+ var dir = constants . directions ;
19
18
20
- , MULTIPLIER = {
21
- 'year' : 1 ,
22
- 'decade' : 10 ,
23
- 'century' : 100
24
- } ,
25
- VIEW = {
26
- 'month' : Month ,
27
- 'year' : Year ,
28
- 'decade' : Decade ,
29
- 'century' : Century ,
30
- }
31
- NEXT_VIEW = {
32
- 'month' : 'year' ,
33
- 'year' : 'decade' ,
34
- 'decade' : 'century'
35
- }
36
- VIEW_UNIT = {
37
- 'month' : 'day' ,
38
- 'year' : 'month' ,
39
- 'decade' : 'year' ,
40
- 'century' : 'decade' ,
41
- } ;
19
+ var views = constants . calendarViews
20
+ , VIEW_OPTIONS = _ . values ( views )
21
+ , ALT_VIEW = _ . invert ( constants . calendarViewHierarchy )
22
+ , NEXT_VIEW = constants . calendarViewHierarchy
23
+ , VIEW_UNIT = constants . calendarViewUnits
24
+ , VIEW = _ . object ( [
25
+ [ views . MONTH , Month ] ,
26
+ [ views . YEAR , Year ] ,
27
+ [ views . DECADE , Decade ] ,
28
+ [ views . CENTURY , Century ]
29
+ ] ) ;
42
30
43
- var VIEW_OPTIONS = [ 'month' , 'year' , 'decade' , 'century' ] ;
31
+ var MULTIPLIER = _ . object ( [
32
+ [ views . YEAR , 1 ] ,
33
+ [ views . DECADE , 10 ] ,
34
+ [ views . CENTURY , 100 ]
35
+ ] ) ;
44
36
45
- module . exports = React . createClass ( {
37
+
38
+ var Calendar = React . createClass ( {
46
39
47
40
displayName : 'Calendar' ,
48
41
@@ -86,19 +79,17 @@ module.exports = React.createClass({
86
79
getInitialState : function ( ) {
87
80
return {
88
81
selectedIndex : 0 ,
89
- open : false ,
90
82
view : this . props . initialView || 'month' ,
91
-
92
- //determines the position of views
93
83
currentDate : this . inRangeValue ( new Date ( this . props . value ) )
94
84
}
95
85
} ,
96
86
97
87
getDefaultProps : function ( ) {
98
88
return {
89
+ open : false ,
99
90
value : new Date ,
100
- min : new Date ( 1900 , 0 , 1 ) ,
101
- max : new Date ( 2099 , 11 , 31 ) ,
91
+ min : new Date ( 1900 , 0 , 1 ) ,
92
+ max : new Date ( 2099 , 11 , 31 ) ,
102
93
103
94
initialView : 'month' ,
104
95
finalView : 'century' ,
@@ -134,7 +125,6 @@ module.exports = React.createClass({
134
125
, key = this . state . view + '_' + dates [ this . state . view ] ( date )
135
126
, id = this . _id ( '_view' ) ;
136
127
137
- //console.log(key)
138
128
return mergeIntoProps ( _ . omit ( this . props , 'value' , 'min' , 'max' ) ,
139
129
React . DOM . div ( { className : cx ( {
140
130
'rw-calendar' : true ,
@@ -148,11 +138,11 @@ module.exports = React.createClass({
148
138
labelId : labelId ,
149
139
messages : this . props . messages ,
150
140
upDisabled : disabled || this . state . view === this . props . finalView ,
151
- prevDisabled : disabled || ! dates . inRange ( this . nextDate ( LEFT ) , this . props . min , this . props . max ) ,
152
- nextDisabled : disabled || ! dates . inRange ( this . nextDate ( RIGHT ) , this . props . min , this . props . max ) ,
153
- onViewChange : this . _maybeHandle ( _ . partial ( this . navigate , UP , null ) ) ,
154
- onMoveLeft : this . _maybeHandle ( _ . partial ( this . navigate , LEFT , null ) ) ,
155
- onMoveRight : this . _maybeHandle ( _ . partial ( this . navigate , RIGHT , null ) ) } ) ,
141
+ prevDisabled : disabled || ! dates . inRange ( this . nextDate ( dir . LEFT ) , this . props . min , this . props . max ) ,
142
+ nextDisabled : disabled || ! dates . inRange ( this . nextDate ( dir . RIGHT ) , this . props . min , this . props . max ) ,
143
+ onViewChange : this . _maybeHandle ( _ . partial ( this . navigate , dir . UP , null ) ) ,
144
+ onMoveLeft : this . _maybeHandle ( _ . partial ( this . navigate , dir . LEFT , null ) ) ,
145
+ onMoveRight : this . _maybeHandle ( _ . partial ( this . navigate , dir . RIGHT , null ) ) } ) ,
156
146
157
147
SlideTransition ( {
158
148
ref : "animation" ,
@@ -168,8 +158,8 @@ module.exports = React.createClass({
168
158
onChange : this . _maybeHandle ( this . change ) ,
169
159
onKeyDown : this . _maybeHandle ( this . _keyDown ) ,
170
160
onFocus : this . _maybeHandle ( _ . partial ( this . _focus , true ) , true ) ,
171
- onMoveLeft : this . _maybeHandle ( _ . partial ( this . navigate , LEFT ) ) ,
172
- onMoveRight : this . _maybeHandle ( _ . partial ( this . navigate , RIGHT ) ) ,
161
+ onMoveLeft : this . _maybeHandle ( _ . partial ( this . navigate , dir . LEFT ) ) ,
162
+ onMoveRight : this . _maybeHandle ( _ . partial ( this . navigate , dir . RIGHT ) ) ,
173
163
disabled : this . props . disabled ,
174
164
readOnly : this . props . readOnly ,
175
165
min : this . props . min ,
@@ -184,21 +174,20 @@ module.exports = React.createClass({
184
174
} ,
185
175
186
176
navigate : function ( direction , date ) {
187
- var alts = _ . invert ( NEXT_VIEW )
188
- , view = this . state . view
189
- , slideDir = ( direction === LEFT || direction === UP )
177
+ var view = this . state . view
178
+ , slideDir = ( direction === dir . LEFT || direction === dir . UP )
190
179
? 'right'
191
180
: 'left' ;
192
181
193
182
if ( ! date )
194
- date = _ . contains ( [ LEFT , RIGHT ] , direction )
183
+ date = _ . contains ( [ dir . LEFT , dir . RIGHT ] , direction )
195
184
? this . nextDate ( direction )
196
185
: this . state . currentDate
197
186
198
- if ( direction === DOWN )
199
- view = alts [ view ] || view
187
+ if ( direction === dir . DOWN )
188
+ view = ALT_VIEW [ view ] || view
200
189
201
- if ( direction === UP )
190
+ if ( direction === dir . UP )
202
191
view = NEXT_VIEW [ view ] || view
203
192
204
193
if ( this . isValidView ( view ) && dates . inRange ( date , this . props . min , this . props . max , view ) ) {
@@ -221,15 +210,15 @@ module.exports = React.createClass({
221
210
222
211
change : function ( date ) {
223
212
if ( this . props . onChange && this . state . view === this . props . initialView )
224
- return this . props . onChange ( date )
213
+ return this . notify ( 'onChange' , date )
225
214
226
- this . navigate ( DOWN , date )
215
+ this . navigate ( dir . DOWN , date )
227
216
} ,
228
217
229
218
nextDate : function ( direction ) {
230
- var method = direction === LEFT ? 'subtract' : 'add'
219
+ var method = direction === dir . LEFT ? 'subtract' : 'add'
231
220
, view = this . state . view
232
- , unit = view === 'month' ? view : 'year'
221
+ , unit = view === views . MONTH ? view : views . YEAR
233
222
, multi = MULTIPLIER [ view ] || 1 ;
234
223
235
224
return dates [ method ] ( this . state . currentDate , 1 * multi , unit )
@@ -242,19 +231,19 @@ module.exports = React.createClass({
242
231
if ( ctrl ) {
243
232
if ( key === 'ArrowDown' ) {
244
233
e . preventDefault ( )
245
- this . navigate ( DOWN )
234
+ this . navigate ( dir . DOWN )
246
235
}
247
236
if ( key === 'ArrowUp' ) {
248
237
e . preventDefault ( )
249
- this . navigate ( UP )
238
+ this . navigate ( dir . UP )
250
239
}
251
240
if ( key === 'ArrowLeft' ) {
252
241
e . preventDefault ( )
253
- this . navigate ( LEFT )
242
+ this . navigate ( dir . LEFT )
254
243
}
255
244
if ( key === 'ArrowRight' ) {
256
245
e . preventDefault ( )
257
- this . navigate ( RIGHT )
246
+ this . navigate ( dir . RIGHT )
258
247
}
259
248
} else {
260
249
this . refs . currentView . _keyDown
@@ -299,3 +288,6 @@ module.exports = React.createClass({
299
288
return current >= bottom && current <= top
300
289
}
301
290
} ) ;
291
+
292
+ module . exports = controlledInput . createControlledClass (
293
+ 'Calendar' , Calendar , { value : 'onChange' } ) ;
0 commit comments