import React, { Component } from 'react';
import moment from 'moment';
import './index.less';
import { DatePicker, Calendar, Icon } from 'antd';
export default class extends Component {
constructor(props) {
super(props);
this.state = { key: 0 };
this.loading = false;
}
dateRender(date) {
const { dateRender, checkRefresh } = this.props;
if (!this.loading && checkRefresh) {
if (checkRefresh(date, () => this.needRefresh())) {
this.loading = true;
}
}
if (dateRender) return dateRender(date);
return
{date.get('date')}
;
}
needRefresh() {
this.setState({ key: this.state.key + 1 });
this.loading = false;
setTimeout(() => {
this.setState({ key: this.state.key + 1 });
}, 1);
}
render() {
const { show, hideInput, disabledDate, theme = '', value, onChange } = this.props;
return (
{
if (!this.ref) {
this.ref = ref;
this.setState({ load: false });
}
}}
className={`g-date-block ${hideInput ? 'hide-input' : ''}`}
>
this.ref.parentNode}
dropdownClassName={`g-date ${theme} ${hideInput ? 'hide-input' : ''}`}
disabledDate={disabledDate}
dateRender={date => this.dateRender(date)}
onChange={date => onChange(date)}
/>
);
}
}
export class TwoDate extends Component {
constructor(props) {
super(props);
const today = moment();
this.state = {
key: 0,
value: today,
leftValue: today.clone().subtract(1, 'month'),
rightValue: today,
};
}
dateRender(date) {
const { checkRefresh } = this.props;
if (!this.loading && checkRefresh) {
if (checkRefresh(date, () => this.needRefresh())) {
this.loading = true;
}
}
return this.renderItem(date);
}
needRefresh() {
this.setState({ key: this.state.key + 1 });
this.loading = false;
setTimeout(() => {
this.setState({ key: this.state.key + 1 });
}, 1);
}
renderItem(date) {
const { value } = this.state;
const { getType } = this.props;
return (
);
}
onLeftSelect(date) {
const { onChange, startDate } = this.props;
if (startDate && date.isBefore(startDate)) return;
this.setState({
value: date,
leftValue: date,
rightValue: date.clone().add(1, 'month'),
});
if (onChange) onChange(date);
}
onRightSelect(date) {
const { onChange, endDate } = this.props;
if (endDate && date.isAfter(endDate)) return;
this.setState({
value: date,
rightValue: date,
leftValue: date.clone().subtract(1, 'month'),
});
if (onChange) onChange(date);
}
leftHeadRender() {
const { leftValue } = this.state;
const { extendInfo } = this.props;
return (
this.onLeftSelect(leftValue.clone().subtract(1, 'year'))}
/>
this.onLeftSelect(leftValue.clone().subtract(1, 'month'))} />
{leftValue.year()}年
{leftValue.month() + 1}月
{extendInfo && extendInfo(leftValue)}
);
}
rightHeadRender() {
const { rightValue } = this.state;
const { extendInfo } = this.props;
return (
{rightValue.year()}年
{rightValue.month() + 1}月
{extendInfo && extendInfo(rightValue)}
this.onRightSelect(rightValue.clone().add(1, 'month'))} />
this.onRightSelect(rightValue.clone().add(1, 'year'))}
/>
);
}
render() {
const { disabledDate } = this.props;
const { leftValue, rightValue } = this.state;
return (
{this.leftHeadRender()}
{this.rightHeadRender()}
this.leftHeadRender(data)}
value={leftValue}
disabledDate={disabledDate}
dateFullCellRender={date => this.dateRender(date)}
onSelect={date => this.onLeftSelect(date)}
onPanelChange={date => this.onLeftSelect(date)}
/>
this.rightHeadRender(data)}
value={rightValue}
disabledDate={disabledDate}
dateFullCellRender={date => this.dateRender(date)}
onSelect={date => this.onRightSelect(date)}
onPanelChange={date => this.onRightSelect(date)}
/>
);
}
}