Skip to content

Commit 8f2734d

Browse files
committed
Add date picker initial version
1 parent 8a967f1 commit 8f2734d

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

components/calendar/index.cjsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = React.createClass
88
# -- States & Properties
99
propTypes:
1010
className: React.PropTypes.string
11+
onChange: React.PropTypes.func
1112
selectedDate: React.PropTypes.object
1213
viewDate: React.PropTypes.object
1314

@@ -20,6 +21,9 @@ module.exports = React.createClass
2021
selectedDate: @props.selectedDate
2122
viewDate: @props.viewDate
2223

24+
componentDidUpdate: (prevProps, prevState) ->
25+
@props.onChange? @ if prevState.selectedDate.getTime() != @state.selectedDate.getTime()
26+
2327
# -- Events
2428
onDayClick: (event) ->
2529
day = parseInt(event.target.textContent)
@@ -90,3 +94,6 @@ module.exports = React.createClass
9094
</div>
9195
</CTG>
9296
</div>
97+
98+
getValue: ->
99+
@state.selectedDate

components/date_picker/index.cjsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
css = require './style'
2+
Calendar = require '../calendar'
3+
dateUtils = require '../date_utils'
4+
5+
module.exports = React.createClass
6+
7+
# -- States & Properties
8+
propTypes:
9+
className: React.PropTypes.string
10+
initialDate: React.PropTypes.object
11+
12+
getDefaultProps: ->
13+
className: ''
14+
initialDate: new Date()
15+
16+
getInitialState: ->
17+
date: @props.initialDate
18+
19+
# -- Events
20+
onCalendarChange: (calendar) ->
21+
@setState
22+
date: dateUtils.cloneDatetime(calendar.getValue())
23+
24+
# -- Render
25+
render: ->
26+
<div className={css.root}>
27+
<header className={css.header}>
28+
<p className={css.headerWeekday}>{dateUtils.weekDayInWords(@state.date.getDay())}</p>
29+
<p className={css.headerMonth}>{dateUtils.monthInShortWords(@state.date)}</p>
30+
<p className={css.headerDay}>{@state.date.getDate()}</p>
31+
<p className={css.headerYear}>{@state.date.getFullYear()}</p>
32+
</header>
33+
34+
<Calendar onChange={@onCalendarChange} selectedDate={@state.date} />
35+
</div>

components/date_picker/style.styl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
@import '../constants'
2+
3+
dayWidth = 38px
4+
innerPadding = 10px
5+
calendarWidth = dayWidth * 7
6+
totalWidth = calendarWidth + innerPadding * 2
7+
8+
:local(.root)
9+
text-align : center
10+
width : totalWidth
11+
12+
:local(.header)
13+
background-color : ACCENT
14+
color : white
15+
16+
:local(.headerWeekday)
17+
background-color : darken(ACCENT, 20%)
18+
font-size : 14px
19+
line-height : 1.8
20+
width : 100%
21+
22+
:local(.headerMonth)
23+
font-size : 18px
24+
padding : 10px
25+
text-transform : uppercase
26+
27+
:local(.headerDay)
28+
font-size : 50px
29+
line-height : 40px
30+
31+
:local(.headerYear)
32+
font-size : 18px
33+
opacity : .7
34+
padding : 10px

components/date_utils.coffee

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module.exports =
22
daysInMonth: (date) ->
3-
(new Date(date.getFullYear(), date.getMonth() + 1, 0)).getDate()
3+
(new Date(date.getFullYear(), date.getMonth(), 0)).getDate()
44

55
firstWeekDay: (date) ->
6-
(new Date(date.getFullYear(), date.getMonth() + 1, 1)).getDay()
6+
(new Date(date.getFullYear(), date.getMonth(), 1)).getDay()
77

88
monthInWords: (date) ->
99
switch (date.getMonth())
@@ -20,6 +20,21 @@ module.exports =
2020
when 10 then 'November'
2121
when 11 then 'December'
2222

23+
monthInShortWords: (date) ->
24+
switch (date.getMonth())
25+
when 0 then 'Jan'
26+
when 1 then 'Feb'
27+
when 2 then 'Mar'
28+
when 3 then 'Apr'
29+
when 4 then 'May'
30+
when 5 then 'Jun'
31+
when 6 then 'Jul'
32+
when 7 then 'Aug'
33+
when 8 then 'Sep'
34+
when 9 then 'Oct'
35+
when 10 then 'Nov'
36+
when 11 then 'Dec'
37+
2338
weekDayInWords: (day) ->
2439
switch (day)
2540
when 0 then 'Sunday'
@@ -30,6 +45,16 @@ module.exports =
3045
when 5 then 'Friday'
3146
when 6 then 'Saturday'
3247

48+
weekDayInShortWords: (day) ->
49+
switch (day)
50+
when 0 then 'Sun'
51+
when 1 then 'Mon'
52+
when 2 then 'Tue'
53+
when 3 then 'Wed'
54+
when 4 then 'Thu'
55+
when 5 then 'Fri'
56+
when 6 then 'Sat'
57+
3358
addDays: (date, days) ->
3459
newDate = @cloneDatetime(date)
3560
newDate.setDate(date.getDate() + days)

0 commit comments

Comments
 (0)