Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ format24h | PropTypes.boolean | Use format 24hour or 12hour
formatHeader | PropTypes.string | Header date format
upperCaseHeader | PropTypes.boolean | Sets date header as uppercase (default false)
headerStyle | PropTypes.object | Header style
hiddenHeader| PropTypes.boolean | Hidden Header (default false)
renderEvent | PropTypes.function | Function return a component to render event `renderEvent={(event) => <Text>{event.title}</Text>}`
eventTapped | PropTypes.function | Function on event press
dateChanged | PropTypes.function | Function on date change. Passes new date as a string formatted as 'YYYY-MM-DD'
Expand Down
47 changes: 25 additions & 22 deletions src/DayView.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @flow
import { View, Text, ScrollView, TouchableOpacity } from 'react-native';
import populateEvents from './Packer';
import React from 'react';
import moment from 'moment';
import _ from 'lodash';
import { View, Text, ScrollView, TouchableOpacity } from "react-native";
import populateEvents from "./Packer";
import React from "react";
import moment from "moment";
import _ from "lodash";

const LEFT_MARGIN = 60 - 1;
// const RIGHT_MARGIN = 10
Expand All @@ -24,7 +24,7 @@ export default class DayView extends React.PureComponent {
const width = props.width - LEFT_MARGIN;
const packedEvents = populateEvents(props.events, width, props.start);
let initPosition =
_.min(_.map(packedEvents, 'top')) -
_.min(_.map(packedEvents, "top")) -
this.calendarHeight / (props.end - props.start);
initPosition = initPosition < 0 ? 0 : initPosition;
this.state = {
Expand All @@ -33,7 +33,7 @@ export default class DayView extends React.PureComponent {
};
}

componentWillReceiveProps(nextProps) {
UNSAFE_componentWillReceiveProps(nextProps) {
const width = nextProps.width - LEFT_MARGIN;
this.setState({
packedEvents: populateEvents(nextProps.events, width, nextProps.start),
Expand Down Expand Up @@ -86,14 +86,18 @@ export default class DayView extends React.PureComponent {
let timeText;
if (i === start) {
timeText = ``;
} else if (i < 12) {
timeText = !format24h ? `${i} AM` : i;
} else if (i < 10) {
timeText = !format24h ? `0${i}:00 AM` : `0${i}:00`;
} else if (i == 10 || i == 11) {
timeText = !format24h ? `${i}:00 AM` : `${i}:00`;
} else if (i === 12) {
timeText = !format24h ? `${i} PM` : i;
timeText = !format24h ? `${i}:00 PM` : `${i}:00`;
} else if (i === 24) {
timeText = !format24h ? `12 AM` : 0;
timeText = !format24h ? `12:00 AM` : `00:00`;
} else if (i < 22) {
timeText = !format24h ? `0${i - 12}:00 PM` : `${i}:00`;
} else {
timeText = !format24h ? `${i - 12} PM` : i;
timeText = !format24h ? `${i - 12}:00 PM` : `${i}:00`;
}
const { width, styles } = this.props;
return [
Expand Down Expand Up @@ -152,37 +156,36 @@ export default class DayView extends React.PureComponent {
// Fixing the number of lines for the event title makes this calculation easier.
// However it would make sense to overflow the title to a new line if needed
const numberOfLines = Math.floor(event.height / TEXT_LINE_HEIGHT);
const formatTime = this.props.format24h ? 'HH:mm' : 'hh:mm A';
const formatTime = this.props.format24h ? "HH:mm" : "hh:mm A";
return (
<TouchableOpacity
activeOpacity={0.5}
onPress={() =>
this._onEventTapped(this.props.events[event.index])
}
key={i} style={[styles.event, style, event.color && eventColor]}
onPress={() => this._onEventTapped(this.props.events[event.index])}
key={i}
style={[styles.event, style, event.color && eventColor]}
>
{this.props.renderEvent ? (
this.props.renderEvent(event)
) : (
<View>
<Text numberOfLines={1} style={styles.eventTitle}>
{event.title || 'Event'}
{event.title || "Event"}
</Text>
{numberOfLines > 1 ? (
<Text
numberOfLines={numberOfLines - 1}
style={[styles.eventSummary]}
>
{event.summary || ' '}
{event.summary || " "}
</Text>
) : null}
{numberOfLines > 2 ? (
<Text style={styles.eventTimes} numberOfLines={1}>
{moment(event.start).format(formatTime)} -{' '}
{moment(event.start).format(formatTime)} -{" "}
{moment(event.end).format(formatTime)}
</Text>
) : null}
</View>
</View>
)}
</TouchableOpacity>
);
Expand All @@ -199,7 +202,7 @@ export default class DayView extends React.PureComponent {
const { styles } = this.props;
return (
<ScrollView
ref={ref => (this._scrollView = ref)}
ref={(ref) => (this._scrollView = ref)}
contentContainerStyle={[
styles.contentStyle,
{ width: this.props.width },
Expand Down
92 changes: 45 additions & 47 deletions src/EventCalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {
TouchableOpacity,
Image,
Text,
} from 'react-native';
import _ from 'lodash';
import moment from 'moment';
import React from 'react';
} from "react-native";
import _ from "lodash";
import moment from "moment";
import React from "react";

import styleConstructor from './style';
import styleConstructor from "./style";

import DayView from './DayView';
import DayView from "./DayView";

export default class EventCalendar extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -43,7 +43,7 @@ export default class EventCalendar extends React.Component {
static defaultProps = {
size: 30,
initDate: new Date(),
formatHeader: 'DD MMMM YYYY',
formatHeader: "DD MMMM YYYY",
};

_getItemLayout(data, index) {
Expand All @@ -54,13 +54,13 @@ export default class EventCalendar extends React.Component {
_getItem(events, index) {
const date = moment(this.props.initDate).add(
index - this.props.size,
'days'
"days"
);
return _.filter(events, event => {
return _.filter(events, (event) => {
const eventStartTime = moment(event.start);
return (
eventStartTime >= date.clone().startOf('day') &&
eventStartTime <= date.clone().endOf('day')
eventStartTime >= date.clone().startOf("day") &&
eventStartTime <= date.clone().endOf("day")
);
});
}
Expand All @@ -75,43 +75,46 @@ export default class EventCalendar extends React.Component {
end = 24,
formatHeader,
upperCaseHeader = false,
hiddenHeader = false,
} = this.props;
const date = moment(initDate).add(index - this.props.size, 'days');
const date = moment(initDate).add(index - this.props.size, "days");

const leftIcon = this.props.headerIconLeft ? (
this.props.headerIconLeft
this.props.headerIconLeft
) : (
<Image source={require('./back.png')} style={this.styles.arrow} />
<Image source={require("./back.png")} style={this.styles.arrow} />
);
const rightIcon = this.props.headerIconRight ? (
this.props.headerIconRight
this.props.headerIconRight
) : (
<Image source={require('./forward.png')} style={this.styles.arrow} />
<Image source={require("./forward.png")} style={this.styles.arrow} />
);

let headerText = upperCaseHeader
? date.format(formatHeader || 'DD MMMM YYYY').toUpperCase()
: date.format(formatHeader || 'DD MMMM YYYY');
? date.format(formatHeader || "DD MMMM YYYY").toUpperCase()
: date.format(formatHeader || "DD MMMM YYYY");

return (
<View style={[this.styles.container, { width }]}>
<View style={this.styles.header}>
<TouchableOpacity
{!this.props.hiddenHeader && (
<View style={this.styles.header}>
<TouchableOpacity
style={this.styles.arrowButton}
onPress={this._previous}
>
{leftIcon}
</TouchableOpacity>
<View style={this.styles.headerTextContainer}>
<Text style={this.styles.headerText}>{headerText}</Text>
</View>
<TouchableOpacity
>
{leftIcon}
</TouchableOpacity>
<View style={this.styles.headerTextContainer}>
<Text style={this.styles.headerText}>{headerText}</Text>
</View>
<TouchableOpacity
style={this.styles.arrowButton}
onPress={this._next}
>
{rightIcon}
</TouchableOpacity>
</View>
>
{rightIcon}
</TouchableOpacity>
</View>
)}
<DayView
date={date}
index={index}
Expand All @@ -137,7 +140,7 @@ export default class EventCalendar extends React.Component {
}
const date = moment(this.props.initDate).add(
index - this.props.size,
'days'
"days"
);
this.refs.calendar.scrollToIndex({ index, animated: false });
this.setState({ index, date });
Expand All @@ -146,9 +149,9 @@ export default class EventCalendar extends React.Component {
_goToDate(date) {
const earliestDate = moment(this.props.initDate).subtract(
this.props.size,
'days'
"days"
);
const index = moment(date).diff(earliestDate, 'days');
const index = moment(date).diff(earliestDate, "days");
this._goToPage(index);
}

Expand All @@ -157,8 +160,8 @@ export default class EventCalendar extends React.Component {
if (this.props.dateChanged) {
this.props.dateChanged(
moment(this.props.initDate)
.add(this.state.index - 1 - this.props.size, 'days')
.format('YYYY-MM-DD')
.add(this.state.index - 1 - this.props.size, "days")
.format("YYYY-MM-DD")
);
}
};
Expand All @@ -168,19 +171,14 @@ export default class EventCalendar extends React.Component {
if (this.props.dateChanged) {
this.props.dateChanged(
moment(this.props.initDate)
.add(this.state.index + 1 - this.props.size, 'days')
.format('YYYY-MM-DD')
.add(this.state.index + 1 - this.props.size, "days")
.format("YYYY-MM-DD")
);
}
};

render() {
const {
width,
virtualizedListProps,
events,
initDate,
} = this.props;
const { width, virtualizedListProps, events, initDate } = this.props;

return (
<View style={[this.styles.container, { width }]}>
Expand All @@ -198,14 +196,14 @@ export default class EventCalendar extends React.Component {
pagingEnabled
renderItem={this._renderItem.bind(this)}
style={{ width: width }}
onMomentumScrollEnd={event => {
onMomentumScrollEnd={(event) => {
const index = parseInt(event.nativeEvent.contentOffset.x / width);
const date = moment(this.props.initDate).add(
index - this.props.size,
'days'
"days"
);
if (this.props.dateChanged) {
this.props.dateChanged(date.format('YYYY-MM-DD'));
this.props.dateChanged(date.format("YYYY-MM-DD"));
}
this.setState({ index, date });
}}
Expand Down
Loading