Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@
<br /><br />
<t-date-picker mode="year" range />
<br /><br />
<t-date-picker mode="week" />
<br /><br />
<t-date-picker mode="week" range />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里没有这个 range api 吧?

<br /><br />
</t-config-provider>
</template>

<script setup>
const DATE_PICK_CONFIGS = {
dayjsLocale: 'en',
placeholder: {
date: 'select date',
month: 'select month',
year: 'select year',
week: 'select week',
},
weekdays: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
Expand All @@ -30,6 +36,7 @@ const DATE_PICK_CONFIGS = {
now: 'Now',
selectTime: 'Select Time',
selectDate: 'Select Date',
weekAbbreviation: 'W',
};

const globalConfig = {
Expand Down
44 changes: 37 additions & 7 deletions packages/components/date-picker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ export default defineComponent({
// 多选不考虑输入情况
if (props.multiple) return;
// 如果不需要确认,直接保存当前值

if (!props.needConfirm && props.enableTimePicker && !visible) {
const nextValue = formatDate(inputValue.value, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
if (nextValue) {
onChange?.(
formatDate(inputValue.value, {
format: formatRef.value.format,
targetFormat: formatRef.value.valueType,
dayjsLocale: globalConfig.value.dayjsLocale,
}) as DateValue,
{
dayjsValue: parseToDayjs(inputValue.value as string, formatRef.value.format),
Expand All @@ -79,6 +82,7 @@ export default defineComponent({
} else {
inputValue.value = formatDate(value.value, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
}
}
Expand All @@ -93,10 +97,12 @@ export default defineComponent({
cacheValue.value = formatDate(dateValue, {
format: formatRef.value.valueType,
targetFormat: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
inputValue.value = formatDate(dateValue, {
format: formatRef.value.valueType,
targetFormat: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});

// 面板展开重置数据
Expand All @@ -112,10 +118,10 @@ export default defineComponent({
// 日期 hover
function onCellMouseEnter(date: Date) {
if (props.multiple) return;

isHoverCell.value = true;
inputValue.value = formatDate(date, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
}

Expand All @@ -126,6 +132,7 @@ export default defineComponent({
isHoverCell.value = false;
inputValue.value = formatDate(cacheValue.value, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
}

Expand All @@ -140,6 +147,7 @@ export default defineComponent({
if (props.enableTimePicker) {
cacheValue.value = formatDate(date, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
} else {
if (props.multiple) {
Expand All @@ -155,6 +163,7 @@ export default defineComponent({
formatDate(date, {
format: formatRef.value.format,
targetFormat: formatRef.value.valueType,
dayjsLocale: globalConfig.value.dayjsLocale,
}) as DateValue,
{
dayjsValue: parseToDayjs(date, formatRef.value.format),
Expand All @@ -170,26 +179,39 @@ export default defineComponent({
function processDate(date: Date) {
let isSameDate: boolean;
const currentValue = (value.value || []) as DateMultipleValue;
const { dayjsLocale } = globalConfig.value;

let currentDate: DateMultipleValue;
if (props.mode !== 'week')
isSameDate = currentValue.some((val) =>
isSame(parseToDayjs(val, formatRef.value.format).toDate(), date, props.mode, dayjsLocale),
isSame(parseToDayjs(val, formatRef.value.format).toDate(), date, props.mode, globalConfig.value.dayjsLocale),
);
else {
isSameDate = currentValue.some((val) => val === dayjs(date).locale(dayjsLocale).format(formatRef.value.format));
isSameDate = currentValue.some(
(val) => val === dayjs(date).locale(globalConfig.value.dayjsLocale).format(formatRef.value.format),
);
}

if (!isSameDate) {
currentDate = currentValue.concat(
formatDate(date, { format: formatRef.value.format, targetFormat: formatRef.value.valueType }),
formatDate(date, {
format: formatRef.value.format,
targetFormat: formatRef.value.valueType,
dayjsLocale: globalConfig.value.dayjsLocale,
}),
);
} else {
currentDate = currentValue.filter(
(val) =>
formatDate(val, { format: formatRef.value.format, targetFormat: formatRef.value.valueType }) !==
formatDate(date, { format: formatRef.value.format, targetFormat: formatRef.value.valueType }),
formatDate(val, {
format: formatRef.value.format,
targetFormat: formatRef.value.valueType,
dayjsLocale: globalConfig.value.dayjsLocale,
}) !==
formatDate(date, {
format: formatRef.value.format,
targetFormat: formatRef.value.valueType,
dayjsLocale: globalConfig.value.dayjsLocale,
}),
);
}
return currentDate;
Expand Down Expand Up @@ -254,9 +276,11 @@ export default defineComponent({
const nextDate = currentDate.hour(nextHours).minute(minutes).second(seconds).millisecond(milliseconds).toDate();
inputValue.value = formatDate(nextDate, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
cacheValue.value = formatDate(nextDate, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});

props.onPick?.(nextDate);
Expand All @@ -266,13 +290,16 @@ export default defineComponent({
function onConfirmClick({ e }: { e: MouseEvent }) {
const nextValue = formatDate(inputValue.value, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
if (nextValue) {
props?.onConfirm?.({ date: dayjs(nextValue as string).toDate(), e });

onChange?.(
formatDate(inputValue.value, {
format: formatRef.value.format,
targetFormat: formatRef.value.valueType,
dayjsLocale: globalConfig.value.dayjsLocale,
}) as DateValue,
{
dayjsValue: parseToDayjs(inputValue.value as string, formatRef.value.format),
Expand All @@ -282,6 +309,7 @@ export default defineComponent({
} else {
inputValue.value = formatDate(value.value, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
}
popupVisible.value = false;
Expand All @@ -294,6 +322,7 @@ export default defineComponent({
formatDate(presetVal, {
format: formatRef.value.format,
targetFormat: formatRef.value.valueType,
dayjsLocale: globalConfig.value.dayjsLocale,
}) as DateValue,
{
dayjsValue: parseToDayjs(presetVal, formatRef.value.format),
Expand All @@ -303,6 +332,7 @@ export default defineComponent({
// 更新到 input,避免 needConfirm 导致值被覆盖
inputValue.value = formatDate(presetVal, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
popupVisible.value = false;
}
Expand Down
41 changes: 31 additions & 10 deletions packages/components/date-picker/DatePickerPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import datePickerPanelProps from './date-picker-panel-props';
import datePickerProps from './props';

import TSinglePanel from './components/panel/SinglePanel';
import { useConfig } from '@tdesign/shared-hooks';

export default defineComponent({
name: 'TDatePickerPanel',
Expand All @@ -35,6 +36,7 @@ export default defineComponent({
},

setup(props: TdDatePickerPanelProps) {
const { globalConfig } = useConfig('datePicker');
const { cacheValue, value, year, month, time, onChange } = useSingleValue(props);

const formatRef = computed(() =>
Expand All @@ -55,12 +57,21 @@ export default defineComponent({
month.value = date.getMonth();
}
if (props.enableTimePicker) {
cacheValue.value = formatDate(date, { format: formatRef.value.format });
} else {
onChange?.(formatDate(date, { format: formatRef.value.format }) as DateValue, {
dayjsValue: parseToDayjs(date, formatRef.value.format),
trigger: 'pick',
cacheValue.value = formatDate(date, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
} else {
onChange?.(
formatDate(date, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
}) as DateValue,
{
dayjsValue: parseToDayjs(date, formatRef.value.format),
trigger: 'pick',
},
);
}
}

Expand Down Expand Up @@ -120,7 +131,10 @@ export default defineComponent({
? dayjs()
: dayjs(cacheValue.value as string, formatRef.value.format);
const nextDate = currentDate.hour(nextHours).minute(minutes).second(seconds).millisecond(milliseconds).toDate();
cacheValue.value = formatDate(nextDate, { format: formatRef.value.format });
cacheValue.value = formatDate(nextDate, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});

props.onTimeChange?.({
time: val,
Expand All @@ -134,6 +148,7 @@ export default defineComponent({
onChange?.(
formatDate(cacheValue.value, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
}) as DateValue,
{
dayjsValue: parseToDayjs(cacheValue.value as string, formatRef.value.format),
Expand All @@ -146,10 +161,16 @@ export default defineComponent({
// 预设
function onPresetClick(preset: any, context: any) {
const presetVal = isFunction(preset) ? preset() : preset;
onChange?.(formatDate(presetVal, { format: formatRef.value.format }) as DateValue, {
dayjsValue: parseToDayjs(presetVal, formatRef.value.format),
trigger: 'preset',
});
onChange?.(
formatDate(presetVal, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
}) as DateValue,
{
dayjsValue: parseToDayjs(presetVal, formatRef.value.format),
trigger: 'preset',
},
);
props.onPresetClick?.(context);
}

Expand Down
12 changes: 11 additions & 1 deletion packages/components/date-picker/DateRangePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineComponent, computed, ref, watch } from 'vue';
import dayjs from 'dayjs';
import { useDisabled, useReadonly, usePrefixClass } from '@tdesign/shared-hooks';
import { useConfig, useDisabled, useReadonly, usePrefixClass } from '@tdesign/shared-hooks';

import { isArray, isFunction } from 'lodash-es';

Expand All @@ -26,6 +26,7 @@ export default defineComponent({
props,
setup(props, { slots }) {
const COMPONENT_NAME = usePrefixClass('date-range-picker');
const { globalConfig } = useConfig('datePicker');

const {
inputValue,
Expand Down Expand Up @@ -65,6 +66,7 @@ export default defineComponent({
cacheValue.value = formatDate(value.value || [], {
format: formatRef.value.valueType,
targetFormat: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
}) as string[];
time.value = formatTime(
value.value || [dayjs().format(formatRef.value.timeFormat), dayjs().format(formatRef.value.timeFormat)],
Expand Down Expand Up @@ -110,6 +112,7 @@ export default defineComponent({
inputValue.value = formatDate(value.value, {
format: formatRef.value.valueType,
targetFormat: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
} else {
confirmValueChange();
Expand All @@ -123,6 +126,7 @@ export default defineComponent({
const nextValue = [...(inputValue.value as string[])];
nextValue[activeIndex.value] = formatDate(date, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
}) as string;
inputValue.value = nextValue;
}
Expand All @@ -143,6 +147,7 @@ export default defineComponent({
const nextValue = [...(inputValue.value as string[])];
nextValue[activeIndex.value] = formatDate(date, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
}) as string;
cacheValue.value = nextValue;
inputValue.value = nextValue;
Expand All @@ -169,6 +174,7 @@ export default defineComponent({
format: formatRef.value.format,
targetFormat: formatRef.value.valueType,
autoSwap: true,
dayjsLocale: globalConfig.value.dayjsLocale,
}) as DateValue[],
{
dayjsValue: nextValue.map((v) => parseToDayjs(v, formatRef.value.format)),
Expand Down Expand Up @@ -245,9 +251,11 @@ export default defineComponent({
isSelected.value = true;
inputValue.value = formatDate(nextInputValue, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
cacheValue.value = formatDate(nextInputValue, {
format: formatRef.value.format,
dayjsLocale: globalConfig.value.dayjsLocale,
});
}
const confirmValueChange = (e?: MouseEvent) => {
Expand Down Expand Up @@ -276,6 +284,7 @@ export default defineComponent({
format: formatRef.value.format,
targetFormat: formatRef.value.valueType,
autoSwap: true,
dayjsLocale: globalConfig.value.dayjsLocale,
}) as DateValue[],
{
dayjsValue: nextValue.map((v) => parseToDayjs(v, formatRef.value.format)),
Expand Down Expand Up @@ -318,6 +327,7 @@ export default defineComponent({
format: formatRef.value.format,
targetFormat: formatRef.value.valueType,
autoSwap: true,
dayjsLocale: globalConfig.value.dayjsLocale,
}) as DateValue[],
{
dayjsValue: presetValue.map((p) => parseToDayjs(p, formatRef.value.format)),
Expand Down
Loading
Loading