From 0390790fd97cc62e608a6a33b3834623b8432525 Mon Sep 17 00:00:00 2001 From: Ander Conal Date: Tue, 31 Oct 2017 17:48:00 +0100 Subject: [PATCH 1/9] minDate & maxDate related starting point raised --- example/app.js | 5 +++ src/calendar.js | 86 +++++++++++++++++++++++++++++++++++++----- src/input-moment.js | 8 +++- src/less/calendar.less | 13 +++++++ 4 files changed, 101 insertions(+), 11 deletions(-) diff --git a/example/app.js b/example/app.js index 406b3b2..24189af 100644 --- a/example/app.js +++ b/example/app.js @@ -20,6 +20,9 @@ class App extends Component { }; render() { + const maxDate = moment(); + maxDate.set('month', moment().month() + 1); + return (

@@ -35,6 +38,8 @@ class App extends Component { onChange={this.handleChange} minStep={5} onSave={this.handleSave} + minDate={moment()} + maxDate={maxDate} />

diff --git a/src/calendar.js b/src/calendar.js index 6b8071f..cf536b6 100644 --- a/src/calendar.js +++ b/src/calendar.js @@ -4,26 +4,46 @@ import cx from 'classnames'; import range from 'lodash/range'; import chunk from 'lodash/chunk'; -const Day = ({ i, w, d, className, ...props }) => { +const Day = ({ i, w, d, minDate, maxDate, currentMoment, className, ...props }) => { const prevMonth = w === 0 && i > 7; const nextMonth = w >= 4 && i <= 14; + const currentMomentCopy = moment(); + + if (nextMonth) { + currentMomentCopy.set('month', currentMoment.month() + 1); + } + const cls = cx({ 'prev-month': prevMonth, 'next-month': nextMonth, - 'current-day': !prevMonth && !nextMonth && i === d + 'current-day': !prevMonth && !nextMonth && i === d, + 'disabled-day': minDate ? i < minDate.date() && currentMomentCopy.month() === currentMoment.month() : false || + maxDate ? i > maxDate.date() : false }); return {i}; }; export default class Calendar extends Component { + constructor(props) { + super(props); + + this.state = { + currentTime: this.props.moment + } + } selectDate = (i, w) => { const prevMonth = w === 0 && i > 7; const nextMonth = w >= 4 && i <= 14; const m = this.props.moment; - if (prevMonth) m.subtract(1, 'month'); - if (nextMonth) m.add(1, 'month'); + if (prevMonth) { + m.subtract(1, 'month'); + } + + if (nextMonth) { + m.add(1, 'month'); + } m.date(i); @@ -32,17 +52,39 @@ export default class Calendar extends Component { prevMonth = e => { e.preventDefault(); - this.props.onChange(this.props.moment.subtract(1, 'month')); + const minDate = this.props.minDate; + + if (!this.props.moment.isBefore(minDate) && !this.props.moment.isSame(minDate)) { + this.props.onChange(this.props.moment.subtract(1, 'month')); + } }; nextMonth = e => { e.preventDefault(); - this.props.onChange(this.props.moment.add(1, 'month')); + const maxDate = this.props.maxDate; + + console.log('this.props.moment.isAfter(maxDate)', this.props.moment.isAfter(maxDate)); + console.log('this.props.moment.isSame(maxDate, month)', this.props.moment.isSame(maxDate, 'month')); + console.log('this.props.moment.isSame(maxDate, day)', this.props.moment.isSame(maxDate, 'day')); + + if (!this.props.moment.isAfter(maxDate) && !this.props.moment.isSame(maxDate)) { + this.props.onChange(this.props.moment.add(1, 'month')); + + console.log('this.props.moment', this.props.moment); + console.log('this.props.moment.isAfter(maxDate)', this.props.moment.isAfter(maxDate)); + console.log('this.props.moment.isSame(maxDate, month)', this.props.moment.isSame(maxDate, 'month')); + console.log('this.props.moment.isSame(maxDate, day)', this.props.moment.isSame(maxDate, 'day')); + } }; render() { + const minDate = this.props.minDate; + const maxDate = this.props.maxDate; + const currentMoment = this.props.moment; const m = this.props.moment; const d = m.date(); + const month = m.month(); + const year = m.year(); const d1 = m.clone().subtract(1, 'month').endOf('month').date(); const d2 = m.clone().date(1).day(); const d3 = m.clone().endOf('month').date(); @@ -56,11 +98,31 @@ export default class Calendar extends Component { return (
- {m.format('MMMM YYYY')} -
@@ -75,14 +137,18 @@ export default class Calendar extends Component { {chunk(days, 7).map((row, w) => - {row.map(i => - { + return this.selectDate(i, w)} /> + } )} )} diff --git a/src/input-moment.js b/src/input-moment.js index a8b35c0..b313ff7 100644 --- a/src/input-moment.js +++ b/src/input-moment.js @@ -9,7 +9,9 @@ export default class InputMoment extends Component { prevMonthIcon: 'ion-ios-arrow-left', nextMonthIcon: 'ion-ios-arrow-right', minStep: 1, - hourStep: 1 + hourStep: 1, + minDate: null, + maxDate: null }; state = { @@ -36,6 +38,8 @@ export default class InputMoment extends Component { minStep, hourStep, onSave, + minDate, + maxDate, ...props } = this.props; const cls = cx('m-input-moment', className); @@ -66,6 +70,8 @@ export default class InputMoment extends Component { onChange={this.props.onChange} prevMonthIcon={this.props.prevMonthIcon} nextMonthIcon={this.props.nextMonthIcon} + minDate={this.props.minDate} + maxDate={this.props.maxDate} />