Skip to content

Commit b0e503e

Browse files
authored
Merge pull request #5 from simpletut/handleMonthsBetter
Handle pagination for `showNumberOfMonths` option
2 parents 626a76a + 3f8d1ae commit b0e503e

File tree

12 files changed

+214
-97
lines changed

12 files changed

+214
-97
lines changed

.storybook/preview.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import './../styles/main.min.css';
1+
import './../styles/main.css';
22

33
export const parameters = {
44
actions: { argTypesRegex: "^on[A-Z].*" },

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Please copy and self host the default CSS linked below.
8888

8989
**Type:** Number\
9090
**Default:** 12\
91+
**Options** 12 | 4 | 2 | 1
9192
**Description:** Number of Months to show
9293

9394
### showCurrentYear

example/src/styles.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
display: flex;
5858
flex-direction: row;
5959
flex-wrap: wrap;
60+
justify-content: center;
61+
align-items: flex-start;
6062
margin: 0 -10px;
6163
}
6264

@@ -69,6 +71,14 @@
6971
box-sizing: border-box;
7072
}
7173

74+
.singleCol .month {
75+
width: 100%;
76+
}
77+
78+
.twoCol .month {
79+
width: 50%;
80+
}
81+
7282
.monthName {
7383
font-family: arial;
7484
font-size: 24px;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "reactjs-availability-calendar",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "React Availability Calendar",
55
"main": "./dist/cjs/index.js",
66
"module": "./dist/esm/index.js",

src/components/Calendar/Controls/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import React, { FC } from 'react'
1+
import React from 'react'
22
import { IControls } from './../types'
33

4-
const Controls: FC<IControls> = ({ prevYear, initCal, nextYear }): JSX.Element => {
4+
const Controls = ({ prev, initCal, next }: IControls): JSX.Element => {
55
return (
66
<div className='controls' data-testid='controls'>
7-
<button className='btn back' onClick={() => prevYear()} data-testid='back'>
7+
<button className='btn back' onClick={() => prev()} data-testid='back'>
88
Back
99
</button>
1010

1111
<button className='btn now' onClick={() => initCal()} data-testid='now'>
12-
Current Year
12+
Reset
1313
</button>
1414

15-
<button className='btn next' onClick={() => nextYear()} data-testid='next'>
15+
<button className='btn next' onClick={() => next()} data-testid='next'>
1616
Next
1717
</button>
1818
</div>

src/components/Calendar/Utils/index.tsx

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,54 @@ import {
77
IGetAllBookedDays,
88
IGetAllHalfDays,
99
IGetDatesInRange,
10+
IGetMonthName,
1011
DaysOfWeekType,
1112
Days,
1213
DaysOfWeekOffsetType,
1314
DayOffset,
1415
} from './../types'
1516

17+
const dateFormat = 'M-D-YYYY'
18+
19+
export const daysOfTheWeek: DaysOfWeekType = [
20+
Days.Monday,
21+
Days.Tusday,
22+
Days.Wednesday,
23+
Days.Thursday,
24+
Days.Friday,
25+
Days.Saturday,
26+
Days.Sunday,
27+
]
28+
29+
export const daysOfTheWeekOffset: DaysOfWeekOffsetType = [
30+
DayOffset.Monday,
31+
DayOffset.Tusday,
32+
DayOffset.Wednesday,
33+
DayOffset.Thursday,
34+
DayOffset.Friday,
35+
DayOffset.Saturday,
36+
DayOffset.Sunday,
37+
]
38+
39+
export const getMonthName = (month: number): string => {
40+
const months: IGetMonthName = {
41+
1: 'January',
42+
2: 'February',
43+
3: 'March',
44+
4: 'April',
45+
5: 'May',
46+
6: 'June',
47+
7: 'July',
48+
8: 'August',
49+
9: 'September',
50+
10: 'October',
51+
11: 'November',
52+
12: 'December',
53+
}
54+
55+
return months[month]
56+
}
57+
1658
export const formatBookingsData = ({ bookings, year }: IFormatBookingsData): BookingType[] => {
1759
if (!Array.isArray(bookings) || bookings.length < 1) return []
1860

@@ -29,8 +71,8 @@ export const formatBookingsData = ({ bookings, year }: IFormatBookingsData): Boo
2971
if (!validStartDate && !validEndDate) return null
3072

3173
const nxtBooking: BookingType = {
32-
from: dayjs(from).format('MM-DD-YYYY'),
33-
to: dayjs(to).format('MM-DD-YYYY'),
74+
from: dayjs(from).format(dateFormat),
75+
to: dayjs(to).format(dateFormat),
3476
middayCheckout,
3577
}
3678

@@ -46,8 +88,8 @@ export const getDatesInRange = ({ startDate, endDate }: IGetDatesInRange): block
4688

4789
const dates: blockedDaysType = []
4890

49-
while (!_startDate.isAfter(dayjs(_endDate))) {
50-
dates.push(_startDate.format('YYYY-M-D'))
91+
while (!_startDate.isAfter(_endDate)) {
92+
dates.push(_startDate.format(dateFormat))
5193

5294
_startDate = _startDate.add(1, 'day')
5395
}
@@ -95,23 +137,3 @@ export const handleBookings = ({
95137

96138
return { halfDays, bookedDays }
97139
}
98-
99-
export const daysOfTheWeek: DaysOfWeekType = [
100-
Days.Monday,
101-
Days.Tusday,
102-
Days.Wednesday,
103-
Days.Thursday,
104-
Days.Friday,
105-
Days.Saturday,
106-
Days.Sunday,
107-
]
108-
109-
export const daysOfTheWeekOffset: DaysOfWeekOffsetType = [
110-
DayOffset.Monday,
111-
DayOffset.Tusday,
112-
DayOffset.Wednesday,
113-
DayOffset.Thursday,
114-
DayOffset.Friday,
115-
DayOffset.Saturday,
116-
DayOffset.Sunday,
117-
]

src/components/Calendar/Year/index.tsx

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
import React, { FC } from 'react'
1+
import React from 'react'
22
import dayjs from 'dayjs'
33
import isBetween from 'dayjs/plugin/isBetween'
44
import { IYear } from './../types'
5-
import { daysOfTheWeek, daysOfTheWeekOffset } from './../Utils'
5+
import { daysOfTheWeek, daysOfTheWeekOffset, getMonthName } from './../Utils'
66

77
dayjs.extend(isBetween)
88

9-
const Year: FC<IYear> = ({
10-
showNumberOfMonths,
11-
bookedDates,
12-
lateCheckouts,
13-
currentYear = dayjs().year(),
14-
}): JSX.Element => {
9+
const Year = ({
10+
activeYear,
11+
showNumberOfMonths = 12,
12+
bookedDates = [],
13+
lateCheckouts = [],
14+
monthsFrom = 1,
15+
}: IYear): JSX.Element => {
16+
const _year = activeYear || dayjs().year()
17+
1518
return (
1619
<div className='year' data-testid='year'>
1720
{new Array(showNumberOfMonths).fill('').map((_, pos) => {
1821
const arrOffset = 1
19-
const month = pos + arrOffset
20-
const date = `${currentYear}-${month}`
21-
const monthName = dayjs(date).format('MMMM')
22+
const month = monthsFrom + pos
23+
const date = `${_year}-${month}`
24+
const monthName = getMonthName(month)
2225
const totalDays = dayjs(date).daysInMonth()
23-
2426
const firstDayOfWeek = dayjs(`${date}-01`).day()
27+
2528
const offsetDays =
2629
firstDayOfWeek !== 0
2730
? new Array(firstDayOfWeek - arrOffset).fill('')
@@ -50,10 +53,11 @@ const Year: FC<IYear> = ({
5053

5154
{daysArr.map((_, pos) => {
5255
const day = pos + arrOffset
53-
const isBooked = Array.isArray(bookedDates) ? bookedDates.includes(`${date}-${day}`) : false
54-
const isLateCheckout = Array.isArray(lateCheckouts)
55-
? lateCheckouts.includes(`${dayjs(`${date}-${day}`).format('MM-DD-YYYY')}`)
56-
: false
56+
const _date = `${month}-${day}-${_year}`
57+
58+
const isBooked = Array.isArray(bookedDates) ? bookedDates.includes(_date) : false
59+
60+
const isLateCheckout = Array.isArray(lateCheckouts) ? lateCheckouts.includes(_date) : false
5761

5862
return (
5963
<div
@@ -72,10 +76,4 @@ const Year: FC<IYear> = ({
7276
)
7377
}
7478

75-
Year.defaultProps = {
76-
showNumberOfMonths: 12,
77-
bookedDates: [],
78-
lateCheckouts: [],
79-
}
80-
8179
export default Year

0 commit comments

Comments
 (0)