我有一个使用该组件的react-day-picker
组件。我不想有一个可以单独增加或减少年份和月份的日期选择器。所以我已经覆盖了navbarElement
(你可以在最后一行看到)。在箭头函数handleNextYearClick
中,年份应该增加 1。但是我如何date
从父 Component访问和设置DayPicker
。我只能打电话onNextClick
,这只会增加月份+1。
export interface IDatePickerNavbarProps {
date?: Date,
nextMonth?: Date,
previousMonth?: Date,
localeUtils?: any,
locale?: string,
onNextClick?: () => {}
}
export interface IDatePickerNavbarState {
}
class DatePickerNavBar extends Component<IDatePickerNavbarProps, IDatePickerNavbarState> {
constructor(props: IDatePickerNavbarProps) {
super(props);
}
handleNextMonthClick = (event: React.MouseEvent<HTMLElement>) => {
this.props.onNextClick();
}
handleNextYearClick = (event: React.MouseEvent<HTMLElement>) => {
// How to set the new date => current date + 1 year
// I am only able to call this.props.onNextClick() but that does
// +1 only month count
}
render() {
return (
<div className={Styles.DayPickerSelectBox}>
<div className={Styles.DayPickerNavbarYear}>
year:
<span>last</span> - <span onClick={this.handleNextYearClick}>next</span>
</div>
<div className={Styles.DayPickerNavbarMonth}>
month:
<span>last</span> - <span onClick={this.handleNextMonthClick}>next</span>
</div>
</div>
);
}
}
...这是我的render()
主要日期选择器组件中方法内的代码...
<DayPicker navbarElement={<DatePickerNavBar />} captionElement={<DatePickerCaption />} />
不要对打字稿符号感到不安。