Skip to content

Commit 25409ad

Browse files
Merge pull request #109 from iM-GeeKy/disabled-dates
Introduce disabled dates prop.
2 parents c1dd7f0 + d18ed51 commit 25409ad

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export default function ReadMeExampleSingle() {
9595
// validRange={{
9696
// startDate: new Date(2021, 1, 2), // optional
9797
// endDate: new Date(), // optional
98+
// disabledDates: [new Date()] // optional
9899
// }}
99100
// onChange={} // same props as onConfirm but triggered without confirmed by user
100101
// saveLabel="Save" // optional
@@ -149,6 +150,7 @@ export default function ReadMeExampleRange() {
149150
// validRange={{
150151
// startDate: new Date(2021, 1, 2), // optional
151152
// endDate: new Date(), // optional
153+
// disabledDates: [new Date()] // optional
152154
// }}
153155
// onChange={} // same props as onConfirm but triggered without confirmed by user
154156
// locale={'nl'} // optional
@@ -203,6 +205,7 @@ export default function ReadMeExampleMultiple() {
203205
// validRange={{
204206
// startDate: new Date(2021, 1, 2), // optional
205207
// endDate: new Date(), // optional
208+
// disabledDates: [new Date()] // optional
206209
// }}
207210
// locale={'nl'} // optional
208211
// saveLabel="Save" // optional

src/Date/Calendar.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type ScrollModeType = 'horizontal' | 'vertical'
2525
export type ValidRangeType = {
2626
startDate?: Date
2727
endDate?: Date
28+
disabledDates?: Date[]
2829
}
2930

3031
export type BaseCalendarProps = {

src/Date/Month.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ function Month(props: MonthSingleProps | MonthRangeProps | MonthMultiProps) {
120120
)
121121
: undefined
122122

123+
const validDisabledDates = validRange?.disabledDates
124+
? validRange?.disabledDates
125+
: undefined
126+
123127
const { monthName, month, year } = React.useMemo(() => {
124128
const md = addMonths(new Date(), realIndex)
125129
const y = md.getFullYear()
@@ -153,11 +157,22 @@ function Month(props: MonthSingleProps | MonthRangeProps | MonthMultiProps) {
153157
let inRange = false
154158
let disabled = false
155159
let selected = false
160+
let inDisabledDates = false
156161
let leftCrop = dayOfMonth === 1
157162
let rightCrop = dayOfMonth === daysInMonth
158163

159164
const isFirstDayOfMonth = dayOfMonth === 1
160165
const isLastDayOfMonth = dayOfMonth === daysInMonth
166+
167+
inDisabledDates = validDisabledDates
168+
? validDisabledDates.some((disabledDate) =>
169+
areDatesOnSameDay(disabledDate, day)
170+
)
171+
: false
172+
if (inDisabledDates) {
173+
disabled = true
174+
}
175+
161176
if (mode === 'range') {
162177
const selectedStartDay = areDatesOnSameDay(day, startDate)
163178
const selectedEndDay = areDatesOnSameDay(day, endDate)
@@ -237,7 +252,7 @@ function Month(props: MonthSingleProps | MonthRangeProps | MonthMultiProps) {
237252
endUnix: validRangeEnd,
238253
})
239254

240-
if (inRange) {
255+
if (inRange && !inDisabledDates) {
241256
disabled = false
242257
}
243258

@@ -273,6 +288,7 @@ function Month(props: MonthSingleProps | MonthRangeProps | MonthMultiProps) {
273288
dates,
274289
validRangeStart,
275290
validRangeEnd,
291+
validDisabledDates,
276292
mode,
277293
])
278294

0 commit comments

Comments
 (0)