) {
+ const {onChange, inputFormat, format, utc} = this.props;
+ const value = e.currentTarget.value;
+ this.setState({endInputValue: value});
+ if (value === '') {
+ onChange('');
+ } else {
+ let newDate = this.getEndDateByDuration(moment(value, inputFormat));
+ this.setState({endDate: newDate});
+ }
+ }
+
+ // 根据 duration 修复结束时间
+ getEndDateByDuration(newValue: moment.Moment) {
+ const {minDuration, maxDuration, type} = this.props;
+ let {startDate, endDate, editState} = this.state;
+ if (!startDate) {
+ return newValue;
+ }
+
+ // 时间范围必须统一成同一天,不然会不一致
+ if (type === 'input-time-range' && startDate) {
+ newValue.set({
+ year: startDate.year(),
+ month: startDate.month(),
+ date: startDate.date()
+ });
+ }
+
+ if (minDuration && newValue.isBefore(startDate.clone().add(minDuration))) {
+ newValue = startDate.clone().add(minDuration);
+ }
+
+ if (maxDuration && newValue.isAfter(startDate.clone().add(maxDuration))) {
+ newValue = startDate.clone().add(maxDuration);
+ }
+
+ return newValue;
+ }
+
+ // 根据 duration 修复起始时间
+ getStartDateByDuration(newValue: moment.Moment) {
+ const {minDuration, maxDuration, type} = this.props;
+ let {endDate, editState} = this.state;
+ if (!endDate) {
+ return newValue;
+ }
+ // 时间范围必须统一成同一天,不然会不一致
+ if (type === 'input-time-range' && endDate) {
+ newValue.set({
+ year: endDate.year(),
+ month: endDate.month(),
+ date: endDate.date()
+ });
+ }
+
+ if (
+ minDuration &&
+ newValue.isBefore(endDate.clone().subtract(minDuration))
+ ) {
+ newValue = endDate.clone().subtract(minDuration);
+ }
+
+ if (
+ maxDuration &&
+ newValue.isAfter(endDate.clone().subtract(maxDuration))
+ ) {
+ newValue = endDate.clone().subtract(maxDuration);
+ }
+
+ return newValue;
}
// 主要用于处理时间的情况
handleTimeStartChange(newValue: moment.Moment) {
- const {embed, timeFormat, minDuration, maxDuration, minDate} = this.props;
+ const {embed, timeFormat, inputFormat, minDuration, maxDuration, minDate} =
+ this.props;
const {startDate, endDate} = this.state;
- if (
- startDate &&
- (!endDate || (endDate && newValue.isSame(startDate))) && // 没有结束时间,或者新的时间也是开始时间,这时都会将新值当成结束时间
- newValue.isSameOrAfter(startDate) &&
- (!minDuration || newValue.isAfter(startDate.clone().add(minDuration))) &&
- (!maxDuration || newValue.isBefore(startDate.clone().add(maxDuration)))
- ) {
- return this.setState(
- {
- endDate: this.filterDate(newValue, endDate, timeFormat, 'end')
- },
- () => {
- embed && this.confirm();
- }
- );
+ // 时间范围必须统一成同一天,不然会不一致
+ if (endDate) {
+ newValue.set({
+ year: endDate.year(),
+ month: endDate.month(),
+ date: endDate.date()
+ });
}
if (minDate && newValue && newValue.isBefore(minDate, 'second')) {
@@ -691,12 +852,8 @@ export class DateRangePicker extends React.Component<
this.setState(
{
- startDate: this.filterDate(
- newValue,
- startDate || minDate,
- timeFormat,
- 'start'
- )
+ startDate: newValue,
+ startInputValue: newValue.format(inputFormat)
},
() => {
embed && this.confirm();
@@ -705,39 +862,40 @@ export class DateRangePicker extends React.Component<
}
handleTimeEndChange(newValue: moment.Moment) {
- const {embed, timeFormat, minDuration, maxDuration, maxDate} = this.props;
+ const {embed, timeFormat, inputFormat, minDuration, maxDuration, maxDate} =
+ this.props;
const {startDate, endDate} = this.state;
-
- if (
- endDate &&
- !startDate &&
- newValue.isSameOrBefore(endDate) &&
- (!minDuration ||
- newValue.isBefore(endDate.clone().subtract(minDuration))) &&
- (!maxDuration || newValue.isAfter(endDate.clone().subtract(maxDuration)))
- ) {
- return this.setState(
- {
- startDate: this.filterDate(newValue, startDate, timeFormat, 'start')
- },
- () => {
- embed && this.confirm();
- }
- );
+ if (startDate) {
+ newValue.set({
+ year: startDate.year(),
+ month: startDate.month(),
+ date: startDate.date()
+ });
}
if (maxDate && newValue && newValue.isAfter(maxDate, 'second')) {
newValue = maxDate;
}
+ if (
+ startDate &&
+ minDuration &&
+ newValue.isAfter(startDate.clone().add(minDuration))
+ ) {
+ newValue = startDate.clone().add(minDuration);
+ }
+ if (
+ startDate &&
+ maxDuration &&
+ newValue.isBefore(startDate.clone().add(maxDuration))
+ ) {
+ newValue = startDate.clone().add(maxDuration);
+ }
+
this.setState(
{
- endDate: this.filterDate(
- newValue,
- endDate || maxDate,
- timeFormat,
- 'end'
- )
+ endDate: newValue,
+ endInputValue: newValue.format(inputFormat)
},
() => {
embed && this.confirm();
@@ -836,7 +994,7 @@ export class DateRangePicker extends React.Component<
e.preventDefault();
e.stopPropagation();
const {resetValue, onChange} = this.props;
-
+ this.setState({startInputValue: '', endInputValue: ''});
onChange(resetValue);
}
@@ -950,6 +1108,7 @@ export class DateRangePicker extends React.Component<
ranges,
locale,
embed,
+ type,
viewMode = 'days'
} = this.props;
const __ = this.props.translate;
@@ -959,7 +1118,6 @@ export class DateRangePicker extends React.Component<
return (
{this.renderRanges(ranges)}
-
-
@@ -1030,8 +1190,10 @@ export class DateRangePicker extends React.Component<
className,
popoverClassName,
classPrefix: ns,
+ classnames: cx,
value,
- placeholder,
+ startPlaceholder,
+ endPlaceholder,
popOverContainer,
inputFormat,
format,
@@ -1059,21 +1221,6 @@ export class DateRangePicker extends React.Component<
const {isOpened, isFocused, startDate, endDate} = this.state;
- const selectedDate = DateRangePicker.unFormatValue(
- value,
- format,
- joinValues,
- delimiter
- );
- const startViewValue = selectedDate.startDate
- ? selectedDate.startDate.format(inputFormat)
- : '';
- const endViewValue = selectedDate.endDate
- ? selectedDate.endDate.format(inputFormat)
- : '';
- const arr = [];
- startViewValue && arr.push(startViewValue);
- endViewValue && arr.push(endViewValue);
const __ = this.props.translate;
const calendarMobile = (
@@ -1138,17 +1285,32 @@ export class DateRangePicker extends React.Component<
className
)}
ref={this.dom}
- onClick={this.handleClick}
>
- {arr.length ? (
-
- {arr.join(__('DateRange.valueConcat'))}
-
- ) : (
-
- {__(placeholder)}
-
- )}
+
+ ~
+
{clearable && !disabled && value ? (
@@ -1185,7 +1347,6 @@ export class DateRangePicker extends React.Component<
className={cx(`${ns}DateRangePicker-popover`, popoverClassName)}
onHide={this.close}
onClick={this.handlePopOverClick}
- overlay
>
{this.renderCalendar()}
diff --git a/src/components/calendar/Calendar.tsx b/src/components/calendar/Calendar.tsx
index 41a27047b..f75d3fa9f 100644
--- a/src/components/calendar/Calendar.tsx
+++ b/src/components/calendar/Calendar.tsx
@@ -223,8 +223,7 @@ class BaseDatePicker extends React.Component<
updatedState.viewDate = moment(props.viewDate);
}
- // time-range 下会有问题,先不支持
- if (Object.keys(updatedState).length && props.viewMode !== 'time') {
+ if (Object.keys(updatedState).length) {
this.setState(updatedState);
}