diff --git a/components/date-picker/RangePicker.tsx b/components/date-picker/RangePicker.tsx index 41f9fd9fd5..67e08403d1 100644 --- a/components/date-picker/RangePicker.tsx +++ b/components/date-picker/RangePicker.tsx @@ -8,6 +8,24 @@ import Icon from '../icon'; import { getLocaleCode } from '../_util/getLocale'; import warning from '../_util/warning'; +function getShowDateFromValue(value: moment.Moment[]): moment.Moment[] { + const [ start, end ] = value; + const newEnd = end && end.isSame(start, 'month') ? end.clone().add(1, 'month') : end; + return [start, newEnd]; +} + +function formatValue(value: moment.Moment | undefined, format: string): string { + return (value && value.format(format)) || ''; +} + +function pickerValueAdapter(value?: moment.Moment | moment.Moment[]): moment.Moment[] | undefined { + if (!value) { return; } + if (Array.isArray(value)) { + return value; + } + return [value, value.clone().add(1, 'month')]; +} + export default class RangePicker extends React.Component { static contextTypes = { antLocale: React.PropTypes.object, @@ -39,8 +57,7 @@ export default class RangePicker extends React.Component { componentWillReceiveProps(nextProps) { if ('value' in nextProps) { const value = nextProps.value || []; - const showDate = value[0]; - this.setState({ value, showDate }); + this.setState({ value, showDate: getShowDateFromValue(value) }); } if ('open' in nextProps) { this.setState({ @@ -56,14 +73,14 @@ export default class RangePicker extends React.Component { this.handleChange([]); } - handleChange = (value) => { + handleChange = (value: moment.Moment[]) => { const props = this.props; if (!('value' in props)) { - this.setState({ value, showDate: value[0] }); + this.setState({ value, showDate: getShowDateFromValue(value) }); } props.onChange(value, [ - (value[0] && value[0].format(props.format)) || '', - (value[1] && value[1].format(props.format)) || '', + formatValue(value[0], props.format), + formatValue(value[1], props.format), ]); } @@ -158,7 +175,7 @@ export default class RangePicker extends React.Component { dateInputPlaceholder={[startPlaceholder, endPlaceholder]} locale={locale.lang} onOk={onOk} - value={showDate || props.defaultPickerValue || moment()} + value={showDate || pickerValueAdapter(props.defaultPickerValue) || pickerValueAdapter(moment())} onValueChange={this.handleShowDateChange} showToday={showToday} /> diff --git a/package.json b/package.json index f55547aa57..7a45cfdfd7 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "object-assign": "~4.1.0", "omit.js": "^0.1.0", "rc-animate": "~2.3.0", - "rc-calendar": "~7.8.0", + "rc-calendar": "~8.0.0", "rc-cascader": "~0.11.0", "rc-checkbox": "~1.5.0", "rc-collapse": "~1.6.4",