Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom view to days with events #124

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 42 additions & 17 deletions components/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class Calendar extends Component {

static propTypes = {
currentMonth: PropTypes.any,
customEventIndicatorView: PropTypes.func,
customStyle: PropTypes.object,
dayHeadings: PropTypes.array,
eventDates: PropTypes.array,
Expand Down Expand Up @@ -113,7 +114,10 @@ export default class Calendar extends Component {
const date = moment(event);
const month = moment(date).startOf('month').format();
parsedDates[month] = parsedDates[month] || {};
parsedDates[month][date.date() - 1] = {};
if(!parsedDates[month][date.date() - 1]) {
parsedDates[month][date.date() - 1] = { events: [] };
}
parsedDates[month][date.date() - 1].events.push(event);
});

// Dates with custom properties
Expand All @@ -123,7 +127,10 @@ export default class Calendar extends Component {
const date = moment(event.date);
const month = moment(date).startOf('month').format();
parsedDates[month] = parsedDates[month] || {};
parsedDates[month][date.date() - 1] = event;
if(!parsedDates[month][date.date() - 1]) {
parsedDates[month][date.date() - 1] = { events: [] };
}
parsedDates[month][date.date() - 1].events.push(event);
}
});
}
Expand Down Expand Up @@ -204,23 +211,31 @@ export default class Calendar extends Component {

if (dayIndex >= 0 && dayIndex < argMonthDaysCount) {
days.push((
<Day
startOfMonth={startOfArgMonthMoment}
isWeekend={isoWeekday === 0 || isoWeekday === 6}
key={`${renderIndex}`}
onPress={() => {
this.selectDate(moment(startOfArgMonthMoment).set('date', dayIndex + 1));
}}
caption={`${dayIndex + 1}`}
isToday={argMonthIsToday && (dayIndex === todayIndex)}
isSelected={selectedMonthIsArg && (dayIndex === selectedIndex)}
event={events && events[dayIndex]}
showEventIndicators={this.props.showEventIndicators}
customStyle={this.props.customStyle}
/>
<View key={`${renderIndex}`}>
<Day
startOfMonth={startOfArgMonthMoment}
isWeekend={isoWeekday === 0 || isoWeekday === 6}
key={`${renderIndex}`}
onPress={() => {
this.selectDate(moment(startOfArgMonthMoment).set('date', dayIndex + 1));
}}
caption={`${dayIndex + 1}`}
isToday={argMonthIsToday && (dayIndex === todayIndex)}
isSelected={selectedMonthIsArg && (dayIndex === selectedIndex)}
event={events && events[dayIndex]}
showEventIndicators={this.props.showEventIndicators}
customStyle={this.props.customStyle}
/>
{events && events[dayIndex] ? this.renderCustomEventIndicatorView(events[dayIndex].events) : null}
</View>
));
} else {
days.push(<Day key={`${renderIndex}`} filler customStyle={this.props.customStyle} />);
days.push(
<View key={`${renderIndex}`}>
<Day key={`${renderIndex}`} filler customStyle={this.props.customStyle} />
{events && events[dayIndex] ? this.renderCustomEventIndicatorView(events[dayIndex].events) : null}
</View>
);
}
if (renderIndex % 7 === 6) {
weekRows.push(
Expand All @@ -242,6 +257,16 @@ export default class Calendar extends Component {
return <View key={argMoment.month()} style={containerStyle}>{weekRows}</View>;
}

renderCustomEventIndicatorView(events) {
const { customEventIndicatorView } = this.props;
if (!events || typeof customEventIndicatorView !== 'function') {
return null;
}
else {
return customEventIndicatorView(events);
}
}

renderHeading() {
const headings = [];
for (let i = 0; i < 7; i++) {
Expand Down