2018-03-19 10:16:27 +08:00
|
|
|
|
2018-03-17 21:38:29 +08:00
|
|
|
import * as moment from 'moment'
|
|
|
|
import RangeCalendar from '../vc-calendar/src/RangeCalendar'
|
|
|
|
import VcDatePicker from '../vc-calendar/src/Picker'
|
|
|
|
import classNames from 'classnames'
|
2018-09-05 21:28:54 +08:00
|
|
|
import shallowequal from 'shallowequal'
|
2018-03-17 21:38:29 +08:00
|
|
|
import Icon from '../icon'
|
2018-09-05 21:28:54 +08:00
|
|
|
import Tag from '../tag'
|
2018-04-07 14:29:59 +08:00
|
|
|
import interopDefault from '../_util/interopDefault'
|
2018-03-17 21:38:29 +08:00
|
|
|
import { RangePickerProps } from './interface'
|
2018-12-08 16:47:05 +08:00
|
|
|
import { hasProp, getOptionProps, initDefaultProps, mergeProps, getComponentFromProp, isValidElement } from '../_util/props-util'
|
2018-03-17 21:38:29 +08:00
|
|
|
import BaseMixin from '../_util/BaseMixin'
|
2018-12-08 16:47:05 +08:00
|
|
|
import { cloneElement } from '../_util/vnode'
|
2018-03-17 21:38:29 +08:00
|
|
|
function noop () {}
|
|
|
|
function getShowDateFromValue (value) {
|
|
|
|
const [start, end] = value
|
|
|
|
// value could be an empty array, then we should not reset showDate
|
|
|
|
if (!start && !end) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const newEnd = end && end.isSame(start, 'month') ? end.clone().add(1, 'month') : end
|
|
|
|
return [start, newEnd]
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatValue (value, format) {
|
|
|
|
return (value && value.format(format)) || ''
|
|
|
|
}
|
|
|
|
|
|
|
|
function pickerValueAdapter (value) {
|
|
|
|
if (!value) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return [value, value.clone().add(1, 'month')]
|
|
|
|
}
|
|
|
|
|
|
|
|
function isEmptyArray (arr) {
|
|
|
|
if (Array.isArray(arr)) {
|
|
|
|
return arr.length === 0 || arr.every(i => !i)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
function fixLocale (value, localeCode) {
|
|
|
|
if (!localeCode) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!value || value.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
2019-01-02 20:20:02 +08:00
|
|
|
const [start, end] = value
|
|
|
|
if (start) {
|
|
|
|
start.locale(localeCode)
|
2018-03-17 21:38:29 +08:00
|
|
|
}
|
2019-01-02 20:20:02 +08:00
|
|
|
if (end) {
|
|
|
|
end.locale(localeCode)
|
2018-03-17 21:38:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
mixins: [BaseMixin],
|
2018-04-08 21:17:20 +08:00
|
|
|
name: 'ARangePicker',
|
2018-03-17 21:38:29 +08:00
|
|
|
props: initDefaultProps(RangePickerProps(), {
|
|
|
|
prefixCls: 'ant-calendar',
|
2018-09-05 21:28:54 +08:00
|
|
|
tagPrefixCls: 'ant-tag',
|
2018-03-17 21:38:29 +08:00
|
|
|
allowClear: true,
|
|
|
|
showToday: false,
|
|
|
|
}),
|
2018-04-14 21:22:09 +08:00
|
|
|
model: {
|
|
|
|
prop: 'value',
|
|
|
|
event: 'change',
|
|
|
|
},
|
2018-03-17 21:38:29 +08:00
|
|
|
data () {
|
|
|
|
const value = this.value || this.defaultValue || []
|
2019-01-02 20:20:02 +08:00
|
|
|
const [start, end] = value
|
2018-03-17 21:38:29 +08:00
|
|
|
if (
|
2019-01-02 20:20:02 +08:00
|
|
|
start && !interopDefault(moment).isMoment(start) ||
|
|
|
|
end && !interopDefault(moment).isMoment(end)
|
2018-03-17 21:38:29 +08:00
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
'The value/defaultValue of RangePicker must be a moment object array after `antd@2.0`, ' +
|
|
|
|
'see: https://u.ant.design/date-picker-value',
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const pickerValue = !value || isEmptyArray(value) ? this.defaultPickerValue : value
|
|
|
|
return {
|
|
|
|
sValue: value,
|
2018-04-07 14:29:59 +08:00
|
|
|
sShowDate: pickerValueAdapter(pickerValue || interopDefault(moment)()),
|
2018-03-17 21:38:29 +08:00
|
|
|
sOpen: this.open,
|
|
|
|
sHoverValue: [],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
value (val) {
|
|
|
|
const value = val || []
|
2018-09-05 21:28:54 +08:00
|
|
|
let state = { sValue: value }
|
|
|
|
if (!shallowequal(val, this.sValue)) {
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
sShowDate: getShowDateFromValue(value) || this.sShowDate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.setState(state)
|
2018-03-17 21:38:29 +08:00
|
|
|
},
|
|
|
|
open (val) {
|
|
|
|
this.setState({
|
|
|
|
sOpen: val,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
clearSelection (e) {
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
this.setState({ sValue: [] })
|
|
|
|
this.handleChange([])
|
|
|
|
},
|
|
|
|
|
|
|
|
clearHoverValue () {
|
|
|
|
this.setState({ sHoverValue: [] })
|
|
|
|
},
|
|
|
|
|
|
|
|
handleChange (value) {
|
|
|
|
if (!hasProp(this, 'value')) {
|
|
|
|
this.setState(({ sShowDate }) => ({
|
|
|
|
sValue: value,
|
|
|
|
sShowDate: getShowDateFromValue(value) || sShowDate,
|
|
|
|
}))
|
|
|
|
}
|
2019-01-02 20:20:02 +08:00
|
|
|
const [start, end] = value
|
2018-03-17 21:38:29 +08:00
|
|
|
this.$emit('change', value, [
|
2019-01-02 20:20:02 +08:00
|
|
|
formatValue(start, this.format),
|
|
|
|
formatValue(end, this.format),
|
2018-03-17 21:38:29 +08:00
|
|
|
])
|
|
|
|
},
|
|
|
|
|
|
|
|
handleOpenChange (open) {
|
|
|
|
if (!hasProp(this, 'open')) {
|
|
|
|
this.setState({ sOpen: open })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (open === false) {
|
|
|
|
this.clearHoverValue()
|
|
|
|
}
|
|
|
|
this.$emit('openChange', open)
|
2019-01-02 20:20:02 +08:00
|
|
|
|
|
|
|
if (!open) {
|
|
|
|
this.focus()
|
|
|
|
}
|
2018-03-17 21:38:29 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
handleShowDateChange (showDate) {
|
|
|
|
this.setState({ sShowDate: showDate })
|
|
|
|
},
|
|
|
|
|
|
|
|
handleHoverChange (hoverValue) {
|
|
|
|
this.setState({ sHoverValue: hoverValue })
|
|
|
|
},
|
|
|
|
|
|
|
|
handleRangeMouseLeave () {
|
|
|
|
if (this.sOpen) {
|
|
|
|
this.clearHoverValue()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleCalendarInputSelect (value) {
|
2019-01-02 20:20:02 +08:00
|
|
|
const [start] = value
|
|
|
|
if (!start) {
|
2018-03-17 21:38:29 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
this.setState(({ sShowDate }) => ({
|
|
|
|
sValue: value,
|
|
|
|
sShowDate: getShowDateFromValue(value) || sShowDate,
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
|
|
|
|
handleRangeClick (value) {
|
|
|
|
if (typeof value === 'function') {
|
|
|
|
value = value()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setValue(value, true)
|
|
|
|
this.$emit('ok', value)
|
2018-12-08 16:47:05 +08:00
|
|
|
this.$emit('openChange', false)
|
2018-03-17 21:38:29 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
setValue (value, hidePanel) {
|
|
|
|
this.handleChange(value)
|
|
|
|
if ((hidePanel || !this.showTime) && !hasProp(this, 'open')) {
|
|
|
|
this.setState({ sOpen: false })
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
onMouseEnter (e) {
|
|
|
|
this.$emit('mouseenter', e)
|
|
|
|
},
|
|
|
|
onMouseLeave (e) {
|
|
|
|
this.$emit('mouseleave', e)
|
|
|
|
},
|
|
|
|
|
2018-03-17 21:38:29 +08:00
|
|
|
focus () {
|
|
|
|
this.$refs.picker.focus()
|
|
|
|
},
|
|
|
|
|
|
|
|
blur () {
|
|
|
|
this.$refs.picker.blur()
|
|
|
|
},
|
|
|
|
|
|
|
|
renderFooter (...args) {
|
2018-09-05 21:28:54 +08:00
|
|
|
const { prefixCls, ranges, $scopedSlots, $slots, tagPrefixCls } = this
|
2018-03-18 22:05:55 +08:00
|
|
|
const renderExtraFooter = this.renderExtraFooter || $scopedSlots.renderExtraFooter || $slots.renderExtraFooter
|
2018-03-17 21:38:29 +08:00
|
|
|
if (!ranges && !renderExtraFooter) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
const customFooter = renderExtraFooter ? (
|
|
|
|
<div class={`${prefixCls}-footer-extra`} key='extra'>
|
2018-03-18 22:05:55 +08:00
|
|
|
{typeof renderExtraFooter === 'function' ? renderExtraFooter(...args) : renderExtraFooter}
|
2018-03-17 21:38:29 +08:00
|
|
|
</div>
|
|
|
|
) : null
|
|
|
|
const operations = Object.keys(ranges || {}).map((range) => {
|
|
|
|
const value = ranges[range]
|
|
|
|
return (
|
2018-09-05 21:28:54 +08:00
|
|
|
<Tag
|
2018-03-17 21:38:29 +08:00
|
|
|
key={range}
|
2018-09-05 21:28:54 +08:00
|
|
|
prefixCls={tagPrefixCls}
|
|
|
|
color='blue'
|
2018-03-17 21:38:29 +08:00
|
|
|
onClick={() => this.handleRangeClick(value)}
|
|
|
|
onMouseenter={() => this.setState({ sHoverValue: value })}
|
|
|
|
onMouseleave={this.handleRangeMouseLeave}
|
|
|
|
>
|
|
|
|
{range}
|
2018-09-05 21:28:54 +08:00
|
|
|
</Tag>
|
2018-03-17 21:38:29 +08:00
|
|
|
)
|
|
|
|
})
|
2018-12-08 16:47:05 +08:00
|
|
|
const rangeNode = (operations && operations.length > 0) ? (
|
2018-03-17 21:38:29 +08:00
|
|
|
<div class={`${prefixCls}-footer-extra ${prefixCls}-range-quick-selector`} key='range'>
|
|
|
|
{operations}
|
2018-12-08 16:47:05 +08:00
|
|
|
</div>) : null
|
2018-03-17 21:38:29 +08:00
|
|
|
return [rangeNode, customFooter]
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const props = getOptionProps(this)
|
2018-12-08 16:47:05 +08:00
|
|
|
let suffixIcon = getComponentFromProp(this, 'suffixIcon')
|
|
|
|
suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon
|
2018-03-17 21:38:29 +08:00
|
|
|
const { sValue: value, sShowDate: showDate, sHoverValue: hoverValue, sOpen: open, $listeners, $scopedSlots } = this
|
|
|
|
const { calendarChange = noop, ok = noop, focus = noop, blur = noop, panelChange = noop } = $listeners
|
|
|
|
const {
|
|
|
|
prefixCls, popupStyle,
|
|
|
|
disabledDate, disabledTime,
|
|
|
|
showTime, showToday,
|
|
|
|
ranges, locale, localeCode, format,
|
|
|
|
} = props
|
|
|
|
const dateRender = props.dateRender || $scopedSlots.dateRender
|
|
|
|
fixLocale(value, localeCode)
|
|
|
|
fixLocale(showDate, localeCode)
|
|
|
|
|
|
|
|
const calendarClassName = classNames({
|
|
|
|
[`${prefixCls}-time`]: showTime,
|
|
|
|
[`${prefixCls}-range-with-ranges`]: ranges,
|
|
|
|
})
|
|
|
|
|
|
|
|
// 需要选择时间时,点击 ok 时才触发 onChange
|
|
|
|
const pickerChangeHandler = {
|
|
|
|
on: {
|
|
|
|
change: this.handleChange,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
let calendarProps = {
|
|
|
|
on: {
|
|
|
|
ok: this.handleChange,
|
|
|
|
}, props: {},
|
|
|
|
}
|
|
|
|
if (props.timePicker) {
|
|
|
|
pickerChangeHandler.on.change = changedValue => this.handleChange(changedValue)
|
|
|
|
} else {
|
|
|
|
calendarProps = { on: {}, props: {}}
|
|
|
|
}
|
|
|
|
if ('mode' in props) {
|
|
|
|
calendarProps.props.mode = props.mode
|
|
|
|
}
|
|
|
|
|
|
|
|
const startPlaceholder = ('placeholder' in props)
|
|
|
|
? props.placeholder[0] : locale.lang.rangePlaceholder[0]
|
|
|
|
const endPlaceholder = ('placeholder' in props)
|
|
|
|
? props.placeholder[1] : locale.lang.rangePlaceholder[1]
|
|
|
|
const rangeCalendarProps = mergeProps(calendarProps, {
|
|
|
|
props: {
|
|
|
|
format: format,
|
|
|
|
prefixCls: prefixCls,
|
|
|
|
renderFooter: this.renderFooter,
|
|
|
|
timePicker: props.timePicker,
|
|
|
|
disabledDate: disabledDate,
|
|
|
|
disabledTime: disabledTime,
|
|
|
|
dateInputPlaceholder: [startPlaceholder, endPlaceholder],
|
|
|
|
locale: locale.lang,
|
|
|
|
dateRender: dateRender,
|
|
|
|
value: showDate,
|
|
|
|
hoverValue: hoverValue,
|
|
|
|
showToday: showToday,
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
change: calendarChange,
|
|
|
|
ok: ok,
|
|
|
|
valueChange: this.handleShowDateChange,
|
|
|
|
hoverChange: this.handleHoverChange,
|
2018-12-08 16:47:05 +08:00
|
|
|
panelChange,
|
2018-03-17 21:38:29 +08:00
|
|
|
inputSelect: this.handleCalendarInputSelect,
|
|
|
|
},
|
|
|
|
class: calendarClassName,
|
|
|
|
scopedSlots: $scopedSlots,
|
|
|
|
})
|
|
|
|
const calendar = (
|
|
|
|
<RangeCalendar
|
|
|
|
{...rangeCalendarProps}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
// default width for showTime
|
|
|
|
const pickerStyle = {}
|
|
|
|
if (props.showTime) {
|
|
|
|
pickerStyle.width = '350px'
|
|
|
|
}
|
2019-01-02 20:20:02 +08:00
|
|
|
const [startValue, endValue] = value
|
|
|
|
const clearIcon = (!props.disabled && props.allowClear && value && (startValue || endValue)) ? (
|
2018-03-17 21:38:29 +08:00
|
|
|
<Icon
|
2018-12-08 16:47:05 +08:00
|
|
|
type='close-circle'
|
2018-03-17 21:38:29 +08:00
|
|
|
class={`${prefixCls}-picker-clear`}
|
|
|
|
onClick={this.clearSelection}
|
2018-12-08 16:47:05 +08:00
|
|
|
theme='filled'
|
2018-03-17 21:38:29 +08:00
|
|
|
/>
|
|
|
|
) : null
|
|
|
|
|
2018-12-08 16:47:05 +08:00
|
|
|
const inputIcon = suffixIcon && (
|
|
|
|
isValidElement(suffixIcon)
|
|
|
|
? cloneElement(
|
|
|
|
suffixIcon,
|
|
|
|
{
|
|
|
|
class: `${prefixCls}-picker-icon`,
|
|
|
|
},
|
|
|
|
) : <span class={`${prefixCls}-picker-icon`}>{suffixIcon}</span>) || (
|
|
|
|
<Icon type='calendar' class={`${prefixCls}-picker-icon`} />
|
|
|
|
)
|
|
|
|
|
2018-03-17 21:38:29 +08:00
|
|
|
const input = ({ value: inputValue }) => {
|
2019-01-02 20:20:02 +08:00
|
|
|
const [start, end] = inputValue
|
2018-03-17 21:38:29 +08:00
|
|
|
return (
|
|
|
|
<span class={props.pickerInputClass}>
|
|
|
|
<input
|
|
|
|
disabled={props.disabled}
|
|
|
|
readOnly
|
|
|
|
value={(start && start.format(props.format)) || ''}
|
|
|
|
placeholder={startPlaceholder}
|
|
|
|
class={`${prefixCls}-range-picker-input`}
|
|
|
|
tabIndex={-1}
|
|
|
|
/>
|
|
|
|
<span class={`${prefixCls}-range-picker-separator`}> ~ </span>
|
|
|
|
<input
|
|
|
|
disabled={props.disabled}
|
|
|
|
readOnly
|
|
|
|
value={(end && end.format(props.format)) || ''}
|
|
|
|
placeholder={endPlaceholder}
|
|
|
|
class={`${prefixCls}-range-picker-input`}
|
|
|
|
tabIndex={-1}
|
|
|
|
/>
|
|
|
|
{clearIcon}
|
2018-12-08 16:47:05 +08:00
|
|
|
{inputIcon}
|
2018-03-17 21:38:29 +08:00
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const vcDatePickerProps = mergeProps({
|
|
|
|
props,
|
|
|
|
on: $listeners,
|
|
|
|
}, pickerChangeHandler, {
|
|
|
|
props: {
|
|
|
|
calendar: calendar,
|
|
|
|
value: value,
|
|
|
|
open: open,
|
|
|
|
prefixCls: `${prefixCls}-picker-container`,
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
openChange: this.handleOpenChange,
|
|
|
|
},
|
|
|
|
style: popupStyle,
|
|
|
|
})
|
|
|
|
return (
|
|
|
|
<span
|
|
|
|
ref='picker'
|
|
|
|
class={props.pickerClass}
|
|
|
|
style={pickerStyle}
|
|
|
|
tabIndex={props.disabled ? -1 : 0}
|
|
|
|
onFocus={focus}
|
|
|
|
onBlur={blur}
|
2018-09-05 21:28:54 +08:00
|
|
|
onMouseenter={this.onMouseEnter}
|
|
|
|
onMouseleave={this.onMouseLeave}
|
2018-03-17 21:38:29 +08:00
|
|
|
>
|
|
|
|
<VcDatePicker
|
|
|
|
{...vcDatePickerProps}
|
|
|
|
>
|
|
|
|
{input}
|
|
|
|
</VcDatePicker>
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
2018-03-19 10:16:27 +08:00
|
|
|
|