Authored by 于良

日历选择 切换至月份选择时年份显示不对 修复 ;日历周数选择优化,只能选择本周之前的周数 review by 盖剑秋

... ... @@ -111,11 +111,6 @@ export default class Calendar extends Component {
};
this.selectMode = selectMode;
let initDate;
if (this.selectMode === 'day') {
// initDate = this.props.selected.day;
}
this.month = this.generateMonth(new Date());
this.weeksInMonth = this.generateWeeks(new Date());
this.monthsInYear = this.generateMonthsInYear(new Date());
... ... @@ -137,6 +132,7 @@ export default class Calendar extends Component {
this.month = this.generateMonth(this.state.month[15].date);
this.weeksInMonth = this.generateWeeks(this.state.month[15].date);
this.monthsInYear = this.generateMonthsInYear(this.state.month[15].date);
this.currentYear = this.state.month[15].date.getFullYear();
this.setState({
selectMode: this.selectMode,
month: this.month,
... ... @@ -305,20 +301,19 @@ export default class Calendar extends Component {
this.selected.selectMode = this.state.selectMode;
if (this.state.selectMode === 'day') {
this.selectDay(value);
this.selectDay(value.date);
}
if (this.state.selectMode === 'week') {
this.selectWeek(value);
this.selectWeek(value.belongWeek);
}
if (this.state.selectMode === 'month') {
this.selectMonth(value);
this.selectMonth(value.month);
}
}
selectDay(value) {
let {date} = value;
selectDay(date) {
let {month} = this;
month = month.map((item) => {
let status = item.date.toDateString() === date.toDateString() ? 'selected' : 'common';
... ... @@ -343,8 +338,7 @@ export default class Calendar extends Component {
});
}
selectWeek(value) {
let {belongWeek} = value;
selectWeek(belongWeek) {
let {month, weeksInMonth} = this;
weeksInMonth = weeksInMonth.map((item) => {
... ... @@ -363,13 +357,13 @@ export default class Calendar extends Component {
return !day.disabled && day.belongWeek === belongWeek;
});
let firstDay = currentWeekDays[0];
let lastDay = currentWeekDays[currentWeekDays.length - 1];
let firstDay = currentWeekDays.length > 0 ? currentWeekDays[0] : null;
let lastDay = currentWeekDays.length > 0 ? currentWeekDays[currentWeekDays.length - 1] : null;
month = month.map((day) => {
let status = day.belongWeek === belongWeek ? 'selected' : 'common';
// let first = day.date.toDateString() === firstDay.date.toDateString() ? true : false;
let last = day.date.toDateString() === lastDay.date.toDateString() ? true : false;
let last = lastDay && day.date.toDateString() === lastDay.date.toDateString() ? true : false;
return {
date: day.date,
status: status,
... ... @@ -381,8 +375,8 @@ export default class Calendar extends Component {
});
this.selected.week = belongWeek;
this.selected.from = firstDay.date;
this.selected.to = lastDay.date
this.selected.from = firstDay && firstDay.date;
this.selected.to = lastDay && lastDay.date
this.month = month;
this.weeksInMonth = weeksInMonth;
... ... @@ -393,8 +387,7 @@ export default class Calendar extends Component {
});
}
selectMonth(value) {
let {month} = value;
selectMonth(month) {
let {monthsInYear} = this;
monthsInYear = monthsInYear.map((item) => {
let status = item.month == month ? 'selected' : 'common';
... ... @@ -459,38 +452,51 @@ export default class Calendar extends Component {
}
onOK() {
if (this.selected.selectMode === 'day') {
if (this.state.selectMode === 'day') {
if (!this.selected.day) {
Alert.alert(
'请选择日期',
'',
[
{text: '确定', onPress: () => console.log('OK Pressed')},
]
[{text: '确定'},]
)
return;
}
} else if (this.selected.selectMode === 'day') {
} else if (this.state.selectMode === 'week') {
if (!this.selected.from || !this.selected.to || !this.selected.week) {
Alert.alert(
'请选择周',
'请选择周',
'',
[
{text: '确定', onPress: () => console.log('OK Pressed')},
]
[{text: '确定'},]
)
return;
}
} else if (this.selected.selectMode === 'day') {
let fromYear = this.selected.from.getFullYear();
let fromMonth = this.selected.from.getMonth();
let now = new Date();
let nowYear = now.getFullYear();
let nowMonth = now.getMonth();
let nowDate = now.getDate();
if (fromYear <= nowYear && fromMonth <= nowMonth) {
let nowWeek = this.getMonthWeek(nowYear, nowMonth, nowDate);
if (nowWeek === this.selected.week) {
Alert.alert(
'请选择本周之前的周次',
'',
[{text: '确定'},]
)
return;
}
}
} else if (this.state.selectMode === 'month') {
if (!this.selected.year || !this.selected.month) {
Alert.alert(
'请选择月份',
'',
[
{text: '确定', onPress: () => console.log('OK Pressed')},
]
[{text: '确定'},]
)
return;
... ... @@ -500,6 +506,21 @@ export default class Calendar extends Component {
this.props.onOK && this.props.onOK(this.selected);
}
/**
* 计算日期在当月是第几周
* 本方法只在周一作为每周第一天时有效!!!
* y date.getFullYear()
* m date.getMonth() 0-11
* d date.getDate() 1-31
*/
getMonthWeek(y, m, d) {
let monthFirstDay = new Date(y, m, 1).getDay(); //本月第一天周几,0-6
let offset = monthFirstDay === 0 ? 6 : monthFirstDay - 1;
let index = d + offset - 1;
let weeksInMonth = Math.floor(index / 7) + 1;
return weeksInMonth;
}
renderContent() {
return (
<Month
... ...