From e6eb8cac3af7a04faf17e05ff77fb3ad52fbcead Mon Sep 17 00:00:00 2001 From: leejimqiu Date: Wed, 21 Sep 2022 21:24:38 +0800 Subject: [PATCH 1/8] feat(calendar): upgrade style --- src/calendar/calendar.vue | 365 +++++++++++++++--------------------- src/calendar/demos/base.vue | 2 +- src/calendar/props.ts | 16 +- src/calendar/type.ts | 6 +- 4 files changed, 168 insertions(+), 221 deletions(-) diff --git a/src/calendar/calendar.vue b/src/calendar/calendar.vue index c8e139c1e..394b3419e 100644 --- a/src/calendar/calendar.vue +++ b/src/calendar/calendar.vue @@ -1,55 +1,40 @@ + + diff --git a/src/calendar/demos/base.vue b/src/calendar/demos/base.vue index d5da19b6f..ff4428781 100644 --- a/src/calendar/demos/base.vue +++ b/src/calendar/demos/base.vue @@ -1,6 +1,6 @@ diff --git a/src/calendar/props.ts b/src/calendar/props.ts index 760cf3aa7..750b7fa7e 100644 --- a/src/calendar/props.ts +++ b/src/calendar/props.ts @@ -6,6 +6,7 @@ import { TdCalendarProps } from './type'; import { PropType } from 'vue'; +import { timestamp } from '@vueuse/core'; export default { /** @@ -35,14 +36,25 @@ export default { /** * 头部插槽(左上角处,默认不显示任何内容) */ - head: { + title: { type: String, }, /** * 当前高亮的日期 */ value: { - type: Date as unknown as PropType, + type: Date as unknown as PropType, default: new Date(), }, + minDate: { + type: Number, + default: null, + }, + maxDate: { + type: Number, + default: null, + }, + onSelect: { + type: Function, + }, }; diff --git a/src/calendar/type.ts b/src/calendar/type.ts index ccc9b0c05..7faae2678 100644 --- a/src/calendar/type.ts +++ b/src/calendar/type.ts @@ -24,7 +24,7 @@ export interface TdCalendarProps { /** * 头部插槽(左上角处,默认不显示任何内容) */ - head?: string; + title?: string; /** * 当前高亮的日期 */ @@ -40,7 +40,7 @@ export type CalendarCell = { year: number; month: number; day: number; -} +}; export type CalendarValue = { year: number; @@ -49,4 +49,4 @@ export type CalendarValue = { week: number; date: string; isToday: boolean; -}; \ No newline at end of file +}; From d6c2e2dc29644f2f1d80a3ded9afb0dd9446426f Mon Sep 17 00:00:00 2001 From: leejimqiu Date: Thu, 22 Sep 2022 19:31:39 +0800 Subject: [PATCH 2/8] feat(calendar): support range --- src/calendar/calendar.vue | 146 +++++++++++++++++------------ src/calendar/demos/base.vue | 6 +- src/calendar/demos/custom-text.vue | 47 ++++++++++ src/calendar/demos/mobile.vue | 14 +-- src/calendar/demos/range.vue | 18 ++++ src/calendar/props.ts | 12 ++- 6 files changed, 170 insertions(+), 73 deletions(-) create mode 100644 src/calendar/demos/custom-text.vue create mode 100644 src/calendar/demos/range.vue diff --git a/src/calendar/calendar.vue b/src/calendar/calendar.vue index 394b3419e..a953bcdb3 100644 --- a/src/calendar/calendar.vue +++ b/src/calendar/calendar.vue @@ -2,8 +2,8 @@
-
- {{ title }} +
+ {{ title || '请选择日期' }}
@@ -13,21 +13,30 @@
- @@ -52,12 +52,11 @@ diff --git a/src/calendar/demos/base.vue b/src/calendar/demos/base.vue index 9f2af481c..765836139 100644 --- a/src/calendar/demos/base.vue +++ b/src/calendar/demos/base.vue @@ -1,6 +1,6 @@ @@ -12,7 +12,7 @@ const visible = ref(false); const handleConfirm = (val: string) => { console.log(val); }; -const cellClick = (val: any) => { +const handleSelect = (val: any) => { console.log(val); }; diff --git a/src/calendar/demos/custom-text.vue b/src/calendar/demos/custom-text.vue index 38d39e492..a8358f1d9 100644 --- a/src/calendar/demos/custom-text.vue +++ b/src/calendar/demos/custom-text.vue @@ -41,7 +41,7 @@ const value = new Date(2022, 1, 15); diff --git a/src/calendar/props.ts b/src/calendar/props.ts index 8b55470f9..21b4b7fee 100644 --- a/src/calendar/props.ts +++ b/src/calendar/props.ts @@ -56,6 +56,9 @@ export default { onSelect: { type: Function, }, + onConfirm: { + type: Function, + }, format: { type: Function, }, diff --git a/src/calendar/type.ts b/src/calendar/type.ts index 7faae2678..47d40dc58 100644 --- a/src/calendar/type.ts +++ b/src/calendar/type.ts @@ -28,7 +28,7 @@ export interface TdCalendarProps { /** * 当前高亮的日期 */ - value?: Date; + value?: CalendarValue; } export interface DateCellDescription { @@ -42,11 +42,4 @@ export type CalendarCell = { day: number; }; -export type CalendarValue = { - year: number; - month: number; - day: number; - week: number; - date: string; - isToday: boolean; -}; +export type CalendarValue = number | number[]; From cb8763fefb1dcba220beecf145ee23af43d6ecda Mon Sep 17 00:00:00 2001 From: leejimqiu Date: Sat, 24 Sep 2022 21:43:52 +0800 Subject: [PATCH 4/8] docs(calendar): update api docs --- src/calendar/calendar.en-US.md | 25 +++++++++++ src/calendar/calendar.md | 28 +++++++----- src/calendar/props.ts | 81 ++++++++++++++-------------------- src/calendar/type.ts | 69 ++++++++++++++++++++--------- 4 files changed, 122 insertions(+), 81 deletions(-) create mode 100644 src/calendar/calendar.en-US.md diff --git a/src/calendar/calendar.en-US.md b/src/calendar/calendar.en-US.md new file mode 100644 index 000000000..6c539744b --- /dev/null +++ b/src/calendar/calendar.en-US.md @@ -0,0 +1,25 @@ +:: BASE_DOC :: + +## API +### Calendar Props + +name | type | default | description | required +-- | -- | -- | -- | -- +confirmBtn | String / Object / Slot / Function | '' | Typescript:`string | TNode | ButtonProps | null`,[Button API Documents](./button?tab=api)。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts)。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/calendar/type.ts) | N +firstDayOfWeek | Number | 7 | \- | N +format | Function | - | Typescript:`(day: TDate) => TDate` `type TDateType = 'selected' | 'disabled' | 'start' | 'centre' | 'end'; ` `interface TDate { date: Date; day: number; type: TDateType; className: string; }`。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/calendar/type.ts) | N +maxDate | Number / Date | - | Typescript:`number | Date` | N +minDate | Number / Date | - | Typescript:`number | Date` | N +title | String / Slot / Function | - | Typescript:`string | TNode`。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts) | N +type | String | 'single' | options:single/multiple/range | N +value | Number / Array / Date | - | Typescript:`number | Date | TCalendarValue[] ` `type TCalendarValue = number | Date`。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/calendar/type.ts) | N +visible | Boolean | false | \- | N +onConfirm | Function | | Typescript:`(value: Date) => void`
| N +onSelect | Function | | Typescript:`(value: Date) => void`
| N + +### Calendar Events + +name | params | description +-- | -- | -- +confirm | `(value: Date)` | \- +select | `(value: Date)` | \- diff --git a/src/calendar/calendar.md b/src/calendar/calendar.md index c26b7d49b..1a03e90b9 100644 --- a/src/calendar/calendar.md +++ b/src/calendar/calendar.md @@ -1,21 +1,25 @@ :: BASE_DOC :: ## API +### Calendar Props -### Calendar Events - -名称 | 参数 | 描述 --- | -- | -- -modelValue | Boolean | false | 控制日历显示与隐藏 | Y -bgColor | String | #0053DB | 按钮和单元格背景颜色 | N -confirmBtn | String | 确认 | 确认按钮文案 | N -head | Slot / Function | - | 头部插槽(左上角处,默认不显示任何内容),TS 类型:`string | TNode` [通用类型定义](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts) | N -cell | Slot / Function | 今天日期 | 自定义单元格 类型:`string | TNode` [通用类型定义](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts) | N -value | String / Date | new Date | 当前高亮的日期,默认今天 | N +名称 | 类型 | 默认值 | 说明 | 必传 +-- | -- | -- | -- | -- +confirmBtn | String / Object / Slot / Function | '' | 确认按钮。值为 null 则不显示确认按钮。值类型为字符串,则表示自定义按钮文本,值类型为 Object 则表示透传 Button 组件属性。。TS 类型:`string | TNode | ButtonProps | null`,[Button API Documents](./button?tab=api)。[通用类型定义](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts)。[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/calendar/type.ts) | N +firstDayOfWeek | Number | 7 | 第一天从星期几开始,默认周日 | N +format | Function | - | 用于格式化日期的函数。TS 类型:`(day: TDate) => TDate` `type TDateType = 'selected' | 'disabled' | 'start' | 'centre' | 'end'; ` `interface TDate { date: Date; day: number; type: TDateType; className: string; }`。[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/calendar/type.ts) | N +maxDate | Number / Date | - | 最大可选的日期,不传则默认半年后。TS 类型:`number | Date` | N +minDate | Number / Date | - | 最小可选的日期,不传则默认今天。TS 类型:`number | Date` | N +title | String / Slot / Function | - | 标题,不传默认为“请选择日期”。TS 类型:`string | TNode`。[通用类型定义](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts) | N +type | String | 'single' | 日历的选择类型,singe = 单选;multiple = 多选; range = 区间选择。可选项:single/multiple/range | N +value | Number / Array / Date | - | 当前选择的日期,不传则默认今天,当 type = multiple 或 range 时传入数组。TS 类型:`number | Date | TCalendarValue[] ` `type TCalendarValue = number | Date`。[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/calendar/type.ts) | N +visible | Boolean | false | 是否显示日历 | N +onConfirm | Function | | TS 类型:`(value: Date) => void`
点击确认按钮时触发 | N +onSelect | Function | | TS 类型:`(value: Date) => void`
选择日期时触发 | N ### Calendar Events 名称 | 参数 | 描述 -- | -- | -- -cell-click | cell: `type CalendarCell = {year: number;month: number;day: number;}`| 点击单元格触发 | -confirm | dateValue: `type CalendarValue = {year: number;month: number;day: number;week: number;date: string;isToday: boolean;}` | 点击确认按钮触发 | +confirm | `(value: Date)` | 点击确认按钮时触发 +select | `(value: Date)` | 选择日期时触发 diff --git a/src/calendar/props.ts b/src/calendar/props.ts index 21b4b7fee..30311c561 100644 --- a/src/calendar/props.ts +++ b/src/calendar/props.ts @@ -6,64 +6,51 @@ import { TdCalendarProps } from './type'; import { PropType } from 'vue'; -import { timestamp } from '@vueuse/core'; export default { - /** - * 控制日历显示隐藏 - */ - visible: { - type: Boolean, - }, - // 按钮颜色 - bgColor: { - type: String, - default: '#0053DB', - }, - /** - * 确认按钮 - */ + /** 确认按钮。值为 null 则不显示确认按钮。值类型为字符串,则表示自定义按钮文本,值类型为 Object 则表示透传 Button 组件属性。 */ confirmBtn: { - type: String, - default: '确认', + type: [String, Object, Function] as PropType, + default: '', }, - /** - * 第一天从星期几开始,仅在日历展示维度为月份时(mode = month)有效。默认为 1 - */ + /** 第一天从星期几开始,默认周日 */ firstDayOfWeek: { type: Number, + default: 7, }, - /** - * 头部插槽(左上角处,默认不显示任何内容) - */ - title: { - type: String, - }, - /** - * 当前高亮的日期 - */ - value: { - type: Number, - }, - minDate: { - type: Number, - default: null, + /** 用于格式化日期的函数 */ + format: { + type: Function as PropType, }, + /** 最大可选的日期,不传则默认半年后 */ maxDate: { - type: Number, - default: null, + type: [Number, Date] as PropType, }, - onSelect: { - type: Function, - }, - onConfirm: { - type: Function, + /** 最小可选的日期,不传则默认今天 */ + minDate: { + type: [Number, Date] as PropType, }, - format: { - type: Function, + /** 标题,不传默认为“请选择日期” */ + title: { + type: [String, Function] as PropType, }, + /** 日历的选择类型,singe = 单选;multiple = 多选; range = 区间选择 */ type: { - type: String, - default: 'single', - }, + type: String as PropType, + default: 'single' as TdCalendarProps['type'], + validator(val: TdCalendarProps['type']): boolean { + if (!val) return true; + return ['single', 'multiple', 'range'].includes(val); + }, + }, + /** 当前选择的日期,不传则默认今天,当 type = multiple 或 range 时传入数组 */ + value: { + type: [Number, Array, Date] as PropType, + }, + /** 是否显示日历 */ + visible: Boolean, + /** 点击确认按钮时触发 */ + onConfirm: Function as PropType, + /** 选择日期时触发 */ + onSelect: Function as PropType, }; diff --git a/src/calendar/type.ts b/src/calendar/type.ts index 47d40dc58..6c31b04a9 100644 --- a/src/calendar/type.ts +++ b/src/calendar/type.ts @@ -3,43 +3,68 @@ /** * 该文件为脚本自动生成文件,请勿随意修改。如需修改请联系 PMC * */ + +import { ButtonProps } from '../button'; +import { TNode } from '../common'; + export interface TdCalendarProps { /** - * 控制日历显示隐藏 + * 确认按钮。值为 null 则不显示确认按钮。值类型为字符串,则表示自定义按钮文本,值类型为 Object 则表示透传 Button 组件属性。 + * @default '' */ - modelValue: boolean; + confirmBtn?: string | TNode | ButtonProps | null; /** - * 背景颜色 + * 第一天从星期几开始,默认周日 + * @default 7 */ - bgColor?: string; - + firstDayOfWeek?: number; /** - * 确认按钮 + * 用于格式化日期的函数 */ - confirmBtn?: string; + format?: (day: TDate) => TDate; /** - * 第一天从星期几开始,仅在日历展示维度为月份时(mode = month)有效。默认为 1 + * 最大可选的日期,不传则默认半年后 */ - firstDayOfWeek?: number; + maxDate?: number | Date; /** - * 头部插槽(左上角处,默认不显示任何内容) + * 最小可选的日期,不传则默认今天 */ - title?: string; + minDate?: number | Date; /** - * 当前高亮的日期 + * 标题,不传默认为“请选择日期” */ - value?: CalendarValue; + title?: string | TNode; + /** + * 日历的选择类型,singe = 单选;multiple = 多选; range = 区间选择 + * @default 'single' + */ + type?: 'single' | 'multiple' | 'range'; + /** + * 当前选择的日期,不传则默认今天,当 type = multiple 或 range 时传入数组 + */ + value?: number | Date | TCalendarValue[]; + /** + * 是否显示日历 + * @default false + */ + visible?: boolean; + /** + * 点击确认按钮时触发 + */ + onConfirm?: (value: Date) => void; + /** + * 选择日期时触发 + */ + onSelect?: (value: Date) => void; } -export interface DateCellDescription { - date: Date; - label: string; -} +export type TDateType = 'selected' | 'disabled' | 'start' | 'centre' | 'end'; -export type CalendarCell = { - year: number; - month: number; +export interface TDate { + date: Date; day: number; -}; + type: TDateType; + className: string; +} -export type CalendarValue = number | number[]; +export type TCalendarValue = number | Date; From b2f9664bdd0e146db95841a627ed53cb685849e2 Mon Sep 17 00:00:00 2001 From: leejimqiu Date: Sat, 24 Sep 2022 23:52:41 +0800 Subject: [PATCH 5/8] feat(calendar): support first-day-of-week property --- src/calendar/calendar.en-US.md | 4 +-- src/calendar/calendar.md | 4 +-- src/calendar/calendar.vue | 54 +++++++++++++++++++++++----------- src/calendar/demos/base.vue | 2 +- src/calendar/demos/cell.vue | 19 ------------ src/calendar/demos/color.vue | 12 -------- src/calendar/demos/head.vue | 14 --------- src/calendar/demos/value.vue | 13 -------- src/calendar/props.ts | 4 +-- src/calendar/type.ts | 10 ++++--- 10 files changed, 50 insertions(+), 86 deletions(-) delete mode 100644 src/calendar/demos/cell.vue delete mode 100644 src/calendar/demos/color.vue delete mode 100644 src/calendar/demos/head.vue delete mode 100644 src/calendar/demos/value.vue diff --git a/src/calendar/calendar.en-US.md b/src/calendar/calendar.en-US.md index 6c539744b..b9b8e0e70 100644 --- a/src/calendar/calendar.en-US.md +++ b/src/calendar/calendar.en-US.md @@ -6,8 +6,8 @@ name | type | default | description | required -- | -- | -- | -- | -- confirmBtn | String / Object / Slot / Function | '' | Typescript:`string | TNode | ButtonProps | null`,[Button API Documents](./button?tab=api)。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts)。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/calendar/type.ts) | N -firstDayOfWeek | Number | 7 | \- | N -format | Function | - | Typescript:`(day: TDate) => TDate` `type TDateType = 'selected' | 'disabled' | 'start' | 'centre' | 'end'; ` `interface TDate { date: Date; day: number; type: TDateType; className: string; }`。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/calendar/type.ts) | N +firstDayOfWeek | Number | 0 | \- | N +format | Function | - | Typescript:`(day: TDate) => TDate` `type TDateType = 'selected' | 'disabled' | 'start' | 'centre' | 'end' | ''; ` `interface TDate { date: Date; day: number; type: TDateType; className?: string; prefix?: string; suffix?: string; }`。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/calendar/type.ts) | N maxDate | Number / Date | - | Typescript:`number | Date` | N minDate | Number / Date | - | Typescript:`number | Date` | N title | String / Slot / Function | - | Typescript:`string | TNode`。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts) | N diff --git a/src/calendar/calendar.md b/src/calendar/calendar.md index 1a03e90b9..45e518535 100644 --- a/src/calendar/calendar.md +++ b/src/calendar/calendar.md @@ -6,8 +6,8 @@ 名称 | 类型 | 默认值 | 说明 | 必传 -- | -- | -- | -- | -- confirmBtn | String / Object / Slot / Function | '' | 确认按钮。值为 null 则不显示确认按钮。值类型为字符串,则表示自定义按钮文本,值类型为 Object 则表示透传 Button 组件属性。。TS 类型:`string | TNode | ButtonProps | null`,[Button API Documents](./button?tab=api)。[通用类型定义](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts)。[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/calendar/type.ts) | N -firstDayOfWeek | Number | 7 | 第一天从星期几开始,默认周日 | N -format | Function | - | 用于格式化日期的函数。TS 类型:`(day: TDate) => TDate` `type TDateType = 'selected' | 'disabled' | 'start' | 'centre' | 'end'; ` `interface TDate { date: Date; day: number; type: TDateType; className: string; }`。[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/calendar/type.ts) | N +firstDayOfWeek | Number | 0 | 第一天从星期几开始,默认 0 = 周日 | N +format | Function | - | 用于格式化日期的函数。TS 类型:`(day: TDate) => TDate` `type TDateType = 'selected' | 'disabled' | 'start' | 'centre' | 'end' | ''; ` `interface TDate { date: Date; day: number; type: TDateType; className?: string; prefix?: string; suffix?: string; }`。[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/calendar/type.ts) | N maxDate | Number / Date | - | 最大可选的日期,不传则默认半年后。TS 类型:`number | Date` | N minDate | Number / Date | - | 最小可选的日期,不传则默认今天。TS 类型:`number | Date` | N title | String / Slot / Function | - | 标题,不传默认为“请选择日期”。TS 类型:`string | TNode`。[通用类型定义](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts) | N diff --git a/src/calendar/calendar.vue b/src/calendar/calendar.vue index 10011252f..25b4016fa 100644 --- a/src/calendar/calendar.vue +++ b/src/calendar/calendar.vue @@ -18,10 +18,10 @@ :class="{ [`${name}__dates-item`]: true, [`${name}__dates-item--${dateItem.type}`]: !!dateItem.type, - [`${dateItem.classname ?? ''}`]: true, + [`${dateItem.className ?? ''}`]: true, }" :style="{ - marginLeft: dateIndex === 0 ? `${49 * item.weekdayOfFirstDay}px` : 0, + marginLeft: dateIndex === 0 ? `${49 * ((item.weekdayOfFirstDay - firstDayOfWeek + 7) % 7)}px` : 0, }" @click="handleSelect(item.year, item.month, dateItem.day, dateItem)" > @@ -44,7 +44,11 @@
@@ -57,6 +61,7 @@ import { CloseIcon } from 'tdesign-icons-vue-next'; import config from '../config'; import calendarProps from './props'; +import { TDate, TDateType } from './type'; const { prefix } = config; const name = `${prefix}-calendar`; @@ -82,9 +87,20 @@ const getYearMonthDay = (date: Date) => { const popup = ref(props.visible); const valueRef = ref(props.value); const selectedDate = ref(); - +const firstDayOfWeek = computed(() => props.firstDayOfWeek); const type = computed(() => props.type); -const days = ref(['日', '一', '二', '三', '四', '五', '六']); +const days = computed(() => { + const raw = '日一二三四五六'; + const ans = []; + let i = firstDayOfWeek.value % 7; + + while (ans.length < 7) { + ans.push(raw[i]); + i = (i + 1) % 7; + } + + return ans; +}); const today = new Date(); const minDate = props.minDate ? new Date(props.minDate) : today; const maxDate = props.maxDate @@ -93,11 +109,9 @@ const maxDate = props.maxDate // 获取日期 const getDate = (year: number, month: number, day: number) => new Date(year, month, day); -interface TDateItem { - type: 'selected' | 'disabled' | 'start' | 'centre' | 'end'; -} + // 选择日期 -const handleSelect = (year: number, month: number, date: number, dateItem: TDateItem) => { +const handleSelect = (year: number, month: number, date: number, dateItem: TDate) => { if (dateItem.type === 'disabled') return; const selected = new Date(year, month, date); @@ -114,18 +128,19 @@ const handleSelect = (year: number, month: number, date: number, dateItem: TDate } else { selectedDate.value = selected; } - props.onSelect?.({ value: selectedDate.value }); + props.onSelect?.(selectedDate.value); }; // 确认 const handleConfirm = () => { popup.value = false; - props.onConfirm?.({ value: selectedDate.value }); + props.onConfirm?.(selectedDate.value); }; const getMonthDates = (date: Date) => { const { year, month } = getYearMonthDay(date); const firstDay = getDate(year, month, 1); const weekdayOfFirstDay = firstDay.getDay(); - const lastDate = new Date(+getDate(year, month + 1, 1) - 24 * 3600).getDate(); + const lastDate = new Date(+getDate(year, month + 1, 1) - 24 * 3600 * 1000).getDate(); + console.log(lastDate); return { year, @@ -134,8 +149,8 @@ const getMonthDates = (date: Date) => { lastDate, }; }; -type TDate = Date | number | { year: number; month: number; date: number }; -const isSameDate = (date1: TDate, date2: TDate) => { +type CompareDate = Date | number | { year: number; month: number; date: number }; +const isSameDate = (date1: CompareDate, date2: CompareDate) => { if (date1 instanceof Date) date1 = getYearMonthDay(date1); if (date2 instanceof Date) date2 = getYearMonthDay(date2); const keys = ['year', 'month', 'date']; @@ -146,7 +161,7 @@ const months = computed(() => { const ans = []; let { year: minYear, month: minMonth } = getYearMonthDay(minDate); const { year: maxYear, month: maxMonth } = getYearMonthDay(maxDate); - const calcType = (year: number, month: number, date: number) => { + const calcType = (year: number, month: number, date: number): TDateType => { const curDate = new Date(year, month, date, 23, 59, 59); if (type.value === 'single') { @@ -171,10 +186,10 @@ const months = computed(() => { while (minYear < maxYear || (minYear === maxYear && minMonth <= maxMonth)) { const target = getMonthDates(getDate(minYear, minMonth, 1)); - const months = []; + const months: TDate[] = []; for (let i = 1; i <= 31; i++) { if (i > target.lastDate) break; - const dateObj = { + const dateObj: TDate = { date: getDate(minYear, minMonth, i), day: i, type: calcType(minYear, minMonth, i), @@ -195,6 +210,11 @@ const months = computed(() => { return ans; }); +const confirmBtn = computed(() => { + if (props.confirmBtn === 'string') return { content: props.confirmBtn }; + return props.confirmBtn; +}); + watch( () => props.visible, (val) => { diff --git a/src/calendar/demos/base.vue b/src/calendar/demos/base.vue index 765836139..2a4e3b7a1 100644 --- a/src/calendar/demos/base.vue +++ b/src/calendar/demos/base.vue @@ -8,7 +8,7 @@ diff --git a/src/calendar/demos/color.vue b/src/calendar/demos/color.vue deleted file mode 100644 index 142fd6772..000000000 --- a/src/calendar/demos/color.vue +++ /dev/null @@ -1,12 +0,0 @@ - - - diff --git a/src/calendar/demos/head.vue b/src/calendar/demos/head.vue deleted file mode 100644 index 0ee0a6aa4..000000000 --- a/src/calendar/demos/head.vue +++ /dev/null @@ -1,14 +0,0 @@ - - - diff --git a/src/calendar/demos/value.vue b/src/calendar/demos/value.vue deleted file mode 100644 index 82db1a794..000000000 --- a/src/calendar/demos/value.vue +++ /dev/null @@ -1,13 +0,0 @@ - - - diff --git a/src/calendar/props.ts b/src/calendar/props.ts index 30311c561..b36abe9fe 100644 --- a/src/calendar/props.ts +++ b/src/calendar/props.ts @@ -13,10 +13,10 @@ export default { type: [String, Object, Function] as PropType, default: '', }, - /** 第一天从星期几开始,默认周日 */ + /** 第一天从星期几开始,默认 0 = 周日 */ firstDayOfWeek: { type: Number, - default: 7, + default: 0, }, /** 用于格式化日期的函数 */ format: { diff --git a/src/calendar/type.ts b/src/calendar/type.ts index 6c31b04a9..a99963741 100644 --- a/src/calendar/type.ts +++ b/src/calendar/type.ts @@ -14,8 +14,8 @@ export interface TdCalendarProps { */ confirmBtn?: string | TNode | ButtonProps | null; /** - * 第一天从星期几开始,默认周日 - * @default 7 + * 第一天从星期几开始,默认 0 = 周日 + * @default 0 */ firstDayOfWeek?: number; /** @@ -58,13 +58,15 @@ export interface TdCalendarProps { onSelect?: (value: Date) => void; } -export type TDateType = 'selected' | 'disabled' | 'start' | 'centre' | 'end'; +export type TDateType = 'selected' | 'disabled' | 'start' | 'centre' | 'end' | ''; export interface TDate { date: Date; day: number; type: TDateType; - className: string; + className?: string; + prefix?: string; + suffix?: string; } export type TCalendarValue = number | Date; From 4b0e9c6359df417057cd7f379434bbde0c5226f1 Mon Sep 17 00:00:00 2001 From: leejimqiu Date: Sun, 25 Sep 2022 00:30:36 +0800 Subject: [PATCH 6/8] feat(calendar): support type = multiple --- src/calendar/calendar.md | 2 +- src/calendar/calendar.vue | 29 +++++++++++++++++++++++------ src/calendar/demos/base.vue | 2 +- src/calendar/demos/custom-text.vue | 2 +- src/calendar/demos/mobile.vue | 7 ++----- src/calendar/demos/multiple.vue | 18 ++++++++++++++++++ src/calendar/props.ts | 2 +- src/calendar/type.ts | 2 +- 8 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 src/calendar/demos/multiple.vue diff --git a/src/calendar/calendar.md b/src/calendar/calendar.md index 45e518535..2b44bc46e 100644 --- a/src/calendar/calendar.md +++ b/src/calendar/calendar.md @@ -11,7 +11,7 @@ format | Function | - | 用于格式化日期的函数。TS 类型:`(day: TDat maxDate | Number / Date | - | 最大可选的日期,不传则默认半年后。TS 类型:`number | Date` | N minDate | Number / Date | - | 最小可选的日期,不传则默认今天。TS 类型:`number | Date` | N title | String / Slot / Function | - | 标题,不传默认为“请选择日期”。TS 类型:`string | TNode`。[通用类型定义](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts) | N -type | String | 'single' | 日历的选择类型,singe = 单选;multiple = 多选; range = 区间选择。可选项:single/multiple/range | N +type | String | 'single' | 日历的选择类型,single = 单选;multiple = 多选; range = 区间选择。可选项:single/multiple/range | N value | Number / Array / Date | - | 当前选择的日期,不传则默认今天,当 type = multiple 或 range 时传入数组。TS 类型:`number | Date | TCalendarValue[] ` `type TCalendarValue = number | Date`。[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/calendar/type.ts) | N visible | Boolean | false | 是否显示日历 | N onConfirm | Function | | TS 类型:`(value: Date) => void`
点击确认按钮时触发 | N diff --git a/src/calendar/calendar.vue b/src/calendar/calendar.vue index 25b4016fa..dd1fff98c 100644 --- a/src/calendar/calendar.vue +++ b/src/calendar/calendar.vue @@ -56,8 +56,9 @@ diff --git a/src/calendar/demos/multiple.vue b/src/calendar/demos/multiple.vue new file mode 100644 index 000000000..381c09111 --- /dev/null +++ b/src/calendar/demos/multiple.vue @@ -0,0 +1,18 @@ + + + diff --git a/src/calendar/props.ts b/src/calendar/props.ts index b36abe9fe..6fe48b28a 100644 --- a/src/calendar/props.ts +++ b/src/calendar/props.ts @@ -34,7 +34,7 @@ export default { title: { type: [String, Function] as PropType, }, - /** 日历的选择类型,singe = 单选;multiple = 多选; range = 区间选择 */ + /** 日历的选择类型,single = 单选;multiple = 多选; range = 区间选择 */ type: { type: String as PropType, default: 'single' as TdCalendarProps['type'], diff --git a/src/calendar/type.ts b/src/calendar/type.ts index a99963741..54a9f03de 100644 --- a/src/calendar/type.ts +++ b/src/calendar/type.ts @@ -35,7 +35,7 @@ export interface TdCalendarProps { */ title?: string | TNode; /** - * 日历的选择类型,singe = 单选;multiple = 多选; range = 区间选择 + * 日历的选择类型,single = 单选;multiple = 多选; range = 区间选择 * @default 'single' */ type?: 'single' | 'multiple' | 'range'; From 77d1deb9d0744de8a2346420952f008d6bea7d45 Mon Sep 17 00:00:00 2001 From: leejimqiu Date: Sun, 25 Sep 2022 16:48:45 +0800 Subject: [PATCH 7/8] feat(calendar): enrich demo --- src/calendar/calendar.vue | 5 +++-- src/calendar/demos/custom-button.vue | 15 +++++++++++++++ src/calendar/demos/custom-range.vue | 14 ++++++++++++++ src/calendar/demos/mobile.vue | 4 ++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/calendar/demos/custom-button.vue create mode 100644 src/calendar/demos/custom-range.vue diff --git a/src/calendar/calendar.vue b/src/calendar/calendar.vue index dd1fff98c..d48502cfb 100644 --- a/src/calendar/calendar.vue +++ b/src/calendar/calendar.vue @@ -46,7 +46,7 @@ @@ -193,7 +193,8 @@ const months = computed(() => { } } - if (curDate.getTime() < minDate.getTime() || curDate.getTime() > maxDate.getTime()) { + const minCurDate = new Date(year, month, date, 0, 0, 0); + if (curDate.getTime() < minDate.getTime() || minCurDate.getTime() > maxDate.getTime()) { return 'disabled'; } return ''; diff --git a/src/calendar/demos/custom-button.vue b/src/calendar/demos/custom-button.vue new file mode 100644 index 000000000..0a38047c8 --- /dev/null +++ b/src/calendar/demos/custom-button.vue @@ -0,0 +1,15 @@ + + + diff --git a/src/calendar/demos/custom-range.vue b/src/calendar/demos/custom-range.vue new file mode 100644 index 000000000..5a7645af5 --- /dev/null +++ b/src/calendar/demos/custom-range.vue @@ -0,0 +1,14 @@ + + + diff --git a/src/calendar/demos/mobile.vue b/src/calendar/demos/mobile.vue index c4f8426c2..d714263d1 100644 --- a/src/calendar/demos/mobile.vue +++ b/src/calendar/demos/mobile.vue @@ -13,6 +13,8 @@ + +