ant-design-vue/components/calendar/index.jsx

249 lines
6.5 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin';
import { getOptionProps, hasProp, initDefaultProps } from '../_util/props-util';
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';
import interopDefault from '../_util/interopDefault';
import enUS from './locale/en_US';
export { HeaderProps } from './Header';
function noop() {
return null;
}
2018-03-19 02:16:27 +00:00
2019-01-12 03:33:27 +00:00
function zerofixed(v) {
2018-03-13 14:40:13 +00:00
if (v < 10) {
2019-01-12 03:33:27 +00:00
return `0${v}`;
2018-03-13 14:40:13 +00:00
}
2019-01-12 03:33:27 +00:00
return `${v}`;
2018-03-13 14:40:13 +00:00
}
export const MomentType = {
type: Object,
2019-01-12 03:33:27 +00:00
validator: function(value) {
return moment.isMoment(value);
2018-03-13 14:40:13 +00:00
},
2019-01-12 03:33:27 +00:00
};
function isMomentArray(value) {
return Array.isArray(value) && !!value.find(val => moment.isMoment(val));
2018-03-13 14:40:13 +00:00
}
2019-01-12 03:33:27 +00:00
export const CalendarMode = PropTypes.oneOf(['month', 'year']);
2018-03-13 14:40:13 +00:00
export const CalendarProps = () => ({
prefixCls: PropTypes.string,
value: MomentType,
defaultValue: MomentType,
mode: CalendarMode,
fullscreen: PropTypes.bool,
2018-03-14 14:00:43 +00:00
// dateCellRender: PropTypes.func,
// monthCellRender: PropTypes.func,
// dateFullCellRender: PropTypes.func,
// monthFullCellRender: PropTypes.func,
locale: PropTypes.object,
2018-03-13 14:40:13 +00:00
// onPanelChange?: (date?: moment.Moment, mode?: CalendarMode) => void;
// onSelect?: (date?: moment.Moment) => void;
disabledDate: PropTypes.func,
2018-04-06 12:56:19 +00:00
validRange: PropTypes.custom(isMomentArray),
2019-01-12 03:33:27 +00:00
});
2018-03-13 14:40:13 +00:00
const Calendar = {
2018-04-08 13:17:20 +00:00
name: 'ACalendar',
2018-03-13 14:40:13 +00:00
mixins: [BaseMixin],
props: initDefaultProps(CalendarProps(), {
locale: {},
fullscreen: true,
prefixCls: PREFIX_CLS,
mode: 'month',
}),
2018-03-14 14:00:43 +00:00
model: {
prop: 'value',
event: 'change',
},
2019-01-12 03:33:27 +00:00
data() {
const value = this.value || this.defaultValue || interopDefault(moment)();
2018-04-06 12:56:19 +00:00
if (!interopDefault(moment).isMoment(value)) {
2019-01-12 03:33:27 +00:00
throw new Error('The value/defaultValue of Calendar must be a moment object, ');
2018-03-13 14:40:13 +00:00
}
return {
sValue: value,
sMode: this.mode,
2019-01-12 03:33:27 +00:00
};
2018-03-13 14:40:13 +00:00
},
watch: {
2019-01-12 03:33:27 +00:00
value(val) {
2018-03-13 14:40:13 +00:00
this.setState({
sValue: val,
2019-01-12 03:33:27 +00:00
});
2018-03-13 14:40:13 +00:00
},
2019-01-12 03:33:27 +00:00
mode(val) {
2018-03-13 14:40:13 +00:00
this.setState({
sMode: val,
2019-01-12 03:33:27 +00:00
});
2018-03-13 14:40:13 +00:00
},
},
methods: {
2019-01-12 03:33:27 +00:00
monthCellRender2(value) {
const { prefixCls, $scopedSlots } = this;
const monthCellRender = this.monthCellRender || $scopedSlots.monthCellRender || noop;
2018-03-13 14:40:13 +00:00
return (
<div class={`${prefixCls}-month`}>
2019-01-12 03:33:27 +00:00
<div class={`${prefixCls}-value`}>{value.localeData().monthsShort(value)}</div>
<div class={`${prefixCls}-content`}>{monthCellRender(value)}</div>
2018-03-13 14:40:13 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-03-13 14:40:13 +00:00
},
2019-01-12 03:33:27 +00:00
dateCellRender2(value) {
const { prefixCls, $scopedSlots } = this;
const dateCellRender = this.dateCellRender || $scopedSlots.dateCellRender || noop;
2018-03-13 14:40:13 +00:00
return (
<div class={`${prefixCls}-date`}>
2019-01-12 03:33:27 +00:00
<div class={`${prefixCls}-value`}>{zerofixed(value.date())}</div>
<div class={`${prefixCls}-content`}>{dateCellRender(value)}</div>
2018-03-13 14:40:13 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-03-13 14:40:13 +00:00
},
2019-01-12 03:33:27 +00:00
setValue(value, way) {
2018-03-13 14:40:13 +00:00
if (way === 'select') {
2019-01-12 03:33:27 +00:00
this.$emit('select', value);
2018-03-13 14:40:13 +00:00
} else if (way === 'changePanel') {
2019-01-12 03:33:27 +00:00
this.onPanelChange(value, this.sMode);
2018-03-13 14:40:13 +00:00
}
if (!hasProp(this, 'value')) {
2019-01-12 03:33:27 +00:00
this.setState({ sValue: value });
}
2018-03-13 14:40:13 +00:00
},
2019-01-12 03:33:27 +00:00
setType(type) {
const mode = type === 'date' ? 'month' : 'year';
2018-03-13 14:40:13 +00:00
if (this.sMode !== mode) {
2019-01-12 03:33:27 +00:00
this.setState({ sMode: mode });
this.onPanelChange(this.sValue, mode);
2018-03-13 14:40:13 +00:00
}
},
2019-01-12 03:33:27 +00:00
onHeaderValueChange(value) {
this.setValue(value, 'changePanel');
2018-03-13 14:40:13 +00:00
},
2019-01-12 03:33:27 +00:00
onHeaderTypeChange(type) {
this.setType(type);
2018-03-13 14:40:13 +00:00
},
2019-01-12 03:33:27 +00:00
onPanelChange(value, mode) {
this.$emit('panelChange', value, mode);
2018-03-14 14:00:43 +00:00
if (value !== this.sValue) {
2019-01-12 03:33:27 +00:00
this.$emit('change', value);
2018-03-14 14:00:43 +00:00
}
2018-03-13 14:40:13 +00:00
},
2019-01-12 03:33:27 +00:00
onSelect(value) {
this.setValue(value, 'select');
2018-03-13 14:40:13 +00:00
},
2019-01-12 03:33:27 +00:00
getDateRange(validRange, disabledDate) {
return current => {
2018-04-06 12:56:19 +00:00
if (!current) {
2019-01-12 03:33:27 +00:00
return false;
2018-04-06 12:56:19 +00:00
}
2019-01-12 03:33:27 +00:00
const [startDate, endDate] = validRange;
const inRange = !current.isBetween(startDate, endDate, 'days', '[]');
2018-04-06 12:56:19 +00:00
if (disabledDate) {
2019-01-12 03:33:27 +00:00
return disabledDate(current) || inRange;
2018-04-06 12:56:19 +00:00
}
2019-01-12 03:33:27 +00:00
return inRange;
};
2018-04-06 12:56:19 +00:00
},
2018-03-13 14:40:13 +00:00
2019-01-12 03:33:27 +00:00
renderCalendar(locale, localeCode) {
const props = getOptionProps(this);
const { sValue: value, sMode: mode, $listeners, $scopedSlots } = this;
2018-03-13 14:40:13 +00:00
if (value && localeCode) {
2019-01-12 03:33:27 +00:00
value.locale(localeCode);
2018-03-13 14:40:13 +00:00
}
2019-01-12 03:33:27 +00:00
const { prefixCls, fullscreen, dateFullCellRender, monthFullCellRender } = props;
const type = mode === 'year' ? 'month' : 'date';
2018-03-13 14:40:13 +00:00
2019-01-12 03:33:27 +00:00
let cls = '';
2018-03-13 14:40:13 +00:00
if (fullscreen) {
2019-01-12 03:33:27 +00:00
cls += ` ${prefixCls}-fullscreen`;
2018-03-13 14:40:13 +00:00
}
2019-01-12 03:33:27 +00:00
const monthCellRender =
monthFullCellRender || $scopedSlots.monthFullCellRender || this.monthCellRender2;
const dateCellRender =
dateFullCellRender || $scopedSlots.dateFullCellRender || this.dateCellRender2;
2018-04-06 12:56:19 +00:00
2019-01-12 03:33:27 +00:00
let disabledDate = props.disabledDate;
2018-04-06 12:56:19 +00:00
if (props.validRange) {
2019-01-12 03:33:27 +00:00
disabledDate = this.getDateRange(props.validRange, disabledDate);
2018-04-06 12:56:19 +00:00
}
2018-03-13 14:40:13 +00:00
const fullCalendarProps = {
props: {
...props,
2018-03-14 14:00:43 +00:00
Select: {},
2018-03-13 14:40:13 +00:00
locale: locale.lang,
type: type,
prefixCls: prefixCls,
showHeader: false,
value: value,
monthCellRender: monthCellRender,
dateCellRender: dateCellRender,
2018-04-06 12:56:19 +00:00
disabledDate,
2018-03-13 14:40:13 +00:00
},
on: {
...$listeners,
select: this.onSelect,
},
2019-01-12 03:33:27 +00:00
};
2018-03-13 14:40:13 +00:00
return (
<div class={cls}>
<Header
fullscreen={fullscreen}
type={type}
value={value}
locale={locale.lang}
prefixCls={prefixCls}
onTypeChange={this.onHeaderTypeChange}
onValueChange={this.onHeaderValueChange}
2018-04-06 12:56:19 +00:00
validRange={props.validRange}
2018-03-13 14:40:13 +00:00
/>
2019-01-12 03:33:27 +00:00
<FullCalendar {...fullCalendarProps} />
2018-03-13 14:40:13 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-03-13 14:40:13 +00:00
},
2019-01-12 03:33:27 +00:00
getDefaultLocale() {
const result = {
...enUS,
...this.$props.locale,
2019-01-12 03:33:27 +00:00
};
result.lang = {
...result.lang,
...(this.$props.locale || {}).lang,
2019-01-12 03:33:27 +00:00
};
return result;
},
2018-03-13 14:40:13 +00:00
},
2019-01-12 03:33:27 +00:00
render() {
2018-03-13 14:40:13 +00:00
return (
<LocaleReceiver
2019-01-12 03:33:27 +00:00
componentName="Calendar"
defaultLocale={this.getDefaultLocale}
2019-01-12 03:33:27 +00:00
scopedSlots={{ default: this.renderCalendar }}
2018-03-13 14:40:13 +00:00
/>
2019-01-12 03:33:27 +00:00
);
2018-03-13 14:40:13 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-03-19 02:16:27 +00:00
/* istanbul ignore next */
2019-01-12 03:33:27 +00:00
Calendar.install = function(Vue) {
Vue.component(Calendar.name, Calendar);
};
2019-01-12 03:33:27 +00:00
export default Calendar;