Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.

Commit d894517

Browse files
committed
add disabledDates props and adapt styles
1 parent eb7972f commit d894517

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

demo/src/components/Main.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ export default class Main extends Component {
7272
key: 'selection',
7373
},
7474
},
75+
dateRangeWithDisabled: {
76+
selection: {
77+
startDate: new Date(new Date().getTime() + 24 * 60 * 60 * 1000),
78+
endDate: null,
79+
key: 'selection',
80+
},
81+
},
7582
definedRange: {
7683
selection: {
7784
startDate: new Date(),
@@ -316,6 +323,31 @@ export default class Main extends Component {
316323
className={'centered'}
317324
/>
318325
</Section>
326+
<Section title="RangePicker with disabled dates">
327+
<div>
328+
<input
329+
type="text"
330+
readOnly
331+
value={formatDateDisplay(this.state.dateRangeWithDisabled.selection.startDate)}
332+
/>
333+
<input
334+
type="text"
335+
readOnly
336+
value={formatDateDisplay(
337+
this.state.dateRangeWithDisabled.selection.endDate,
338+
'Continuous'
339+
)}
340+
/>
341+
</div>
342+
343+
<DateRange
344+
onChange={this.handleRangeChange.bind(this, 'dateRangeWithDisabled')}
345+
moveRangeOnFirstSelection={false}
346+
ranges={[this.state.dateRangeWithDisabled.selection]}
347+
className={'PreviewArea'}
348+
disabledDates={[Date()]}
349+
/>
350+
</Section>
319351
</main>
320352
);
321353
}

src/components/Calendar.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class Calendar extends PureComponent {
116116
setTimeout(() => this.focusToDate(this.state.focusedDate), 1);
117117
}
118118
}
119-
componentWillReceiveProps(nextProps) {
119+
UNSAFE_componentWillReceiveProps(nextProps) {
120120
const propMapper = {
121121
dateRange: 'ranges',
122122
date: 'date',
@@ -351,6 +351,7 @@ class Calendar extends PureComponent {
351351
onPreviewChange,
352352
scroll,
353353
direction,
354+
disabledDates,
354355
maxDate,
355356
minDate,
356357
rangeColors,
@@ -400,6 +401,8 @@ class Calendar extends PureComponent {
400401
axis={isVertical ? 'y' : 'x'}
401402
itemRenderer={(index, key) => {
402403
const monthStep = addMonths(minDate, index);
404+
let tomorrow = new Date();
405+
tomorrow.setDate(tomorrow.getDate + 1);
403406
return (
404407
<Month
405408
{...this.props}
@@ -409,6 +412,7 @@ class Calendar extends PureComponent {
409412
key={key}
410413
drag={this.state.drag}
411414
dateOptions={this.dateOptions}
415+
disabledDates={disabledDates}
412416
month={monthStep}
413417
onDragSelectionStart={this.onDragSelectionStart}
414418
onDragSelectionEnd={this.onDragSelectionEnd}
@@ -436,6 +440,8 @@ class Calendar extends PureComponent {
436440
)}>
437441
{new Array(this.props.months).fill(null).map((_, i) => {
438442
const monthStep = addMonths(this.state.focusedDate, i);
443+
let tomorrow = new Date();
444+
tomorrow.setDate(tomorrow.getDate + 1);
439445
return (
440446
<Month
441447
{...this.props}
@@ -445,6 +451,7 @@ class Calendar extends PureComponent {
445451
key={i}
446452
drag={this.state.drag}
447453
dateOptions={this.dateOptions}
454+
disabledDates={disabledDates}
448455
month={monthStep}
449456
onDragSelectionStart={this.onDragSelectionStart}
450457
onDragSelectionEnd={this.onDragSelectionEnd}
@@ -466,6 +473,7 @@ class Calendar extends PureComponent {
466473
Calendar.defaultProps = {
467474
showMonthArrow: true,
468475
showMonthAndYearPickers: true,
476+
disabledDates: [],
469477
classNames: {},
470478
locale: defaultLocale,
471479
ranges: [],
@@ -490,6 +498,7 @@ Calendar.defaultProps = {
490498
Calendar.propTypes = {
491499
showMonthArrow: PropTypes.bool,
492500
showMonthAndYearPickers: PropTypes.bool,
501+
disabledDates: PropTypes.array,
493502
minDate: PropTypes.object,
494503
maxDate: PropTypes.object,
495504
date: PropTypes.object,

src/components/Month.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function renderWeekdays(styles, dateOptions) {
3636
class Month extends PureComponent {
3737
render() {
3838
const now = new Date();
39-
const { displayMode, focusedRange, drag, styles } = this.props;
39+
const { displayMode, focusedRange, drag, styles, disabledDates } = this.props;
4040
const minDate = this.props.minDate && startOfDay(this.props.minDate);
4141
const maxDate = this.props.maxDate && endOfDay(this.props.maxDate);
4242
const monthDisplay = getMonthDisplayRange(this.props.month, this.props.dateOptions);
@@ -68,6 +68,9 @@ class Month extends PureComponent {
6868
const isEndOfMonth = isSameDay(day, monthDisplay.endDateOfMonth);
6969
const isOutsideMinMax =
7070
(minDate && isBefore(day, minDate)) || (maxDate && isAfter(day, maxDate));
71+
const isDisabledSpecifically = disabledDates.some(disabledDate =>
72+
isSameDay(disabledDate, day)
73+
);
7174
return (
7275
<DayCell
7376
{...this.props}
@@ -81,7 +84,7 @@ class Month extends PureComponent {
8184
isStartOfMonth={isStartOfMonth}
8285
isEndOfMonth={isEndOfMonth}
8386
key={index}
84-
disabled={isOutsideMinMax}
87+
disabled={isOutsideMinMax || isDisabledSpecifically}
8588
isPassive={
8689
!isWithinInterval(day, {
8790
start: monthDisplay.startDateOfMonth,
@@ -112,6 +115,7 @@ Month.propTypes = {
112115
month: PropTypes.object,
113116
drag: PropTypes.object,
114117
dateOptions: PropTypes.object,
118+
disabledDates: PropTypes.array,
115119
preview: PropTypes.shape({
116120
startDate: PropTypes.object,
117121
endDate: PropTypes.object,

0 commit comments

Comments
 (0)