Skip to content

Commit

Permalink
fix: 优化 datepicker 输入事件交互 (#1736)
Browse files Browse the repository at this point in the history
  • Loading branch information
honkinglin authored Sep 26, 2022
1 parent 5a544c3 commit 71313d2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
14 changes: 9 additions & 5 deletions src/date-picker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,20 @@ export default defineComponent({
);

watch(popupVisible, (visible) => {
cacheValue.value = formatDate(value.value, {
format: formatRef.value.format,
});
inputValue.value = formatDate(value.value, {
format: formatRef.value.format,
});

// 面板展开重置数据
if (visible) {
year.value = parseToDayjs(value.value, formatRef.value.format).year();
month.value = parseToDayjs(value.value, formatRef.value.format).month();
time.value = formatTime(value.value, formatRef.value.timeFormat);
if (value.value) {
cacheValue.value = formatDate(value.value, {
format: formatRef.value.format,
});
}
} else {
isHoverCell.value = false;
}
});

Expand Down
1 change: 1 addition & 0 deletions src/date-picker/DateRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default defineComponent({
nextMonth[0] === 11 ? (nextMonth[0] -= 1) : (nextMonth[1] += 1);
}
month.value = nextMonth;
year.value = value.value.map((v: string) => parseToDayjs(v || new Date(), formatRef.value.format).year());
} else {
year.value = value.value.map((v: string) => parseToDayjs(v || new Date(), formatRef.value.format).year());
month.value = value.value.map((v: string) => parseToDayjs(v || new Date(), formatRef.value.format).month());
Expand Down
12 changes: 6 additions & 6 deletions src/date-picker/hooks/useSingle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ export default function useSingle(props: TdDatePickerProps) {
!Number.isNaN(newTime) && (time.value = newTime);
},
onEnter: (val: string) => {
if (!val) {
onChange('', { dayjsValue: dayjs(), trigger: 'enter' });
popupVisible.value = false;
return;
}

if (!isValidDate(val, formatRef.value.format) && !isValidDate(value.value, formatRef.value.format)) return;

popupVisible.value = false;
Expand Down Expand Up @@ -113,12 +119,6 @@ export default function useSingle(props: TdDatePickerProps) {
popupVisible.value = true;
return;
}
if (!visible) {
isHoverCell.value = false;
inputValue.value = formatDate(value.value, {
format: formatRef.value.format,
});
}
popupVisible.value = visible;
},
}));
Expand Down

0 comments on commit 71313d2

Please sign in to comment.