React-big-calendar 只显示当前月份和下个月
React-big-calendar Show only current month and next month
如何使用 React 大日历仅显示当前月份和下个月并使其每天动态变化?
我有一个看起来像这样的组件:
import React, {Component} from 'react';
import 'react-big-calendar/lib/css/react-big-calendar.css'
import BigCalendar from 'react-big-calendar';
import moment from 'moment';
import 'moment/locale/pl';
class NewCalendar extends Component {
constructor(props, context) {
super(props, context);
BigCalendar.momentLocalizer(moment);
}
render() {
return (
<div {...this.props}>
<BigCalendar
messages={{next: "Następny", previous: "Poprzedni", today: "Dzisiaj", month: "Miesiąc", week: "Tydzień"}}
culture='pl-PL'
timeslots={1}
events={[]}
views={['month', 'week', 'day']}
min={new Date('2017, 1, 7, 08:00')}
max={new Date('2017, 1, 7, 20:00')}
/>
</div>
);
}
}
export default NewCalendar;
但它只显示从早上 8 点到晚上 8 点的最小和最大小时数,如何将最大和最小时间设置为天数?
我不太明白你的整个问题。
但是如果你想隐藏当前月份之外的日子,你可以使用 css
.rbc-off-range {
/* color: #999999; */
color: transparent;
pointer-events: none;
}
请见附件。
如果您想动态显示日期,只需传入一个 javascript 日期字符串的日期。
<BigCalendar
{...allYourProps}
date={new Date('9-8-1990')
/*evaluates to 'Sat Sep 08 1990 00:00:00 GMT-0400 (EDT)'*/
}
/>
enter image description here
我做了一些研究,在这里找到了一些黑客灵感:
似乎没有像 minDate
或 maxDate
这样容易获得的东西。但是有一个名为 date
的 prop,它是 JS Date()
的一个实例。而date
决定了可见的日历范围。
还有另一个名为 onNavigate
的道具 function
。
所以你应该确保你的状态有一个初始键值对,如:
{
dayChosen: new Date() //just initalize as current moment
}
然后作为 MyCalendar
组件的两个 props 你可以这样写:
date={this.state.dayChosen}
onNavigate={(focusDate, flipUnit, prevOrNext) => {
console.log("what are the 3 params focusDate, flipUnit, prevOrNext ?", focusDate, flipUnit, prevOrNext);
const _this = this;
const now = new Date();
const nowNum = now.getDate();
const nextWeekToday = moment().add(7, "day").toDate();
//I imported `moment.js` earlier
const nextWeekTodayNum = nextWeekToday.getDate();
if (prevOrNext === "NEXT"
&& _this.state.dayChosen.getDate() === nowNum){
_this.setState({
dayChosen: nextWeekToday
});
} else if (prevOrNext === "PREV"
&& _this.state.dayChosen.getDate() === nextWeekTodayNum){
_this.setState({
dayChosen: now
});
}
}}
在我的示例中,用户可以单击按钮 back
和 next
,但我已成功将日历限制为仅显示 this week
和 the following week
。用户无法查看 previous weeks
或 more than 1 week down the road
。如果你想要 monthly restriction
而不是 weekly
,你可以很容易地改变逻辑。
如何使用 React 大日历仅显示当前月份和下个月并使其每天动态变化?
我有一个看起来像这样的组件:
import React, {Component} from 'react';
import 'react-big-calendar/lib/css/react-big-calendar.css'
import BigCalendar from 'react-big-calendar';
import moment from 'moment';
import 'moment/locale/pl';
class NewCalendar extends Component {
constructor(props, context) {
super(props, context);
BigCalendar.momentLocalizer(moment);
}
render() {
return (
<div {...this.props}>
<BigCalendar
messages={{next: "Następny", previous: "Poprzedni", today: "Dzisiaj", month: "Miesiąc", week: "Tydzień"}}
culture='pl-PL'
timeslots={1}
events={[]}
views={['month', 'week', 'day']}
min={new Date('2017, 1, 7, 08:00')}
max={new Date('2017, 1, 7, 20:00')}
/>
</div>
);
}
}
export default NewCalendar;
但它只显示从早上 8 点到晚上 8 点的最小和最大小时数,如何将最大和最小时间设置为天数?
我不太明白你的整个问题。
但是如果你想隐藏当前月份之外的日子,你可以使用 css
.rbc-off-range {
/* color: #999999; */
color: transparent;
pointer-events: none;
}
请见附件。
如果您想动态显示日期,只需传入一个 javascript 日期字符串的日期。
<BigCalendar
{...allYourProps}
date={new Date('9-8-1990')
/*evaluates to 'Sat Sep 08 1990 00:00:00 GMT-0400 (EDT)'*/
}
/>
enter image description here
我做了一些研究,在这里找到了一些黑客灵感:
似乎没有像 minDate
或 maxDate
这样容易获得的东西。但是有一个名为 date
的 prop,它是 JS Date()
的一个实例。而date
决定了可见的日历范围。
还有另一个名为 onNavigate
的道具 function
。
所以你应该确保你的状态有一个初始键值对,如:
{
dayChosen: new Date() //just initalize as current moment
}
然后作为 MyCalendar
组件的两个 props 你可以这样写:
date={this.state.dayChosen}
onNavigate={(focusDate, flipUnit, prevOrNext) => {
console.log("what are the 3 params focusDate, flipUnit, prevOrNext ?", focusDate, flipUnit, prevOrNext);
const _this = this;
const now = new Date();
const nowNum = now.getDate();
const nextWeekToday = moment().add(7, "day").toDate();
//I imported `moment.js` earlier
const nextWeekTodayNum = nextWeekToday.getDate();
if (prevOrNext === "NEXT"
&& _this.state.dayChosen.getDate() === nowNum){
_this.setState({
dayChosen: nextWeekToday
});
} else if (prevOrNext === "PREV"
&& _this.state.dayChosen.getDate() === nextWeekTodayNum){
_this.setState({
dayChosen: now
});
}
}}
在我的示例中,用户可以单击按钮 back
和 next
,但我已成功将日历限制为仅显示 this week
和 the following week
。用户无法查看 previous weeks
或 more than 1 week down the road
。如果你想要 monthly restriction
而不是 weekly
,你可以很容易地改变逻辑。