|
1 | | -import React, { Component } from 'react'; |
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
2 | 3 | import { Segment } from 'semantic-ui-react'; |
3 | | -import cn from 'classnames'; |
4 | 4 | import Dayzed from 'dayzed'; |
5 | 5 | import Button from '../button'; |
6 | 6 | import CalendarCell from '../cell'; |
7 | 7 | import { monthNamesShort, weekdayNamesShort } from '../../data'; |
8 | 8 | import 'semantic-ui-css/semantic.min.css'; |
9 | 9 | import './calendar.css'; |
10 | 10 |
|
11 | | -class Calendar extends Component { |
12 | | - render() { |
13 | | - const { |
14 | | - fluid, |
15 | | - onDateSelected, |
16 | | - selected, |
17 | | - selectedClassName, |
18 | | - ...otherProps |
19 | | - } = this.props; |
| 11 | +const Calendar = ({ |
| 12 | + fluid, |
| 13 | + onDateSelected, |
| 14 | + selected, |
| 15 | + selectedClassName, |
| 16 | + ...otherProps |
| 17 | +}) => ( |
| 18 | + <Dayzed |
| 19 | + {...otherProps} |
| 20 | + onDateSelected={onDateSelected} |
| 21 | + selected={selected} |
| 22 | + render={({ calendars, getBackProps, getForwardProps, getDateProps }) => |
| 23 | + calendars.map(calendar => ( |
| 24 | + <Segment compact={!fluid} key={`${calendar.year}-${calendar.month}`}> |
| 25 | + <div className="clndr-control"> |
| 26 | + <Button |
| 27 | + icon="angle double left" |
| 28 | + title="Last year" |
| 29 | + {...getBackProps({ calendars, offset: 12 })} |
| 30 | + /> |
| 31 | + <Button |
| 32 | + icon="angle left" |
| 33 | + style={{ marginRight: 0 }} |
| 34 | + title="Last month" |
| 35 | + {...getBackProps({ calendars })} |
| 36 | + /> |
20 | 37 |
|
21 | | - return ( |
22 | | - <Dayzed |
23 | | - {...otherProps} |
24 | | - onDateSelected={onDateSelected} |
25 | | - selected={selected} |
26 | | - render={({ calendars, getBackProps, getForwardProps, getDateProps }) => |
27 | | - calendars.map(calendar => ( |
28 | | - <Segment |
29 | | - compact={!fluid} |
30 | | - key={`${calendar.year}-${calendar.month}`} |
31 | | - > |
32 | | - <div className="clndr-control"> |
33 | | - <Button |
34 | | - icon="angle double left" |
35 | | - iconPosition="left" |
36 | | - title="Last year" |
37 | | - {...getBackProps({ calendars, offset: 12 })} |
38 | | - /> |
39 | | - <Button |
40 | | - icon="angle left" |
41 | | - iconPosition="left" |
42 | | - style={{ marginRight: 0 }} |
43 | | - title="Last month" |
44 | | - {...getBackProps({ calendars })} |
45 | | - /> |
| 38 | + <span className="clndr-control-month"> |
| 39 | + {monthNamesShort[calendar.month]} {calendar.year} |
| 40 | + </span> |
46 | 41 |
|
47 | | - <span className="clndr-control-month"> |
48 | | - {monthNamesShort[calendar.month]} {calendar.year} |
49 | | - </span> |
| 42 | + <Button |
| 43 | + icon="angle right" |
| 44 | + title="Next month" |
| 45 | + {...getForwardProps({ calendars })} |
| 46 | + /> |
| 47 | + <Button |
| 48 | + icon="angle double right" |
| 49 | + style={{ marginRight: 0 }} |
| 50 | + title="Next year" |
| 51 | + {...getForwardProps({ calendars, offset: 12 })} |
| 52 | + /> |
| 53 | + </div> |
| 54 | + <div className="clndr-days"> |
| 55 | + {weekdayNamesShort.map(weekday => ( |
| 56 | + <CalendarCell |
| 57 | + key={`${calendar.year}-${calendar.month}-${weekday}`} |
| 58 | + > |
| 59 | + {weekday} |
| 60 | + </CalendarCell> |
| 61 | + ))} |
| 62 | + {calendar.weeks.map(week => |
| 63 | + week.map((dateObj, index) => { |
| 64 | + const key = `${calendar.year}-${calendar.month}-${index}`; |
50 | 65 |
|
51 | | - <Button |
52 | | - icon="angle right" |
53 | | - iconPosition="right" |
54 | | - title="Next month" |
55 | | - {...getForwardProps({ calendars })} |
56 | | - /> |
57 | | - <Button |
58 | | - icon="angle double right" |
59 | | - iconPosition="right" |
60 | | - style={{ marginRight: 0 }} |
61 | | - title="Next year" |
62 | | - {...getForwardProps({ calendars, offset: 12 })} |
63 | | - /> |
64 | | - </div> |
65 | | - <div className="clndr-days"> |
66 | | - {weekdayNamesShort.map(weekday => ( |
| 66 | + return dateObj ? ( |
67 | 67 | <CalendarCell |
68 | | - key={`${calendar.year}-${calendar.month}-${weekday}`} |
| 68 | + key={key} |
| 69 | + selectedClassName={selectedClassName} |
| 70 | + {...dateObj} |
| 71 | + {...getDateProps({ dateObj })} |
69 | 72 | > |
70 | | - {weekday} |
| 73 | + {dateObj.date.getDate()} |
71 | 74 | </CalendarCell> |
72 | | - ))} |
73 | | - {calendar.weeks.map(week => |
74 | | - week.map((dateObj, index) => { |
75 | | - const key = `${calendar.year}-${calendar.month}-${index}`; |
| 75 | + ) : ( |
| 76 | + <CalendarCell key={key} /> |
| 77 | + ); |
| 78 | + }) |
| 79 | + )} |
| 80 | + </div> |
| 81 | + </Segment> |
| 82 | + )) |
| 83 | + } |
| 84 | + /> |
| 85 | +); |
76 | 86 |
|
77 | | - return !dateObj ? ( |
78 | | - <CalendarCell key={key} /> |
79 | | - ) : ( |
80 | | - <CalendarCell |
81 | | - key={key} |
82 | | - selectedClassName={selectedClassName} |
83 | | - {...dateObj} |
84 | | - {...getDateProps({ dateObj })} |
85 | | - > |
86 | | - {dateObj.date.getDate()} |
87 | | - </CalendarCell> |
88 | | - ); |
89 | | - }) |
90 | | - )} |
91 | | - </div> |
92 | | - </Segment> |
93 | | - )) |
94 | | - } |
95 | | - /> |
96 | | - ); |
97 | | - } |
98 | | -} |
| 87 | +Calendar.propTypes = { |
| 88 | + fluid: PropTypes.bool, |
| 89 | + onDateSelected: PropTypes.func, |
| 90 | + selected: PropTypes.instanceOf(Date), |
| 91 | + selectedClassName: PropTypes.string, |
| 92 | +}; |
| 93 | + |
| 94 | +Calendar.defaultProps = { |
| 95 | + onDateSelected: () => {}, |
| 96 | +}; |
99 | 97 |
|
100 | 98 | export default Calendar; |
0 commit comments