Skip to content

Commit f1bcfcf

Browse files
committed
feat(calendar): Adding a Today button and a showToday prop that defaults to true
1 parent 8ae311d commit f1bcfcf

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

src/components/calendar/calendar.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Segment } from 'semantic-ui-react';
44
import Dayzed from 'dayzed';
55
import Button from '../button';
66
import CalendarCell from '../cell';
7+
import { getToday } from '../../utils';
78
import { monthNamesShort, weekdayNamesShort } from '../../data';
89
import 'semantic-ui-css/semantic.min.css';
910
import './calendar.css';
@@ -13,6 +14,7 @@ const Calendar = ({
1314
onDateSelected,
1415
selected,
1516
selectedClassName,
17+
showToday,
1618
...otherProps
1719
}) => (
1820
<Dayzed
@@ -77,6 +79,15 @@ const Calendar = ({
7779
);
7880
})
7981
)}
82+
{showToday && (
83+
<CalendarCell
84+
fluid
85+
{...getToday(selected)}
86+
{...getDateProps({ dateObj: getToday(selected) })}
87+
>
88+
Today
89+
</CalendarCell>
90+
)}
8091
</div>
8192
</Segment>
8293
))
@@ -89,10 +100,12 @@ Calendar.propTypes = {
89100
onDateSelected: PropTypes.func,
90101
selected: PropTypes.instanceOf(Date),
91102
selectedClassName: PropTypes.string,
103+
showToday: PropTypes.bool,
92104
};
93105

94106
Calendar.defaultProps = {
95107
onDateSelected: () => {},
108+
showToday: true,
96109
};
97110

98111
export default Calendar;

src/components/cell/cell.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@
1818
background: #4f4f4f;
1919
color: #f2f2f2;
2020
}
21+
22+
.clndr-cell-full {
23+
grid-column: 1 / -1;
24+
}

src/components/cell/cell.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import cn from 'classnames';
44
import './cell.css';
55

66
const CalendarCell = ({
7+
fluid,
78
selectable,
89
selected,
910
selectedClassName,
@@ -12,6 +13,7 @@ const CalendarCell = ({
1213
}) => (
1314
<span
1415
className={cn('clndr-cell', {
16+
'clndr-cell-full': fluid,
1517
'clndr-cell-today': today,
1618
'clndr-cell-disabled': !selectable,
1719
[selectedClassName]: selected,
@@ -21,6 +23,7 @@ const CalendarCell = ({
2123
);
2224

2325
CalendarCell.propTypes = {
26+
fluid: PropTypes.bool,
2427
selected: PropTypes.bool,
2528
selectable: PropTypes.bool,
2629
selectedClassName: PropTypes.string,

src/utils/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export const normalizeDate = date => {
2+
const newDate = new Date(date.getTime());
3+
newDate.setHours(0, 0, 0, 0);
4+
5+
return newDate;
6+
};
7+
8+
const isEqual = (date, dateToCompare) => {
9+
if (!date || !dateToCompare) {
10+
return false;
11+
}
12+
13+
return (
14+
normalizeDate(date).getTime() === normalizeDate(dateToCompare).getTime()
15+
);
16+
};
17+
18+
export const getToday = selected => {
19+
return {
20+
date: normalizeDate(new Date()),
21+
selectable: true,
22+
selected: isEqual(new Date(), selected),
23+
today: true,
24+
};
25+
};

0 commit comments

Comments
 (0)