@wangxinowo/vue-datepicker-next 1.0.2 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,264 +1,277 @@
1
- import PropTypes from '../../../_util/vue-types';
2
- import BaseMixin from '../../../_util/BaseMixin';
3
- import { getOptionProps, getListeners } from '../../../_util/props-util';
4
- import MonthPanel from '../month/MonthPanel';
5
- import YearPanel from '../year/YearPanel';
6
- import DecadePanel from '../decade/DecadePanel';
7
- function noop() {}
8
- function goMonth(direction) {
9
- const next = this.value.clone();
10
- next.add(direction, 'months');
11
- this.__emit('valueChange', next);
12
- }
13
-
14
- function goYear(direction) {
15
- const next = this.value.clone();
16
- next.add(direction, 'years');
17
- this.__emit('valueChange', next);
18
- }
19
-
20
- function showIf(condition, el) {
21
- return condition ? el : null;
22
- }
23
-
24
- const CalendarHeader = {
25
- name: 'CalendarHeader',
26
- mixins: [BaseMixin],
27
- props: {
28
- prefixCls: PropTypes.string,
29
- value: PropTypes.object,
30
- // onValueChange: PropTypes.func,
31
- showTimePicker: PropTypes.bool,
32
- // onPanelChange: PropTypes.func,
33
- locale: PropTypes.object,
34
- enablePrev: PropTypes.any.def(1),
35
- enableNext: PropTypes.any.def(1),
36
- disabledMonth: PropTypes.func,
37
- mode: PropTypes.any,
38
- selectedValue: PropTypes.object,
39
- monthCellRender: PropTypes.func,
40
- monthCellContentRender: PropTypes.func,
41
- renderFooter: PropTypes.func,
42
- },
43
- data() {
44
- this.nextMonth = goMonth.bind(this, 1);
45
- this.previousMonth = goMonth.bind(this, -1);
46
- this.nextYear = goYear.bind(this, 1);
47
- this.previousYear = goYear.bind(this, -1);
48
- return {
49
- yearPanelReferer: null,
50
- };
51
- },
52
- methods: {
53
- onMonthSelect(value) {
54
- this.__emit('panelChange', value, 'date');
55
- if (getListeners(this).monthSelect) {
56
- this.__emit('monthSelect', value);
57
- } else {
58
- // 只有当当前不是月份选择器模式时才触发 valueChange
59
- // mode === 'month' 时,表示用户在月份选择器模式下直接选择月份
60
- // 此时不需要触发 valueChange,因为选择已通过 panelChange 处理
61
- if (this.mode !== 'month') {
62
- this.__emit('valueChange', value);
63
- }
64
- }
65
- },
66
-
67
- onYearSelect(value) {
68
- const referer = this.yearPanelReferer;
69
- this.setState({ yearPanelReferer: null });
70
- this.__emit('panelChange', value, referer);
71
- // 只有当从其他面板进入年份选择时才触发 valueChange
72
- // 当 referer 为 null 时,表示用户在年份选择器模式下直接选择年份
73
- // 此时不需要触发 valueChange,因为选择已通过 panelChange 处理
74
- // 触发 valueChange 会导致面板显示值被重置
75
- if (referer) {
76
- this.__emit('valueChange', value);
77
- }
78
- },
79
-
80
- onDecadeSelect(value) {
81
- this.__emit('panelChange', value, 'year');
82
- this.__emit('valueChange', value);
83
- },
84
-
85
- // 处理年份面板的十年导航变化
86
- onYearValueChange(value) {
87
- this.__emit('valueChange', value);
88
- },
89
-
90
- changeYear(direction) {
91
- if (direction > 0) {
92
- this.nextYear();
93
- } else {
94
- this.previousYear();
95
- }
96
- },
97
-
98
- monthYearElement(showTimePicker) {
99
- const props = this.$props;
100
- const prefixCls = props.prefixCls;
101
- const locale = props.locale;
102
- const value = props.value;
103
- const localeData = value.localeData();
104
- const monthBeforeYear = locale.monthBeforeYear;
105
- const selectClassName = `${prefixCls}-${monthBeforeYear ? 'my-select' : 'ym-select'}`;
106
- const timeClassName = showTimePicker ? ` ${prefixCls}-time-status` : '';
107
- const year = (
108
- <a
109
- class={`${prefixCls}-year-select${timeClassName}`}
110
- role="button"
111
- onClick={showTimePicker ? noop : () => this.showYearPanel('date')}
112
- title={showTimePicker ? null : locale.yearSelect}
113
- >
114
- {value.format(locale.yearFormat)}
115
- </a>
116
- );
117
- const month = (
118
- <a
119
- class={`${prefixCls}-month-select${timeClassName}`}
120
- role="button"
121
- onClick={showTimePicker ? noop : this.showMonthPanel}
122
- title={showTimePicker ? null : locale.monthSelect}
123
- >
124
- {locale.monthFormat ? value.format(locale.monthFormat) : localeData.monthsShort(value)}
125
- </a>
126
- );
127
- let day;
128
- if (showTimePicker) {
129
- day = (
130
- <a class={`${prefixCls}-day-select${timeClassName}`} role="button">
131
- {value.format(locale.dayFormat)}
132
- </a>
133
- );
134
- }
135
- let my = [];
136
- if (monthBeforeYear) {
137
- my = [month, day, year];
138
- } else {
139
- my = [year, month, day];
140
- }
141
- return <span class={selectClassName}>{my}</span>;
142
- },
143
-
144
- showMonthPanel() {
145
- // null means that users' interaction doesn't change value
146
- this.__emit('panelChange', null, 'month');
147
- },
148
-
149
- showYearPanel(referer) {
150
- this.setState({ yearPanelReferer: referer });
151
- this.__emit('panelChange', null, 'year');
152
- },
153
-
154
- showDecadePanel() {
155
- this.__emit('panelChange', null, 'decade');
156
- },
157
- },
158
-
159
- render() {
160
- const props = getOptionProps(this);
161
- const {
162
- prefixCls,
163
- locale,
164
- mode,
165
- value,
166
- showTimePicker,
167
- enableNext,
168
- enablePrev,
169
- disabledMonth,
170
- renderFooter,
171
- selectedValue,
172
- } = props;
173
-
174
- let panel = null;
175
- if (mode === 'month') {
176
- panel = (
177
- <MonthPanel
178
- locale={locale}
179
- value={value}
180
- selectedValue={selectedValue}
181
- rootPrefixCls={prefixCls}
182
- onSelect={this.onMonthSelect}
183
- onYearPanelShow={() => this.showYearPanel('month')}
184
- disabledDate={disabledMonth}
185
- cellRender={props.monthCellRender}
186
- contentRender={props.monthCellContentRender}
187
- renderFooter={renderFooter}
188
- changeYear={this.changeYear}
189
- />
190
- );
191
- }
192
- if (mode === 'year') {
193
- panel = (
194
- <YearPanel
195
- locale={locale}
196
- value={value}
197
- selectedValue={selectedValue}
198
- rootPrefixCls={prefixCls}
199
- onSelect={this.onYearSelect}
200
- onValueChange={this.onYearValueChange}
201
- onDecadePanelShow={this.showDecadePanel}
202
- renderFooter={renderFooter}
203
- disabledDate={disabledMonth}
204
- />
205
- );
206
- }
207
- if (mode === 'decade') {
208
- panel = (
209
- <DecadePanel
210
- locale={locale}
211
- value={value}
212
- rootPrefixCls={prefixCls}
213
- onSelect={this.onDecadeSelect}
214
- renderFooter={renderFooter}
215
- />
216
- );
217
- }
218
-
219
- return (
220
- <div class={`${prefixCls}-header`}>
221
- <div style={{ position: 'relative' }}>
222
- {showIf(
223
- enablePrev && !showTimePicker,
224
- <a
225
- class={`${prefixCls}-prev-year-btn`}
226
- role="button"
227
- onClick={this.previousYear}
228
- title={locale.previousYear}
229
- />,
230
- )}
231
- {showIf(
232
- enablePrev && !showTimePicker,
233
- <a
234
- class={`${prefixCls}-prev-month-btn`}
235
- role="button"
236
- onClick={this.previousMonth}
237
- title={locale.previousMonth}
238
- />,
239
- )}
240
- {this.monthYearElement(showTimePicker)}
241
- {showIf(
242
- enableNext && !showTimePicker,
243
- <a
244
- class={`${prefixCls}-next-month-btn`}
245
- onClick={this.nextMonth}
246
- title={locale.nextMonth}
247
- />,
248
- )}
249
- {showIf(
250
- enableNext && !showTimePicker,
251
- <a
252
- class={`${prefixCls}-next-year-btn`}
253
- onClick={this.nextYear}
254
- title={locale.nextYear}
255
- />,
256
- )}
257
- </div>
258
- {panel}
259
- </div>
260
- );
261
- },
262
- };
263
-
264
- export default CalendarHeader;
1
+ import PropTypes from '../../../_util/vue-types';
2
+ import BaseMixin from '../../../_util/BaseMixin';
3
+ import { getOptionProps, getListeners } from '../../../_util/props-util';
4
+ import MonthPanel from '../month/MonthPanel';
5
+ import YearPanel from '../year/YearPanel';
6
+ import DecadePanel from '../decade/DecadePanel';
7
+ function noop() {}
8
+ function goMonth(direction) {
9
+ const next = this.value.clone();
10
+ next.add(direction, 'months');
11
+ this.__emit('valueChange', next);
12
+ }
13
+
14
+ function goYear(direction) {
15
+ const next = this.value.clone();
16
+ next.add(direction, 'years');
17
+ this.__emit('valueChange', next);
18
+ }
19
+
20
+ function showIf(condition, el) {
21
+ return condition ? el : null;
22
+ }
23
+
24
+ const CalendarHeader = {
25
+ name: 'CalendarHeader',
26
+ mixins: [BaseMixin],
27
+ props: {
28
+ prefixCls: PropTypes.string,
29
+ value: PropTypes.object,
30
+ // onValueChange: PropTypes.func,
31
+ showTimePicker: PropTypes.bool,
32
+ // onPanelChange: PropTypes.func,
33
+ locale: PropTypes.object,
34
+ enablePrev: PropTypes.any.def(1),
35
+ enableNext: PropTypes.any.def(1),
36
+ disabledMonth: PropTypes.func,
37
+ mode: PropTypes.any,
38
+ // 支持数组形式,用于范围选择器
39
+ selectedValue: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
40
+ // hover 值,用于范围选择时的预览效果
41
+ hoverValue: PropTypes.array.def([]),
42
+ monthCellRender: PropTypes.func,
43
+ monthCellContentRender: PropTypes.func,
44
+ renderFooter: PropTypes.func,
45
+ // 用于范围选择器,标识是左面板还是右面板
46
+ direction: PropTypes.string,
47
+ },
48
+ data() {
49
+ this.nextMonth = goMonth.bind(this, 1);
50
+ this.previousMonth = goMonth.bind(this, -1);
51
+ this.nextYear = goYear.bind(this, 1);
52
+ this.previousYear = goYear.bind(this, -1);
53
+ return {
54
+ yearPanelReferer: null,
55
+ };
56
+ },
57
+ methods: {
58
+ onMonthSelect(value) {
59
+ this.__emit('panelChange', value, 'date');
60
+ if (getListeners(this).monthSelect) {
61
+ this.__emit('monthSelect', value);
62
+ } else {
63
+ // 只有当当前不是月份选择器模式时才触发 valueChange
64
+ // 当 mode === 'month' 时,表示用户在月份选择器模式下直接选择月份
65
+ // 此时不需要触发 valueChange,因为选择已通过 panelChange 处理
66
+ if (this.mode !== 'month') {
67
+ this.__emit('valueChange', value);
68
+ }
69
+ }
70
+ },
71
+
72
+ onYearSelect(value) {
73
+ const referer = this.yearPanelReferer;
74
+ this.setState({ yearPanelReferer: null });
75
+ this.__emit('panelChange', value, referer);
76
+ // 只有当从其他面板进入年份选择时才触发 valueChange
77
+ // 当 referer 为 null 时,表示用户在年份选择器模式下直接选择年份
78
+ // 此时不需要触发 valueChange,因为选择已通过 panelChange 处理
79
+ // 触发 valueChange 会导致面板显示值被重置
80
+ if (referer) {
81
+ this.__emit('valueChange', value);
82
+ }
83
+ },
84
+
85
+ onDecadeSelect(value) {
86
+ this.__emit('panelChange', value, 'year');
87
+ this.__emit('valueChange', value);
88
+ },
89
+
90
+ // 处理年份面板的十年导航变化
91
+ onYearValueChange(value) {
92
+ this.__emit('valueChange', value);
93
+ },
94
+
95
+ changeYear(direction) {
96
+ if (direction > 0) {
97
+ this.nextYear();
98
+ } else {
99
+ this.previousYear();
100
+ }
101
+ },
102
+
103
+ monthYearElement(showTimePicker) {
104
+ const props = this.$props;
105
+ const prefixCls = props.prefixCls;
106
+ const locale = props.locale;
107
+ const value = props.value;
108
+ const localeData = value.localeData();
109
+ const monthBeforeYear = locale.monthBeforeYear;
110
+ const selectClassName = `${prefixCls}-${monthBeforeYear ? 'my-select' : 'ym-select'}`;
111
+ const timeClassName = showTimePicker ? ` ${prefixCls}-time-status` : '';
112
+ const year = (
113
+ <a
114
+ class={`${prefixCls}-year-select${timeClassName}`}
115
+ role="button"
116
+ onClick={showTimePicker ? noop : () => this.showYearPanel('date')}
117
+ title={showTimePicker ? null : locale.yearSelect}
118
+ >
119
+ {value.format(locale.yearFormat)}
120
+ </a>
121
+ );
122
+ const month = (
123
+ <a
124
+ class={`${prefixCls}-month-select${timeClassName}`}
125
+ role="button"
126
+ onClick={showTimePicker ? noop : this.showMonthPanel}
127
+ title={showTimePicker ? null : locale.monthSelect}
128
+ >
129
+ {locale.monthFormat ? value.format(locale.monthFormat) : localeData.monthsShort(value)}
130
+ </a>
131
+ );
132
+ let day;
133
+ if (showTimePicker) {
134
+ day = (
135
+ <a class={`${prefixCls}-day-select${timeClassName}`} role="button">
136
+ {value.format(locale.dayFormat)}
137
+ </a>
138
+ );
139
+ }
140
+ let my = [];
141
+ if (monthBeforeYear) {
142
+ my = [month, day, year];
143
+ } else {
144
+ my = [year, month, day];
145
+ }
146
+ return <span class={selectClassName}>{my}</span>;
147
+ },
148
+
149
+ showMonthPanel() {
150
+ // null means that users' interaction doesn't change value
151
+ this.__emit('panelChange', null, 'month');
152
+ },
153
+
154
+ showYearPanel(referer) {
155
+ this.setState({ yearPanelReferer: referer });
156
+ this.__emit('panelChange', null, 'year');
157
+ },
158
+
159
+ showDecadePanel() {
160
+ this.__emit('panelChange', null, 'decade');
161
+ },
162
+ },
163
+
164
+ render() {
165
+ const props = getOptionProps(this);
166
+ const {
167
+ prefixCls,
168
+ locale,
169
+ mode,
170
+ value,
171
+ showTimePicker,
172
+ enableNext,
173
+ enablePrev,
174
+ disabledMonth,
175
+ renderFooter,
176
+ selectedValue,
177
+ hoverValue,
178
+ } = props;
179
+
180
+ const yearHover = getListeners(this).yearHover || noop;
181
+ const monthHover = getListeners(this).monthHover || noop;
182
+
183
+ let panel = null;
184
+ if (mode === 'month') {
185
+ panel = (
186
+ <MonthPanel
187
+ locale={locale}
188
+ value={value}
189
+ selectedValue={selectedValue}
190
+ hoverValue={hoverValue}
191
+ rootPrefixCls={prefixCls}
192
+ onSelect={this.onMonthSelect}
193
+ onYearPanelShow={() => this.showYearPanel('month')}
194
+ onMonthHover={monthHover}
195
+ disabledDate={disabledMonth}
196
+ cellRender={props.monthCellRender}
197
+ contentRender={props.monthCellContentRender}
198
+ renderFooter={renderFooter}
199
+ changeYear={this.changeYear}
200
+ />
201
+ );
202
+ }
203
+ if (mode === 'year') {
204
+ panel = (
205
+ <YearPanel
206
+ locale={locale}
207
+ value={value}
208
+ selectedValue={selectedValue}
209
+ hoverValue={hoverValue}
210
+ rootPrefixCls={prefixCls}
211
+ onSelect={this.onYearSelect}
212
+ onValueChange={this.onYearValueChange}
213
+ onDecadePanelShow={this.showDecadePanel}
214
+ onYearHover={yearHover}
215
+ renderFooter={renderFooter}
216
+ disabledDate={disabledMonth}
217
+ />
218
+ );
219
+ }
220
+ if (mode === 'decade') {
221
+ panel = (
222
+ <DecadePanel
223
+ locale={locale}
224
+ value={value}
225
+ rootPrefixCls={prefixCls}
226
+ onSelect={this.onDecadeSelect}
227
+ renderFooter={renderFooter}
228
+ />
229
+ );
230
+ }
231
+
232
+ return (
233
+ <div class={`${prefixCls}-header`}>
234
+ <div style={{ position: 'relative' }}>
235
+ {showIf(
236
+ enablePrev && !showTimePicker,
237
+ <a
238
+ class={`${prefixCls}-prev-year-btn`}
239
+ role="button"
240
+ onClick={this.previousYear}
241
+ title={locale.previousYear}
242
+ />,
243
+ )}
244
+ {showIf(
245
+ enablePrev && !showTimePicker,
246
+ <a
247
+ class={`${prefixCls}-prev-month-btn`}
248
+ role="button"
249
+ onClick={this.previousMonth}
250
+ title={locale.previousMonth}
251
+ />,
252
+ )}
253
+ {this.monthYearElement(showTimePicker)}
254
+ {showIf(
255
+ enableNext && !showTimePicker,
256
+ <a
257
+ class={`${prefixCls}-next-month-btn`}
258
+ onClick={this.nextMonth}
259
+ title={locale.nextMonth}
260
+ />,
261
+ )}
262
+ {showIf(
263
+ enableNext && !showTimePicker,
264
+ <a
265
+ class={`${prefixCls}-next-year-btn`}
266
+ onClick={this.nextYear}
267
+ title={locale.nextYear}
268
+ />,
269
+ )}
270
+ </div>
271
+ {panel}
272
+ </div>
273
+ );
274
+ },
275
+ };
276
+
277
+ export default CalendarHeader;