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

Add weekStartsOn prop #120

Merged
merged 1 commit into from
Aug 1, 2021
Merged
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 @@ -36,6 +36,7 @@ const MyComponent = () => (
* **`events`** _(Array)_ - Events to display, in `Event Object` format (see [sub-section below](#event-object))
* **`onEventPress`** _(Function)_ - Callback when event item is clicked
* **`numberOfDays`** _(Number)_ - Set number of days to show in view, can be `1`, `3`, `5`, `7`.
* **`weekStartsOn`** _(Number)_ - Day to start the week (0 is Sunday, 1 is Monday, and so on). Defaults to 1. Only useful when `numberOfDays === 7` or `fixedHorizontally` is true.
* **`formatDateHeader`** _(String)_ - Format for dates of header, default is `MMM D`
* **`selectedDate`** _(Date)_ - Intial date to show week/days in the view. Note: changing this prop will not have any effect in the displayed date; to actually change the date being displayed, use the `goToDate()` method, see below.
* **`onSwipeNext`** _(Function)_ - Callback when calendar is swiped to next week/days
Expand Down
11 changes: 9 additions & 2 deletions src/WeekView/WeekView.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default class WeekView extends Component {
const initialDates = this.calculatePagesDates(
props.selectedDate,
props.numberOfDays,
props.weekStartsOn,
props.prependMostRecent,
props.fixedHorizontally,
);
Expand Down Expand Up @@ -259,14 +260,18 @@ export default class WeekView extends Component {
calculatePagesDates = (
currentMoment,
numberOfDays,
weekStartsOn,
prependMostRecent,
fixedHorizontally,
) => {
const initialDates = [];
const centralDate = moment(currentMoment);
if (numberOfDays === 7 || fixedHorizontally) {
// Start week on monday
centralDate.startOf('isoWeek');
centralDate.subtract(
// Ensure centralDate is before currentMoment
(centralDate.day() + 7 - weekStartsOn) % 7,
'days',
);
}
for (let i = -this.pageOffset; i <= this.pageOffset; i += 1) {
const initialDate = moment(centralDate).add(numberOfDays * i, 'd');
Expand Down Expand Up @@ -455,6 +460,7 @@ WeekView.propTypes = {
events: PropTypes.arrayOf(Event.propTypes.event),
formatDateHeader: PropTypes.string,
numberOfDays: PropTypes.oneOf(availableNumberOfDays).isRequired,
weekStartsOn: PropTypes.number,
onSwipeNext: PropTypes.func,
onSwipePrev: PropTypes.func,
onEventPress: PropTypes.func,
Expand Down Expand Up @@ -482,6 +488,7 @@ WeekView.defaultProps = {
events: [],
locale: 'en',
hoursInDisplay: 6,
weekStartsOn: 1,
timeStep: 60,
formatTimeLabel: 'H:mm',
startHour: 0,
Expand Down