Skip to content

Commit 3c73111

Browse files
author
Jon Brito
committed
Added support for modifiers with date ranges.
Added ability to have WeekDay header in <Week />. Clean up.
1 parent c7670bf commit 3c73111

File tree

7 files changed

+74
-13
lines changed

7 files changed

+74
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ One year calendar ([Demo](http://freiksenet.github.io/react-calendar/)):
3636
/>
3737
```
3838

39-
Each component can be used separately AND passed to other components to modify
40-
rendering.
39+
Each component can be used separately ~~AND passed to other components to modify
40+
rendering.~~ We have deprecated this in favor of passing a more flexible modifier object.
4141

4242
```html
4343
<Month date={moment()} />

demo.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ const PagingCalendar = React.createClass({
5050
component: [ 'day', 'month', 'week' ]
5151
},
5252
{
53-
date: moment().add(3, 'days'),
54-
classNames: [ 'event' ],
53+
startDate: moment().add(3, 'days'),
54+
endDate: moment().add(7, 'days'),
55+
classNames: [ 'longEvent' ],
5556
component: [ 'day' ]
5657
},
5758
{
@@ -79,7 +80,7 @@ const PagingCalendar = React.createClass({
7980
}
8081
}
8182
]
82-
} />
83+
} />
8384
</div>
8485
);
8586
}

less/bootstrap-theme.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
&--current {
8080
background-color: @brand-primary;
8181
}
82+
&--longEvent {
83+
background-color: @brand-success;
84+
}
8285
&--warning {
8386
background-color: @brand-warning;
8487
}

src/Calendar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default class Calendar extends Component {
4141
}
4242

4343
getMonthRange () {
44-
const focus = this.moment(this.props.date).startOf('month');
44+
const focus = this.moment(this.props.date || this.props.startDate).startOf('month');
4545
const start = this.moment(this.props.startDate);
4646
const end = this.moment(this.props.endDate);
4747
const size = end.diff(start, 'month') + 1;

src/Month.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ const renderWeekHeader = (props) => {
2323
};
2424

2525
const renderHeader = (props) => {
26+
if (props.renderHeader) {
27+
return props.renderHeader(props);
28+
}
29+
2630
if (!props.monthNames) {
2731
return null;
2832
}
@@ -32,7 +36,7 @@ const renderHeader = (props) => {
3236
{ props.date.format(props.monthNameFormat) }
3337
</header>
3438
);
35-
}
39+
};
3640

3741
const Month = (props) => {
3842
const { date, weekNumbers } = props;

src/Week.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,25 @@ const makeWeekNumber = (props) => {
1717
{ props.date.format(props.weekNumberFormat) }
1818
</div>
1919
);
20-
}
20+
};
21+
22+
const renderWeekHeader = (props) => {
23+
if (!props.weekHeader) {
24+
return null;
25+
}
26+
27+
return (
28+
<div className={`${clsPrefix}-weekdays`}>
29+
{
30+
daysOfWeek(props.date).map((weekday, i) =>
31+
<div key={ `weekday-header-${i}` } className={ classnames(`${clsPrefix}-weekdays-weekday`) }>
32+
{ weekday.format(props.weekdayFormat) }
33+
</div>
34+
)
35+
}
36+
</div>
37+
);
38+
};
2139

2240
const Week = (props) => {
2341
const { mods, date } = props;
@@ -37,6 +55,7 @@ const Week = (props) => {
3755

3856
return (
3957
<div key="days" className={ classnames(clsPrefix, clsMods) } { ...events }>
58+
{ renderWeekHeader(props) }
4059
{ makeWeekNumber(props) }
4160
<div className={ classnames(`${clsPrefix}-days`) }>
4261
{
@@ -56,13 +75,17 @@ const Week = (props) => {
5675
};
5776

5877
Week.propTypes = {
78+
weekHeader: PropTypes.bool,
5979
weekNumbers: PropTypes.bool,
60-
weekNumberFormat: PropTypes.string
80+
weekNumberFormat: PropTypes.string,
81+
weekdayFormat: PropTypes.string,
6182
};
6283

6384
Week.defaultProps = {
85+
weekHeader: false,
6486
weekNumbers: false,
65-
weekNumberFormat: 'w'
87+
weekNumberFormat: 'w',
88+
weekdayFormat: 'dd',
6689
};
6790

6891
export default Week;

src/util.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const bindEvents = (events, date) => {
22
const boundEvents = {};
33

4+
if (!events) return null;
5+
46
Object.keys(events)
57
.forEach((key) => boundEvents[key] = events[key].bind(null, date));
68

@@ -10,6 +12,9 @@ const bindEvents = (events, date) => {
1012
const getClsMods = (clsPrefix, mods) =>
1113
!mods || !mods.classNames ? null : mods.classNames.map((cls) => `${clsPrefix}--${cls}`);
1214

15+
/**
16+
* Internal: Creates a single modifier object for a date
17+
*/
1318
const getModByDate = (mods, date, type) => {
1419
const modifier = {
1520
date: null,
@@ -20,13 +25,38 @@ const getModByDate = (mods, date, type) => {
2025
mods.filter((mod) => mod.date ? mod.date.isSame(date, type) : null)
2126
.forEach((_mod) => {
2227
modifier.date = _mod.date;
23-
modifier.events = Object.assign(modifier.events, _mod.events);
28+
modifier.events = _mod.events;
2429
modifier.classNames.push(..._mod.classNames);
2530
});
2631

2732
return modifier;
2833
}
2934

35+
const getModsWithDateRange = (mods) =>
36+
mods.filter((mod) => !!mod.startDate)
37+
38+
const explodeDateRanges = (mods) => {
39+
return mods.map((mod) => {
40+
const diff = mod.endDate.diff(mod.startDate, 'days');
41+
42+
if (!diff) { // if the diff is 0 just return the mod
43+
mod.date = mod.startDate.clone();
44+
return mod;
45+
}
46+
47+
return Array(diff).fill(mod).map((mod, i) => {
48+
return {
49+
...mod,
50+
date: mod.startDate.clone().add(i, 'days')
51+
}
52+
});
53+
})
54+
.reduce((a, b) => a.concat(b), []);
55+
}
56+
57+
const getModsWithSingleDate = (mods) =>
58+
mods.filter((mod) => !mod.startDate && mod.date)
59+
3060
const getModsWithoutDate = (mods) =>
3161
mods.filter((mod) => !mod.date)
3262

@@ -39,8 +69,8 @@ export const getMods = (mods, date, clsPrefix, type) => {
3969
}
4070

4171
const events = {};
42-
43-
const mod = getModByDate(mods, date, type);
72+
const exploded = explodeDateRanges(getModsWithDateRange(mods));
73+
const mod = getModByDate([ ...mods, ...exploded ], date, type);
4474
const clsMods = getClsMods(clsPrefix, mod) || [];
4575
const clsCompMods = getClsMods(clsPrefix, getModsWithoutDate(mods)) || [];
4676

0 commit comments

Comments
 (0)