You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/calendar/index.jsx

245 lines
6.3 KiB

import PropTypes from '../_util/vue-types'
import BaseMixin from '../_util/BaseMixin'
import { getOptionProps, hasProp, initDefaultProps } from '../_util/props-util'
7 years ago
import * as moment from 'moment'
import FullCalendar from '../vc-calendar/src/FullCalendar'
import LocaleReceiver from '../locale-provider/LocaleReceiver'
import { PREFIX_CLS } from './Constants'
import Header from './Header'
7 years ago
import interopDefault from '../_util/interopDefault'
7 years ago
import enUS from './locale/en_US'
export { HeaderProps } from './Header'
function noop () { return null }
function zerofixed (v) {
if (v < 10) {
return `0${v}`
}
return `${v}`
}
export const MomentType = {
type: Object,
validator: function (value) {
return moment.isMoment(value)
},
}
7 years ago
function isMomentArray (value) {
return Array.isArray(value) && !!value.find((val) => moment.isMoment(val))
}
7 years ago
export const CalendarMode = PropTypes.oneOf(['month', 'year'])
export const CalendarProps = () => ({
prefixCls: PropTypes.string,
value: MomentType,
defaultValue: MomentType,
mode: CalendarMode,
fullscreen: PropTypes.bool,
7 years ago
// dateCellRender: PropTypes.func,
// monthCellRender: PropTypes.func,
// dateFullCellRender: PropTypes.func,
// monthFullCellRender: PropTypes.func,
7 years ago
locale: PropTypes.any,
// onPanelChange?: (date?: moment.Moment, mode?: CalendarMode) => void;
// onSelect?: (date?: moment.Moment) => void;
disabledDate: PropTypes.func,
7 years ago
validRange: PropTypes.custom(isMomentArray),
7 years ago
})
export default {
name: 'ACalendar',
7 years ago
mixins: [BaseMixin],
props: initDefaultProps(CalendarProps(), {
locale: {},
fullscreen: true,
prefixCls: PREFIX_CLS,
mode: 'month',
}),
7 years ago
model: {
prop: 'value',
event: 'change',
},
7 years ago
data () {
7 years ago
const value = this.value || this.defaultValue || interopDefault(moment)()
if (!interopDefault(moment).isMoment(value)) {
7 years ago
throw new Error(
'The value/defaultValue of Calendar must be a moment object, ',
)
}
return {
sValue: value,
sMode: this.mode,
}
},
watch: {
value (val) {
this.setState({
sValue: val,
})
},
mode (val) {
this.setState({
sMode: val,
})
},
},
methods: {
7 years ago
monthCellRender2 (value) {
const { prefixCls, $scopedSlots } = this
const monthCellRender = this.monthCellRender || $scopedSlots.monthCellRender || noop
7 years ago
return (
<div class={`${prefixCls}-month`}>
<div class={`${prefixCls}-value`}>
{value.localeData().monthsShort(value)}
</div>
<div class={`${prefixCls}-content`}>
{monthCellRender(value)}
</div>
</div>
)
},
7 years ago
dateCellRender2 (value) {
const { prefixCls, $scopedSlots } = this
const dateCellRender = this.dateCellRender || $scopedSlots.dateCellRender || noop
7 years ago
return (
<div class={`${prefixCls}-date`}>
<div class={`${prefixCls}-value`}>
{zerofixed(value.date())}
</div>
<div class={`${prefixCls}-content`}>
{dateCellRender(value)}
</div>
</div>
)
},
setValue (value, way) {
if (!hasProp(this, 'value')) {
this.setState({ sValue: value })
}
if (way === 'select') {
this.$emit('select', value)
7 years ago
this.$emit('change', value)
7 years ago
} else if (way === 'changePanel') {
7 years ago
this.onPanelChange(value, this.sMode)
7 years ago
}
},
setType (type) {
const mode = (type === 'date') ? 'month' : 'year'
if (this.sMode !== mode) {
this.setState({ sMode: mode })
7 years ago
this.onPanelChange(this.sValue, mode)
7 years ago
}
},
onHeaderValueChange (value) {
this.setValue(value, 'changePanel')
},
onHeaderTypeChange (type) {
this.setType(type)
},
onPanelChange (value, mode) {
this.$emit('panelChange', value, mode)
7 years ago
if (value !== this.sValue) {
this.$emit('change', value)
}
7 years ago
},
onSelect (value) {
this.setValue(value, 'select')
},
7 years ago
getDateRange (
validRange,
disabledDate,
) {
return (current) => {
if (!current) {
return false
}
const [startDate, endDate] = validRange
const inRange = !current.isBetween(startDate, endDate, 'days', '[]')
if (disabledDate) {
return (disabledDate(current) || inRange)
}
return inRange
}
},
7 years ago
renderCalendar (locale, localeCode) {
const props = getOptionProps(this)
7 years ago
const { sValue: value, sMode: mode, $listeners, $scopedSlots } = this
7 years ago
if (value && localeCode) {
value.locale(localeCode)
}
const { prefixCls, fullscreen, dateFullCellRender, monthFullCellRender } = props
const type = (mode === 'year') ? 'month' : 'date'
let cls = ''
if (fullscreen) {
cls += (` ${prefixCls}-fullscreen`)
}
7 years ago
const monthCellRender = monthFullCellRender || $scopedSlots.monthFullCellRender || this.monthCellRender2
const dateCellRender = dateFullCellRender || $scopedSlots.dateFullCellRender || this.dateCellRender2
7 years ago
let disabledDate = props.disabledDate
if (props.validRange) {
disabledDate = this.getDateRange(props.validRange, disabledDate)
}
7 years ago
const fullCalendarProps = {
props: {
...props,
7 years ago
Select: {},
7 years ago
locale: locale.lang,
type: type,
prefixCls: prefixCls,
showHeader: false,
value: value,
monthCellRender: monthCellRender,
dateCellRender: dateCellRender,
7 years ago
disabledDate,
7 years ago
},
on: {
...$listeners,
select: this.onSelect,
},
}
return (
<div class={cls}>
<Header
fullscreen={fullscreen}
type={type}
value={value}
locale={locale.lang}
prefixCls={prefixCls}
onTypeChange={this.onHeaderTypeChange}
onValueChange={this.onHeaderValueChange}
7 years ago
validRange={props.validRange}
7 years ago
/>
<FullCalendar {...fullCalendarProps}/>
</div>
)
},
},
render () {
return (
<LocaleReceiver
componentName='Calendar'
defaultLocale={enUS}
7 years ago
scopedSlots={
{ default: this.renderCalendar }
}
7 years ago
/>
)
},
}