|
2
|
3
|
4
|
5
|
6
|
7
|
8
diff --git a/src/calendar.js b/src/calendar.js
index 6b8071f..c88f132 100644
--- a/src/calendar.js
+++ b/src/calendar.js
@@ -4,48 +4,59 @@ 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, className, selected, month, isValid, selectDate, ...props }) => {
const prevMonth = w === 0 && i > 7;
const nextMonth = w >= 4 && i <= 14;
+ const m = moment(month);
+ if(prevMonth) m.subtract(1, 'month');
+ if(nextMonth) m.add(1, 'month');
+ m.date(i);
+ var valid = isValid(m);
+
const cls = cx({
'prev-month': prevMonth,
'next-month': nextMonth,
- 'current-day': !prevMonth && !nextMonth && i === d
+ 'current-day': !prevMonth && !nextMonth && i === d,
+ 'valid': valid,
+ 'invalid': !valid
});
- return | {i} | ;
+ return selectDate(m)} {...props}>{i} | ;
};
export default class Calendar extends Component {
- 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');
-
- m.date(i);
+ constructor(props) {
+ super(props);
+ // The inital month shown on the calendar is the month of the current moment
+ this.state = {month: moment(this.props.moment).startOf('month')};
+ }
+ selectDate = selectMoment => {
+ var m = moment(this.props.moment);
+ m.year(selectMoment.year()).month(selectMoment.month()).date(selectMoment.date());
- this.props.onChange(m);
- };
+ if(this.props.isValid(m)) {
+ this.setState({month: moment(m).startOf('month')});
+ this.props.onChange(m);
+ }
+ }
prevMonth = e => {
e.preventDefault();
- this.props.onChange(this.props.moment.subtract(1, 'month'));
+ this.setState({month: moment(this.state.month).subtract(1, 'month')});
};
nextMonth = e => {
e.preventDefault();
- this.props.onChange(this.props.moment.add(1, 'month'));
+ this.setState({month: moment(this.state.month).add(1, 'month')});
};
render() {
const m = this.props.moment;
+ var month = this.state.month;
const d = m.date();
- const d1 = m.clone().subtract(1, 'month').endOf('month').date();
- const d2 = m.clone().date(1).day();
- const d3 = m.clone().endOf('month').date();
+ const d1 = month.clone().subtract(1, 'month').endOf('month').date();
+ const d2 = month.clone().date(1).day();
+ const d3 = month.clone().endOf('month').date();
const days = [].concat(
range(d1 - d2 + 1, d1 + 1),
range(1, d3 + 1),
@@ -59,7 +70,7 @@ export default class Calendar extends Component {
- {m.format('MMMM YYYY')}
+ {month.format('MMMM YYYY')}
@@ -81,7 +92,10 @@ export default class Calendar extends Component {
i={i}
d={d}
w={w}
- onClick={() => this.selectDate(i, w)}
+ month={month}
+ selected={m}
+ isValid={this.props.isValid}
+ selectDate={this.selectDate}
/>
)}
diff --git a/src/input-moment.js b/src/input-moment.js
index a8b35c0..beb3d67 100644
--- a/src/input-moment.js
+++ b/src/input-moment.js
@@ -9,7 +9,8 @@ export default class InputMoment extends Component {
prevMonthIcon: 'ion-ios-arrow-left',
nextMonthIcon: 'ion-ios-arrow-right',
minStep: 1,
- hourStep: 1
+ hourStep: 1,
+ isValid: () => true
};
state = {
@@ -36,6 +37,7 @@ export default class InputMoment extends Component {
minStep,
hourStep,
onSave,
+ isValid,
...props
} = this.props;
const cls = cx('m-input-moment', className);
@@ -66,6 +68,7 @@ export default class InputMoment extends Component {
onChange={this.props.onChange}
prevMonthIcon={this.props.prevMonthIcon}
nextMonthIcon={this.props.nextMonthIcon}
+ isValid={isValid}
/>