-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathindex.tsx
257 lines (241 loc) · 7.79 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { defineComponent, inject, PropType } from 'vue';
import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin';
import { getOptionProps, hasProp } from '../_util/props-util';
import moment from 'moment';
import FullCalendar from '../vc-calendar/src/FullCalendar';
import Header from './Header';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import interopDefault from '../_util/interopDefault';
import { defaultConfigProvider } from '../config-provider';
import enUS from './locale/en_US';
import { checkValidate, stringToMoment, momentToString, TimeType } from '../_util/moment-util';
import { tuple, withInstall } from '../_util/type';
function noop() {
return null;
}
function zerofixed(v: number) {
if (v < 10) {
return `0${v}`;
}
return `${v}`;
}
const CalendarModeTypes = tuple('month', 'year');
export type CalendarMode = typeof CalendarModeTypes[number];
export const CalendarProps = {
monthCellRender: PropTypes.func,
dateCellRender: PropTypes.func,
monthFullCellRender: PropTypes.func,
dateFullCellRender: PropTypes.func,
prefixCls: PropTypes.string,
value: TimeType,
defaultValue: TimeType,
mode: PropTypes.oneOf(CalendarModeTypes),
fullscreen: PropTypes.looseBool.def(true),
locale: PropTypes.object.def({}),
disabledDate: PropTypes.func,
validRange: {
type: Array as PropType<moment.Moment[]>,
},
headerRender: PropTypes.func,
valueFormat: PropTypes.string,
onPanelChange: PropTypes.func,
onSelect: PropTypes.func,
onChange: PropTypes.func,
'onUpdate:value': PropTypes.func,
};
const Calendar = defineComponent({
name: 'ACalendar',
mixins: [BaseMixin],
inheritAttrs: false,
props: CalendarProps,
setup() {
return {
configProvider: inject('configProvider', defaultConfigProvider),
sPrefixCls: undefined,
};
},
data() {
const { value, defaultValue, valueFormat } = this;
const sValue = value || defaultValue || interopDefault(moment)();
checkValidate('Calendar', defaultValue, 'defaultValue', valueFormat);
checkValidate('Calendar', value, 'value', valueFormat);
return {
sValue: stringToMoment(sValue, valueFormat),
sMode: this.mode || 'month',
};
},
watch: {
value(val) {
checkValidate('Calendar', val, 'value', this.valueFormat);
this.setState({
sValue: stringToMoment(val, this.valueFormat),
});
},
mode(val) {
this.setState({
sMode: val,
});
},
},
methods: {
onHeaderValueChange(value: moment.Moment) {
this.setValue(value, 'changePanel');
},
onHeaderTypeChange(mode: CalendarMode) {
this.sMode = mode;
this.triggerPanelChange(this.sValue, mode);
},
triggerPanelChange(value: moment.Moment, mode: CalendarMode | undefined) {
const val = this.valueFormat ? momentToString(value, this.valueFormat) : value;
if (value !== this.sValue) {
this.$emit('update:value', val);
this.$emit('change', val);
}
this.$emit('panelChange', val, mode);
},
triggerSelect(value: moment.Moment) {
this.setValue(value, 'select');
},
setValue(value: moment.Moment, way: 'select' | 'changePanel') {
const prevValue = this.value ? stringToMoment(this.value, this.valueFormat) : this.sValue;
const { sMode: mode, valueFormat } = this;
if (!hasProp(this, 'value')) {
this.setState({ sValue: value });
}
if (way === 'select') {
const val = valueFormat ? momentToString(value, valueFormat) : value;
if (prevValue && prevValue.month() !== value.month()) {
this.triggerPanelChange(value, mode);
} else {
this.$emit('update:value', val);
}
this.$emit('select', val);
} else if (way === 'changePanel') {
this.triggerPanelChange(value, mode);
}
},
getDateRange(
validRange: [moment.Moment, moment.Moment],
disabledDate?: (current: moment.Moment) => boolean,
) {
return (current: moment.Moment) => {
if (!current) {
return false;
}
const [startDate, endDate] = validRange;
const inRange = !current.isBetween(startDate, endDate, 'days', '[]');
if (disabledDate) {
return disabledDate(current) || inRange;
}
return inRange;
};
},
getDefaultLocale() {
const result = {
...enUS,
...this.$props.locale,
};
result.lang = {
...result.lang,
...(this.$props.locale || {}).lang,
};
return result;
},
monthCellRender2({ current: value }) {
const { sPrefixCls, $slots } = this;
const monthCellRender: Function = this.monthCellRender || $slots.monthCellRender || noop;
return (
<div class={`${sPrefixCls}-month`}>
<div class={`${sPrefixCls}-value`}>{value.localeData().monthsShort(value)}</div>
<div class={`${sPrefixCls}-content`}>{monthCellRender({ current: value })}</div>
</div>
);
},
dateCellRender2({ current: value }) {
const { sPrefixCls, $slots } = this;
const dateCellRender: Function = this.dateCellRender || $slots.dateCellRender || noop;
return (
<div class={`${sPrefixCls}-date`}>
<div class={`${sPrefixCls}-value`}>{zerofixed(value.date())}</div>
<div class={`${sPrefixCls}-content`}>{dateCellRender({ current: value })}</div>
</div>
);
},
renderCalendar(locale: any, localeCode: string) {
const props: any = { ...getOptionProps(this), ...this.$attrs };
const { sValue: value, sMode: mode, $slots } = this;
if (value && localeCode) {
value.locale(localeCode);
}
const {
prefixCls: customizePrefixCls,
fullscreen,
dateFullCellRender,
monthFullCellRender,
class: className,
style,
} = props;
const headerRender = this.headerRender || $slots.headerRender;
const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('fullcalendar', customizePrefixCls);
// To support old version react.
// Have to add prefixCls on the instance.
// https://github.com/facebook/react/issues/12397
this.sPrefixCls = prefixCls;
let cls = className || '';
if (fullscreen) {
cls += ` ${prefixCls}-fullscreen`;
}
const monthCellRender =
monthFullCellRender || $slots.monthFullCellRender || this.monthCellRender2;
const dateCellRender =
dateFullCellRender || $slots.dateFullCellRender || this.dateCellRender2;
let disabledDate = props.disabledDate;
if (props.validRange) {
disabledDate = this.getDateRange(props.validRange, disabledDate);
}
const fullCalendarProps = {
...props,
...this.$attrs,
Select: {},
locale: locale.lang,
type: mode === 'year' ? 'month' : 'date',
prefixCls,
showHeader: false,
value,
monthCellRender,
dateCellRender,
disabledDate,
onSelect: this.triggerSelect,
};
return (
<div class={cls} style={style}>
<Header
fullscreen={fullscreen}
type={mode}
headerRender={headerRender}
value={value}
locale={locale.lang}
prefixCls={prefixCls}
onTypeChange={this.onHeaderTypeChange}
onValueChange={this.onHeaderValueChange}
validRange={props.validRange}
/>
<FullCalendar {...fullCalendarProps} />
</div>
);
},
},
render() {
return (
<LocaleReceiver
componentName="Calendar"
defaultLocale={this.getDefaultLocale}
children={this.renderCalendar}
/>
);
},
});
export { HeaderProps } from './Header';
export default withInstall(Calendar);