-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathuseRange.ts
More file actions
168 lines (152 loc) · 5.5 KB
/
useRange.ts
File metadata and controls
168 lines (152 loc) · 5.5 KB
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
import { ref, computed, watchEffect } from '@vue/composition-api';
import dayjs from 'dayjs';
import { usePrefixClass, useConfig } from '../../hooks/useConfig';
import { TdDateRangePickerProps, DateValue } from '../type';
import { isValidDate, formatDate, getDefaultFormat } from '../../_common/js/date-picker/format';
import useRangeValue from './useRangeValue';
export const PARTIAL_MAP = { first: 'start', second: 'end' };
export default function useRange(props: TdDateRangePickerProps, { emit }: any) {
const COMPONENT_NAME = usePrefixClass('date-range-picker');
const { global } = useConfig('datePicker');
const isMountedRef = ref(false);
const inputRef = ref();
const {
value, onChange, time, month, year, cacheValue, isFirstValueSelected,
} = useRangeValue(props);
const formatRef = computed(() => getDefaultFormat({
mode: props.mode,
format: props.format,
valueType: props.valueType,
enableTimePicker: props.enableTimePicker,
}));
const popupVisible = ref(false);
const isHoverCell = ref(false);
const activeIndex = ref(0); // 确定当前选中的输入框序号
const inputValue = ref(
formatDate(props.value, { format: formatRef.value.format, targetFormat: formatRef.value.format }),
); // 未真正选中前可能不断变更输入框的内容
// input 设置
const rangeInputProps = computed(() => ({
...props.rangeInputProps,
ref: inputRef,
clearable: props.clearable,
prefixIcon: props.prefixIcon,
readonly: !props.allowInput,
separator: props.separator,
placeholder: props.placeholder || global.value.placeholder[props.mode],
activeIndex: popupVisible.value ? activeIndex.value : undefined,
class: {
[`${COMPONENT_NAME.value}__input--placeholder`]: isHoverCell.value,
},
onClick: ({ position }: any) => {
activeIndex.value = position === 'first' ? 0 : 1;
},
onClear: ({ e }: { e: MouseEvent }) => {
e.stopPropagation();
popupVisible.value = false;
onChange?.([], { dayjsValue: [], trigger: 'clear' });
emit('clear', [], { dayjsValue: [], trigger: 'clear' });
},
onBlur: (newVal: string[], { e, position }: any) => {
props.onBlur?.({ value: newVal, partial: PARTIAL_MAP[position], e });
emit('blur', { value: newVal, partial: PARTIAL_MAP[position], e });
},
onFocus: (newVal: string[], { e, position }: any) => {
props.onFocus?.({ value: newVal, partial: PARTIAL_MAP[position], e });
emit('focus', { value: newVal, partial: PARTIAL_MAP[position], e });
activeIndex.value = position === 'first' ? 0 : 1;
},
onChange: (newVal: string[]) => {
inputValue.value = newVal;
// 跳过不符合格式化的输入框内容
if (!isValidDate(newVal, formatRef.value.format)) return;
const newYear: Array<number> = [];
const newMonth: Array<number> = [];
const newTime: Array<string> = [];
newVal.forEach((v, i) => {
newYear.push(dayjs(v).year() || year.value[i]);
newMonth.push(dayjs(v).month() || month.value[i]);
newTime.push(dayjs(v).format(formatRef.value.timeFormat) || time.value[i]);
});
year.value = newYear;
month.value = newMonth;
time.value = newTime;
},
onEnter: (newVal: string[]) => {
if (!isValidDate(newVal, formatRef.value.format) && !isValidDate(value.value, formatRef.value.format)) return;
popupVisible.value = false;
if (isValidDate(newVal, formatRef.value.format)) {
onChange?.(
formatDate(newVal, {
format: formatRef.value.format,
targetFormat: formatRef.value.valueType,
}) as DateValue[],
{
dayjsValue: newVal.map((v) => dayjs(v)),
trigger: 'enter',
},
);
} else if (isValidDate(value.value, formatRef.value.format)) {
inputValue.value = formatDate(value.value, {
format: formatRef.value.format,
targetFormat: formatRef.value.format,
});
} else {
inputValue.value = [];
}
},
}));
// popup 设置
const popupProps = computed(() => ({
expandAnimation: true,
...props.popupProps,
overlayInnerStyle: props.popupProps?.overlayInnerStyle ?? { width: 'auto' },
overlayClassName: [props.popupProps?.overlayClassName, `${COMPONENT_NAME.value}__panel-container`],
onVisibleChange: (visible: boolean, context: any) => {
// 输入框点击不关闭面板
if (context.trigger === 'trigger-element-click') {
const indexMap = { 0: 'first', 1: 'second' };
inputRef.value?.focus?.({ position: indexMap[activeIndex.value] });
popupVisible.value = true;
return;
}
popupVisible.value = visible;
},
}));
// 输入框响应 value 变化
watchEffect(() => {
if (!value.value) {
inputValue.value = [];
return;
}
if (!isValidDate(value.value, 'valueType')) return;
inputValue.value = formatDate(value.value, {
format: formatRef.value.format,
targetFormat: formatRef.value.format,
});
});
// activeIndex 变化自动 focus 对应输入框
watchEffect(() => {
if (!isMountedRef.value) {
isMountedRef.value = true;
return;
}
const indexMap = { 0: 'first', 1: 'second' };
inputRef.value?.focus?.({ position: indexMap[activeIndex.value] });
});
return {
year,
month,
value,
time,
inputValue,
popupVisible,
rangeInputProps,
popupProps,
isHoverCell,
activeIndex,
isFirstValueSelected,
cacheValue,
onChange,
};
}