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
53 changes: 53 additions & 0 deletions src/date-picker/demos/enUS/date-slot.demo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<markdown>
# Custom Date Cell

Use the `date` slot to customize the content of date cells, such as adding lunar calendar dates or holidays.
</markdown>

<script setup lang="ts">
// Simple holiday mapping example (for demonstration only)
const holidays: Record<string, string> = {
'2024-1-1': 'NY',
'2024-12-25': 'Xmas',
'2024-7-4': 'July 4'
}

function getHoliday(year: number, month: number, date: number) {
const key = `${year}-${month + 1}-${date}`
return holidays[key]
}
</script>

<template>
<n-space vertical>
<n-date-picker type="date" :default-value="Date.now()">
<template #date="{ year, month, date }">
<div style="display: flex; flex-direction: column; align-items: center">
<span>{{ date }}</span>
<span
v-if="getHoliday(year, month, date)"
style="font-size: 10px; color: #18a058"
>
{{ getHoliday(year, month, date) }}
</span>
</div>
</template>
</n-date-picker>
<n-date-picker
type="daterange"
:default-value="[Date.now(), Date.now() + 86400000 * 7]"
>
<template #date="{ year, month, date }">
<div style="display: flex; flex-direction: column; align-items: center">
<span>{{ date }}</span>
<span
v-if="getHoliday(year, month, date)"
style="font-size: 10px; color: #18a058"
>
{{ getHoliday(year, month, date) }}
</span>
</div>
</template>
</n-date-picker>
</n-space>
</template>
20 changes: 11 additions & 9 deletions src/date-picker/demos/enUS/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ shortcuts.vue
events.vue
format.vue
footerslot.vue
date-slot.vue
update-on-close.vue
focus.vue
status.vue
Expand Down Expand Up @@ -192,15 +193,16 @@ panel.vue

### DatePicker Slots

| Name | Parameters | Description | Version |
| ---------- | ---------- | --------------------------------- | ------- |
| date-icon | `()` | Date icon of the input box. | 2.29.0 |
| footer | `()` | Extra Footer. | |
| next-month | `()` | Next icon of the date panel. | 2.33.4 |
| next-year | `()` | Fast next icon of the date panel. | 2.33.4 |
| prev-month | `()` | Prev icon of the date panel. | 2.33.4 |
| prev-year | `()` | Fast prev icon of the date panel. | 2.33.4 |
| separator | `()` | Separator of range picker. | 2.29.0 |
| Name | Parameters | Description | Version |
| --- | --- | --- | --- |
| date | `(props: { year: number, month: number, date: number })` | Custom date cell content. | NEXT_VERSION |
| date-icon | `()` | Date icon of the input box. | 2.29.0 |
| footer | `()` | Extra Footer. | |
| next-month | `()` | Next icon of the date panel. | 2.33.4 |
| next-year | `()` | Fast next icon of the date panel. | 2.33.4 |
| prev-month | `()` | Prev icon of the date panel. | 2.33.4 |
| prev-year | `()` | Fast prev icon of the date panel. | 2.33.4 |
| separator | `()` | Separator of range picker. | 2.29.0 |

### Date, Year, QuarterRange, Week Slots

Expand Down
64 changes: 64 additions & 0 deletions src/date-picker/demos/zhCN/date-slot.demo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<markdown>
# 自定义日期

使用 `date` 插槽可以自定义日期单元格的内容,例如添加农历、节日等信息。
</markdown>

<script lang="ts">
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个也要改

import { defineComponent } from 'vue'

export default defineComponent({
setup() {
// 简单的农历映射示例(仅用于演示)
const lunarDates: Record<string, string> = {
'2024-1-1': '廿一',
'2024-1-15': '初五',
'2024-2-10': '初一',
'2024-12-25': '圣诞'
}

const getLunarDate = (year: number, month: number, date: number) => {
const key = `${year}-${month + 1}-${date}`
return lunarDates[key]
}

return {
getLunarDate
}
}
})
</script>

<template>
<n-space vertical>
<n-date-picker type="date" :default-value="Date.now()">
<template #date="{ year, month, date }">
<div style="display: flex; flex-direction: column; align-items: center">
<span>{{ date }}</span>
<span
v-if="getLunarDate(year, month, date)"
style="font-size: 10px; color: #999"
>
{{ getLunarDate(year, month, date) }}
</span>
</div>
</template>
</n-date-picker>
<n-date-picker
type="daterange"
:default-value="[Date.now(), Date.now() + 86400000 * 7]"
>
<template #date="{ year, month, date }">
<div style="display: flex; flex-direction: column; align-items: center">
<span>{{ date }}</span>
<span
v-if="getLunarDate(year, month, date)"
style="font-size: 10px; color: #999"
>
{{ getLunarDate(year, month, date) }}
</span>
</div>
</template>
</n-date-picker>
</n-space>
</template>
20 changes: 11 additions & 9 deletions src/date-picker/demos/zhCN/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ shortcuts.vue
events.vue
format.vue
footerslot.vue
date-slot.vue
status.vue
icon.vue
panel.vue
Expand Down Expand Up @@ -192,15 +193,16 @@ form-debug.vue

### DatePicker Slots

| 名称 | 参数 | 说明 | 版本 |
| ---------- | ---- | ---------------------------- | ------ |
| date-icon | `()` | 日期输入框的图标 | 2.29.0 |
| footer | `()` | 添加额外的页脚 | |
| next-month | `()` | 日期面板的 `下一个` 图标 | 2.33.4 |
| next-year | `()` | 日期面板的 `快速下一个` 图标 | 2.33.4 |
| prev-month | `()` | 日期面板的 `上一个` 图标 | 2.33.4 |
| prev-year | `()` | 日期面板的 `快速上一个` 图标 | 2.33.4 |
| separator | `()` | 日期范围分隔符号 | 2.29.0 |
| 名称 | 参数 | 说明 | 版本 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

| --- | --- | --- | --- |
| date | `(props: { year: number, month: number, date: number })` | 自定义日期单元格内容 | NEXT_VERSION |
| date-icon | `()` | 日期输入框的图标 | 2.29.0 |
| footer | `()` | 添加额外的页脚 | |
| next-month | `()` | 日期面板的 `下一个` 图标 | 2.33.4 |
| next-year | `()` | 日期面板的 `快速下一个` 图标 | 2.33.4 |
| prev-month | `()` | 日期面板的 `上一个` 图标 | 2.33.4 |
| prev-year | `()` | 日期面板的 `快速上一个` 图标 | 2.33.4 |
| separator | `()` | 日期范围分隔符号 | 2.29.0 |

### Date, Year, QuarterRange, Week Slots

Expand Down
3 changes: 2 additions & 1 deletion src/date-picker/src/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
Value
} from './interface'
import type { UsePanelCommonProps } from './panel/use-panel-common'
import type { DatePickerInst } from './public-types'
import type { DatePickerDateSlotProps, DatePickerInst } from './public-types'
import { format, getTime, isValid } from 'date-fns'
import { getPreciseEventTarget, happensIn } from 'seemly'
import { clickoutside } from 'vdirs'
Expand Down Expand Up @@ -73,6 +73,7 @@ export type DatePickerSetupProps = ExtractPropTypes<typeof datePickerProps>

export interface DatePickerSlots {
'date-icon'?: () => VNode[]
date?: (props: DatePickerDateSlotProps) => VNode[]
footer?: () => VNode[]
'next-month'?: () => VNode[]
'next-year'?: () => VNode[]
Expand Down
3 changes: 2 additions & 1 deletion src/date-picker/src/panel/date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '../../../_utils'
import { NButton, NxButton } from '../../../button'
import PanelHeader from './panelHeader'
import { renderDate } from './renderDate'
import { useCalendar, useCalendarProps } from './use-calendar'

/**
Expand Down Expand Up @@ -160,7 +161,7 @@ export default defineComponent({
}}
>
<div class={`${mergedClsPrefix}-date-panel-date__trigger`} />
{dateItem.dateObject.date}
{renderDate(datePickerSlots.date, dateItem.dateObject)}
{dateItem.isCurrentDate ? (
<div class={`${mergedClsPrefix}-date-panel-date__sup`} />
) : null}
Expand Down
5 changes: 3 additions & 2 deletions src/date-picker/src/panel/daterange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '../../../_utils'
import { NButton, NxButton } from '../../../button'
import PanelHeader from './panelHeader'
import { renderDate } from './renderDate'
import { useDualCalendar, useDualCalendarProps } from './use-dual-calendar'

export default defineComponent({
Expand Down Expand Up @@ -148,7 +149,7 @@ export default defineComponent({
}}
>
<div class={`${mergedClsPrefix}-date-panel-date__trigger`} />
{dateItem.dateObject.date}
{renderDate(datePickerSlots.date, dateItem.dateObject)}
{dateItem.isCurrentDate ? (
<div class={`${mergedClsPrefix}-date-panel-date__sup`} />
) : null}
Expand Down Expand Up @@ -247,7 +248,7 @@ export default defineComponent({
}}
>
<div class={`${mergedClsPrefix}-date-panel-date__trigger`} />
{dateItem.dateObject.date}
{renderDate(datePickerSlots.date, dateItem.dateObject)}
{dateItem.isCurrentDate ? (
<div class={`${mergedClsPrefix}-date-panel-date__sup`} />
) : null}
Expand Down
3 changes: 2 additions & 1 deletion src/date-picker/src/panel/datetime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { NButton, NxButton } from '../../../button'
import { NInput } from '../../../input'
import { NTimePicker } from '../../../time-picker'
import PanelHeader from './panelHeader'
import { renderDate } from './renderDate'
import { useCalendar, useCalendarProps } from './use-calendar'

/**
Expand Down Expand Up @@ -167,7 +168,7 @@ export default defineComponent({
}}
>
<div class={`${mergedClsPrefix}-date-panel-date__trigger`} />
{dateItem.dateObject.date}
{renderDate(datePickerSlots.date, dateItem.dateObject)}
{dateItem.isCurrentDate ? (
<div class={`${mergedClsPrefix}-date-panel-date__sup`} />
) : null}
Expand Down
5 changes: 3 additions & 2 deletions src/date-picker/src/panel/datetimerange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { NButton, NxButton } from '../../../button'
import { NInput } from '../../../input'
import { NTimePicker } from '../../../time-picker'
import PanelHeader from './panelHeader'
import { renderDate } from './renderDate'
import { useDualCalendar, useDualCalendarProps } from './use-dual-calendar'

export default defineComponent({
Expand Down Expand Up @@ -226,7 +227,7 @@ export default defineComponent({
}
>
<div class={`${mergedClsPrefix}-date-panel-date__trigger`} />
{dateItem.dateObject.date}
{renderDate(datePickerSlots.date, dateItem.dateObject)}
{dateItem.isCurrentDate ? (
<div class={`${mergedClsPrefix}-date-panel-date__sup`} />
) : null}
Expand Down Expand Up @@ -335,7 +336,7 @@ export default defineComponent({
}
>
<div class={`${mergedClsPrefix}-date-panel-date__trigger`} />
{dateItem.dateObject.date}
{renderDate(datePickerSlots.date, dateItem.dateObject)}
{dateItem.isCurrentDate ? (
<div class={`${mergedClsPrefix}-date-panel-date__sup`} />
) : null}
Expand Down
11 changes: 11 additions & 0 deletions src/date-picker/src/panel/renderDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { VNodeChild } from 'vue'
import type { DatePickerDateSlotProps } from '../public-types'

export type DatePickerDateSlot = (props: DatePickerDateSlotProps) => VNodeChild

export function renderDate(
dateSlot: DatePickerDateSlot | undefined,
dateObject: DatePickerDateSlotProps
): VNodeChild {
return dateSlot ? dateSlot(dateObject) : dateObject.date
}
6 changes: 6 additions & 0 deletions src/date-picker/src/public-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ export interface DatePickerConfirmSlotProps {
disabled: boolean
text: string
}

export interface DatePickerDateSlotProps {
year: number
month: number
date: number
}