DatePicker 日期選擇器
在面板上選年份、月份、日期、日期時間,或者選一段日期範圍,也可以直接在輸入框裡按格式輸入。
引入
import { HuiDatePicker } from '@nscode/handy-ui'
原生屬性與標籤關聯
class、style 保留在可見外層;id、name、autocomplete、tabindex、ARIA 及其他原生屬性只落到輸入元素。顯式 ID 與 FormItem 標籤同步,使用者說明和表單說明/錯誤 ID 動態合併、去重。data-hui-field、data-invalid、data-disabled、data-open、data-filled 是保留狀態標記,只落在實際畫邊框的元素,外傳 value 不覆蓋實際模型。
editable=false 時沒有原生 input,屬性落到實際 role="button" 的觸發元素,使用 aria-label 或 aria-labelledby 命名;此模式的 name 不會建立原生表單欄位。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiDatePicker } from '@nscode/handy-ui'
const version = ref(1)
const value = ref<Date | null>(new Date(2026, 10, 20))
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="version++">更換欄位 ID</HuiButton>
<label :for="`native-datepicker-${version}`">欄位名稱</label>
<HuiDatePicker
:id="`native-datepicker-${version}`"
v-model="value"
name="native-datepicker"
aria-describedby="native-datepicker-help"
autocomplete="on"
style="width: 280px; max-width: 100%"
/>
<span id="native-datepicker-help">標籤與說明始終關聯實際輸入欄位。</span>
</HuiSpace>
</template>
基礎用法
用 v-model 綁定。不傳 valueFormat 時發出的是 Date,沒有值時是 null。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
</script>
<template>
<HuiDatePicker v-model="value" style="width: 240px" />
<p style="margin-top: 12px">綁定值:{{ value ? value.toLocaleString() : 'null' }}</p>
</template>
選擇器類型
type 決定面板的樣子和發出的值:
| type | 面板 | 發出的值 |
|---|---|---|
year | 年份 | 那年 1 月 1 日 0 點 |
month | 月份 | 那個月 1 日 0 點 |
date | 日期(預設) | 當天 0 點 |
datetime | 日期加時分秒 | 帶時分秒的 Date |
daterange | 左右兩個月 | [开始, 结束],都是 0 點 |
datetimerange | 兩個月加時分秒 | [开始, 结束],各自帶時分秒 |
下面每一行是一種類型,右邊顯示選完以後發出的值:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const year = ref<Date | null>(null)
const month = ref<Date | null>(null)
const date = ref<Date | null>(null)
const datetime = ref<Date | null>(null)
const daterange = ref<[Date, Date] | null>(null)
const datetimerange = ref<[Date, Date] | null>(null)
// Date 按本地時間顯示,範圍用 ~ 連起來
const show = (value: Date | [Date, Date] | null) => {
if (!value) return 'null'
const text = (date: Date) => date.toLocaleString()
return Array.isArray(value) ? value.map(text).join(' ~ ') : text(value)
}
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">year</span>
<HuiDatePicker v-model="year" type="year" style="width: 240px" />
<span class="row-value">{{ show(year) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">month</span>
<HuiDatePicker v-model="month" type="month" style="width: 240px" />
<span class="row-value">{{ show(month) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">date</span>
<HuiDatePicker v-model="date" type="date" style="width: 240px" />
<span class="row-value">{{ show(date) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">datetime</span>
<HuiDatePicker v-model="datetime" type="datetime" style="width: 240px" />
<span class="row-value">{{ show(datetime) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">daterange</span>
<HuiDatePicker v-model="daterange" type="daterange" style="width: 300px" />
<span class="row-value">{{ show(daterange) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">datetimerange</span>
<HuiDatePicker v-model="datetimerange" type="datetimerange" style="width: 400px" />
<span class="row-value">{{ show(datetimerange) }}</span>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 104px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.row-value {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
日期範圍
type="daterange" 時在面板上點兩次選出範圍,先點哪頭都行,點完第二個日期後按先後排好再發出。只點了一個日期就關掉面板的,這次選擇不算。點了第一個日期以後,滑鼠停在別的日期上(或者用方向鍵移到別的日期上)會預覽將要選中的範圍。
範圍只有一個輸入框,提示文字是“開始 分隔符 結束”:預設取語言包(中文的開始、結束提示文字分別是“開始日期”“結束日期”,分隔符是“至”)。只傳 placeholder 時整段用它;空提示文字保留原有的語言回退,顯式空分隔符不回退。面板的星期、月份、按鈕和螢幕閱讀器名稱也隨語言變化,日期綁定值和 format 不變。
左右兩個面板預設顯示相鄰兩個月,一起翻頁。加上 unlinkPanels 後兩個面板各自翻頁。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const range1 = ref<[Date, Date] | null>(null)
const range2 = ref<[Date, Date] | null>(null)
const range3 = ref<[Date, Date] | null>(null)
const range4 = ref<[Date, Date] | null>(null)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">預設</span>
<HuiDatePicker v-model="range1" type="daterange" style="width: 300px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">開始、結束、分隔符</span>
<HuiDatePicker
v-model="range2"
type="daterange"
start-placeholder="入住"
end-placeholder="離店"
range-separator="到"
style="width: 300px"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">只傳 placeholder</span>
<HuiDatePicker
v-model="range3"
type="daterange"
placeholder="請選擇統計週期"
style="width: 300px"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">unlink-panels</span>
<HuiDatePicker v-model="range4" type="daterange" unlink-panels style="width: 300px" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 120px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
日期時間
datetime、datetimerange 選了日期以後面板不關,下面的時、分、秒輸入框改完立即生效。超出範圍的數字收回到 0-23 / 0-59,刪空後恢復原值。還沒選日期時先改時間,選日期時會用上。同一天的範圍,開始時間不會晚於結束時間。
底部按鈕在 datetime 類型是“此刻”,在 date 類型是“今天”(發當天 0 點),範圍類型不顯示。
defaultTime 是選日期時用的時分秒。datetime 傳一個 Date,只取它的時分秒;datetimerange 傳兩個,常配成開始 0 點、結束 23:59:59:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
const range = ref<[Date, Date] | null>(null)
// 只用時分秒,年月日隨便寫
const nineOClock = new Date(2000, 0, 1, 9, 0, 0)
const defaultTime: [Date, Date] = [
new Date(2000, 0, 1, 0, 0, 0),
new Date(2000, 0, 1, 23, 59, 59)
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">預設 09:00:00</span>
<HuiDatePicker
v-model="value"
type="datetime"
:default-time="nineOClock"
style="width: 240px"
/>
<span class="row-value">{{ value ? value.toLocaleString() : 'null' }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">開始 0 點、結束 23:59:59</span>
<HuiDatePicker
v-model="range"
type="datetimerange"
:default-time="defaultTime"
style="width: 400px"
/>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 160px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.row-value {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
格式
format只管輸入框裡如何顯示,以及手動輸入時按什麼格式解析。不傳時按類型取預設值:YYYY、YYYY-MM、YYYY-MM-DD、YYYY-MM-DD HH:mm:ss。valueFormat不傳時發Date;x發毫秒時間戳數字,X發秒時間戳數字,其他格式發字串,綁定值也按它解析。數字輸入僅在X時按秒,其餘按毫秒。
| 單位 | 含義 | 單位 | 含義 |
|---|---|---|---|
YYYY | 四位年份 | HH | 兩位小時,24 時制 |
YY | 兩位年份 | H | 小時,不補零 |
MM | 兩位月份 | mm | 兩位分鐘 |
M | 月份,不補零 | m | 分鐘,不補零 |
DD | 兩位日期 | ss | 兩位秒 |
D | 日期,不補零 | s | 秒,不補零 |
Z | 本地偏移 ±HH:mm | ZZ | 本地偏移 ±HHmm |
方括號裡的內容原樣輸出,例如 YYYY[年]M[月]D[日]。
Z / ZZ 解析顯式偏移,輸入 Z 表示 UTC;輸出使用該時刻的本地時間及本地偏移,UTC 環境輸出 +00:00 / +0000,不保留輸入偏移。[Z] / [ZZ] 仍只是字面量。偏移小時 00~23、分鐘 00~59;帶偏移格式的手動輸入和綁定字串嚴格匹配,非法日期、非法/缺失偏移及 Z/ZZ 形狀不匹配均不退回寬鬆解析。
x、X 必須是完整格式,不能和其他單位混用;[x]、YYYY[x] 裡的 x 仍是字面量。秒時間戳向下取整,-1 毫秒轉成 X 是 -1 秒;回讀只有秒精度。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker } from '@nscode/handy-ui'
const value = ref('2024-01-15')
</script>
<template>
<HuiDatePicker
v-model="value"
format="YYYY[年]M[月]D[日]"
value-format="YYYY-MM-DD"
style="width: 240px"
/>
</template>
顯示格式
下面幾個選擇器綁定的是同一個值,只是 format 不同。在任意一個裡改了日期,其他幾個跟著變。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(new Date(2024, 0, 5, 9, 8, 7))
const range = ref<[Date, Date] | null>([new Date(2024, 0, 5), new Date(2024, 0, 12)])
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">YYYY/MM/DD</span>
<HuiDatePicker v-model="value" format="YYYY/MM/DD" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYY[年]M[月]D[日]</span>
<HuiDatePicker v-model="value" format="YYYY[年]M[月]D[日]" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">DD.MM.YY</span>
<HuiDatePicker v-model="value" format="DD.MM.YY" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYY-MM-DD HH:mm</span>
<HuiDatePicker
v-model="value"
type="datetime"
format="YYYY-MM-DD HH:mm"
style="width: 240px"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YY-M-D H:m</span>
<HuiDatePicker v-model="value" type="datetime" format="YY-M-D H:m" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYY[年]MM[月]</span>
<HuiDatePicker v-model="value" type="month" format="YYYY[年]MM[月]" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYY[年度]</span>
<HuiDatePicker v-model="value" type="year" format="YYYY[年度]" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">範圍 YYYY/M/D</span>
<HuiDatePicker v-model="range" type="daterange" format="YYYY/M/D" style="width: 300px" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 144px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
月份、年份類型選完會把日期改成那個月(那年)的 1 日 0 點,所以在這兩個裡選了以後,上面幾個的日、時間也會變。
綁定值格式
valueFormat 決定 v-model 拿到的是什麼。右邊是目前的綁定值:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const asDate = ref<Date | null>(new Date(2024, 0, 15))
const asString = ref('2024-01-15')
const compact = ref('20240115')
const withTime = ref('2024/01/15 09:30:00')
const range = ref<[string, string] | null>(['2024-01-15', '2024-01-20'])
const show = (value: unknown) => {
if (value instanceof Date) return `Date:${value.toLocaleString()}`
return JSON.stringify(value)
}
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">不傳</span>
<HuiDatePicker v-model="asDate" style="width: 240px" />
<span class="row-value">{{ show(asDate) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYY-MM-DD</span>
<HuiDatePicker v-model="asString" value-format="YYYY-MM-DD" style="width: 240px" />
<span class="row-value">{{ show(asString) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYYMMDD</span>
<HuiDatePicker v-model="compact" value-format="YYYYMMDD" style="width: 240px" />
<span class="row-value">{{ show(compact) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYY/MM/DD HH:mm:ss</span>
<HuiDatePicker
v-model="withTime"
type="datetime"
value-format="YYYY/MM/DD HH:mm:ss"
style="width: 240px"
/>
<span class="row-value">{{ show(withTime) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">範圍 YYYY-MM-DD</span>
<HuiDatePicker
v-model="range"
type="daterange"
value-format="YYYY-MM-DD"
style="width: 300px"
/>
<span class="row-value">{{ show(range) }}</span>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 144px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.row-value {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
帶偏移的日期時間
下面兩個初值分別使用正偏移 +05:45 和負偏移 -0330,表示同一時刻。輸入框按本地時區回顯;重新選擇或手動輸入後,綁定值按本地偏移輸出。可在輸入框直接輸入帶 Z 的 UTC 時間;不要把 [Z] 字面量當作 UTC 標記。此能力只處理數值偏移,沒有指定 IANA 時區或保留原始偏移的配置。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
import type { DatePickerValue } from '@nscode/handy-ui'
const colon = ref<DatePickerValue>('2024-01-01T00:15:00+05:45')
const compact = ref<DatePickerValue>('2023-12-31T15:00:00-0330')
const resetUtc = () => {
colon.value = '2023-12-31T18:30:00Z'
compact.value = '2023-12-31T18:30:00Z'
}
const iso = (value: DatePickerValue) => {
if (value === null || Array.isArray(value)) return '未選擇'
const date = value instanceof Date ? value : new Date(value)
return Number.isNaN(date.getTime()) ? '無效日期' : date.toISOString()
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="resetUtc">回填同一時刻的 UTC 值</HuiButton>
<HuiSpace direction="vertical" fill>
<span>Z:±HH:mm</span>
<HuiDatePicker
v-model="colon"
type="datetime"
format="YYYY-MM-DDTHH:mm:ssZ"
value-format="YYYY-MM-DDTHH:mm:ssZ"
style="width: 100%; max-width: 360px"
clearable
/>
<code>綁定值:{{ colon }}</code>
<code>UTC 時刻:{{ iso(colon) }}</code>
</HuiSpace>
<HuiSpace direction="vertical" fill>
<span>ZZ:±HHmm</span>
<HuiDatePicker
v-model="compact"
type="datetime"
format="YYYY-MM-DDTHH:mm:ssZZ"
value-format="YYYY-MM-DDTHH:mm:ssZZ"
style="width: 100%; max-width: 360px"
clearable
/>
<code>綁定值:{{ compact }}</code>
<code>UTC 時刻:{{ iso(compact) }}</code>
</HuiSpace>
</HuiSpace>
</template>
數字時間戳
value-format="x" 綁定毫秒數字,value-format="X" 綁定秒數字。它們只改變綁定值,不改變輸入框的日期顯示和手動輸入格式。範圍兩端也是數字;清除後仍是 null。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
import type { DatePickerValue } from '@nscode/handy-ui'
const milliseconds = ref<DatePickerValue>(new Date(2024, 0, 15).getTime())
const seconds = ref<DatePickerValue>(Math.floor(new Date(2024, 0, 15, 9, 30).getTime() / 1000))
const range = ref<DatePickerValue>([
Math.floor(new Date(2024, 0, 15).getTime() / 1000),
Math.floor(new Date(2024, 0, 20).getTime() / 1000)
])
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span>x:毫秒數字</span>
<HuiDatePicker v-model="milliseconds" value-format="x" clearable />
<code>{{ typeof milliseconds }}:{{ milliseconds }}</code>
</HuiSpace>
<HuiSpace wrap>
<span>X:秒數字</span>
<HuiDatePicker v-model="seconds" type="datetime" value-format="X" clearable />
<code>{{ typeof seconds }}:{{ seconds }}</code>
</HuiSpace>
<HuiSpace wrap>
<span>X:秒數字範圍</span>
<HuiDatePicker v-model="range" type="daterange" value-format="X" clearable />
<code>{{ JSON.stringify(range) }}</code>
</HuiSpace>
</HuiSpace>
</template>
禁用日期和面板選項
pickerOptions 裡:
disabledDate(date)回傳true的日期不能選,手動輸入這樣的日期也會恢復原值。月份、年份格子要整月、整年都禁用時才禁用。cellClassName(date)回傳的類名加在日期格子上。firstDayOfWeek取 1 到 7,7 是週日,預設 7。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker } from '@nscode/handy-ui'
import type { DatePickerOptions } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
const pickerOptions: DatePickerOptions = {
disabledDate: (date) => date.getTime() < new Date().setHours(0, 0, 0, 0),
firstDayOfWeek: 1
}
</script>
<template>
<HuiDatePicker v-model="value" :picker-options="pickerOptions" style="width: 240px" />
</template>
禁用日期
disabledDate 收到的是格子那天的 0 點。各種類型都能用:日期類型禁用單個格子;範圍類型兩頭都不能落在禁用的日期上;月份、年份類型只有整月、整年都被禁用時格子才變灰(下面的“month”行,本月還有能選的日子,所以本月能選,下個月起不能選)。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
import type { DatePickerOptions } from '@nscode/handy-ui'
const today = new Date()
today.setHours(0, 0, 0, 0)
const DAY = 24 * 3600 * 1000
const beforeToday: DatePickerOptions = {
disabledDate: (date) => date.getTime() < today.getTime()
}
const weekend: DatePickerOptions = {
disabledDate: (date) => date.getDay() === 0 || date.getDay() === 6
}
const last30Days: DatePickerOptions = {
disabledDate: (date) =>
date.getTime() > today.getTime() || date.getTime() < today.getTime() - 29 * DAY
}
const afterToday: DatePickerOptions = {
disabledDate: (date) => date.getTime() > today.getTime()
}
const before2020: DatePickerOptions = {
disabledDate: (date) => date.getFullYear() < 2020
}
const value1 = ref<Date | null>(null)
const value2 = ref<Date | null>(null)
const value3 = ref<[Date, Date] | null>(null)
const value4 = ref<Date | null>(null)
const value5 = ref<Date | null>(null)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">今天以前不能選</span>
<HuiDatePicker v-model="value1" :picker-options="beforeToday" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">週末不能選</span>
<HuiDatePicker v-model="value2" :picker-options="weekend" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">只能選最近 30 天</span>
<HuiDatePicker
v-model="value3"
type="daterange"
:picker-options="last30Days"
style="width: 300px"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">month:今天以後</span>
<HuiDatePicker
v-model="value4"
type="month"
:picker-options="afterToday"
style="width: 240px"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">year:2020 年以前</span>
<HuiDatePicker
v-model="value5"
type="year"
:picker-options="before2020"
style="width: 240px"
/>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 120px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
在第一行的輸入框裡輸入一個過去的日期再按 Enter,輸入框會恢復原來的值。
每週第一天
firstDayOfWeek 決定日期面板的星期從哪天排起:1 到 6 是週一到週六,7 是週日(預設)。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">週一(1)</span>
<HuiDatePicker v-model="value" :picker-options="{ firstDayOfWeek: 1 }" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">週六(6)</span>
<HuiDatePicker v-model="value" :picker-options="{ firstDayOfWeek: 6 }" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">週日(7,預設)</span>
<HuiDatePicker v-model="value" style="width: 240px" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 104px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
單元格類名和面板類名
cellClassName 給日期格子加類名,popperClass 給整個下拉面板加類名。面板掛在 body 下,元件裡的 <style scoped> 管不到,樣式要寫在不帶 scoped 的樣式裡,並用 popperClass 限定範圍,免得影響頁面上別的日期選擇器。下面把週末標成紅色,每月 1 日、15 日下面加一個圓點:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker } from '@nscode/handy-ui'
import type { DatePickerOptions } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
const pickerOptions: DatePickerOptions = {
cellClassName: (date) => {
const classes: string[] = []
if (date.getDay() === 0 || date.getDay() === 6) classes.push('is-weekend')
if (date.getDate() === 1 || date.getDate() === 15) classes.push('is-marked')
return classes.join(' ')
}
}
</script>
<template>
<HuiDatePicker
v-model="value"
:picker-options="pickerOptions"
popper-class="demo-marked-panel"
style="width: 240px"
/>
</template>
<style>
/* 只改本月的日期,选中的日期保持白字 */
.demo-marked-panel
.hui-date-picker-panel__cell--normal.is-weekend:not(.hui-date-picker-panel__cell--selected) {
color: var(--hui-color-danger);
}
.demo-marked-panel .hui-date-picker-panel__cell.is-marked {
position: relative;
}
.demo-marked-panel .hui-date-picker-panel__cell.is-marked::after {
content: '';
position: absolute;
bottom: 3px;
left: 50%;
width: 4px;
height: 4px;
margin-left: -2px;
border-radius: 50%;
background-color: var(--hui-color-warning);
}
</style>
快捷選項
pickerOptions.shortcuts 顯示在面板旁邊,有兩種寫法:
{ text, value }:點了直接選value。範圍類型的value要是[Date, Date]。{ text, onClick(picker) }:點了調onClick,picker是選擇器實例,在裡面調picker.pick(...)。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker } from '@nscode/handy-ui'
import type { DatePickerOptions } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
const pickerOptions: DatePickerOptions = {
// 類型是 DatePickerShortcut[] | DatePickerOption[],同一個陣列裡不能混用兩種寫法
shortcuts: [
{ text: '今天', onClick: (picker) => picker.pick(new Date()) },
{
text: '一週前',
onClick: (picker) => picker.pick(new Date(Date.now() - 7 * 24 * 3600 * 1000))
}
]
}
</script>
<template>
<HuiDatePicker v-model="value" :picker-options="pickerOptions" style="width: 240px" />
</template>
{ text, value } 寫法,日期類型和範圍類型各一個。value 在定義時就算好了,適合固定的日期;要按點擊時的時間計算(比如頁面開了一整天),用上面的 onClick 寫法。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
import type { DatePickerOptions } from '@nscode/handy-ui'
const DAY = 24 * 3600 * 1000
const today = new Date()
today.setHours(0, 0, 0, 0)
const date = ref<Date | null>(null)
const range = ref<[Date, Date] | null>(null)
const dateOptions: DatePickerOptions = {
shortcuts: [
{ text: '昨天', value: new Date(today.getTime() - DAY) },
{ text: '本月 1 日', value: new Date(today.getFullYear(), today.getMonth(), 1) },
{ text: '今年元旦', value: new Date(today.getFullYear(), 0, 1) }
]
}
const rangeOptions: DatePickerOptions = {
shortcuts: [
{ text: '最近一週', value: [new Date(today.getTime() - 6 * DAY), today] },
{ text: '最近一個月', value: [new Date(today.getTime() - 29 * DAY), today] },
{ text: '最近三個月', value: [new Date(today.getTime() - 89 * DAY), today] }
]
}
</script>
<template>
<HuiSpace wrap>
<HuiDatePicker v-model="date" :picker-options="dateOptions" style="width: 240px" />
<HuiDatePicker
v-model="range"
type="daterange"
:picker-options="rangeOptions"
style="width: 300px"
/>
</HuiSpace>
</template>
預設月份
沒有值時面板顯示目前月份。defaultValue 可以換成別的月份,範圍類型可以傳兩個,分別給左右面板(兩個月不相鄰時加上 unlinkPanels,否則右邊固定是左邊的下個月)。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
const range = ref<[Date, Date] | null>(null)
const rangeDefault: [Date, Date] = [new Date(2020, 0, 1), new Date(2020, 5, 1)]
</script>
<template>
<HuiSpace wrap>
<HuiDatePicker v-model="value" :default-value="new Date(2020, 5, 1)" style="width: 240px" />
<HuiDatePicker
v-model="range"
type="daterange"
:default-value="rangeDefault"
unlink-panels
style="width: 300px"
/>
</HuiSpace>
</template>
可清除
clearable 後,有值時在右邊顯示清除按鈕(禁用、唯讀時不顯示)。點了依次發 update:modelValue(null)、clear、change(null)。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(new Date())
const range = ref<[Date, Date] | null>([new Date(), new Date(Date.now() + 7 * 24 * 3600 * 1000)])
const clearCount = ref(0)
</script>
<template>
<HuiSpace wrap>
<HuiDatePicker v-model="value" clearable style="width: 240px" @clear="clearCount++" />
<HuiDatePicker
v-model="range"
type="daterange"
clearable
style="width: 300px"
@clear="clearCount++"
/>
</HuiSpace>
<p style="margin-top: 12px">已清除 {{ clearCount }} 次</p>
</template>
手動輸入
預設可以在輸入框裡輸入,按 Enter 或失焦時生效。普通無偏移格式先按 format 解析,再認 2024-1-5、2024/1/5 這類常見寫法;帶 Z/ZZ 的格式嚴格解析,失敗後不退回常見寫法。認不出來、是禁用日期時恢復原值。生效後面板翻到那個月。
editable 設成 false 後不能輸入,點輸入區打開面板,聚焦後按空白鍵開關面板。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">可以輸入(預設)</span>
<HuiDatePicker v-model="value" placeholder="試試輸入 2024/1/5" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">editable=false</span>
<HuiDatePicker v-model="value" :editable="false" style="width: 240px" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 120px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
尺寸
size 可選 large、default、small,輸入框和同尺寸的 Input、Button 一致:高度 40 / 32 / 24px,字級 16 / 14 / 12px,圓角 8 / 6 / 4px,日曆、清除、箭頭圖示 18 / 16 / 14px。各種 type 都一樣,範圍選擇的兩個日期和中間的分隔符也跟著變。日期面板不隨尺寸變化。不寫時依次跟隨所在的 HuiForm / HuiFormItem、外層 HuiConfigProvider 的 size,都沒有時是 default。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(new Date())
const range = ref<[Date, Date] | null>([new Date(), new Date(Date.now() + 7 * 24 * 3600 * 1000)])
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">large</span>
<HuiDatePicker v-model="value" size="large" clearable style="width: 220px" />
<HuiDatePicker v-model="range" type="daterange" size="large" style="width: 320px" />
<HuiButton size="large">按鈕</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">default</span>
<HuiDatePicker v-model="value" clearable style="width: 220px" />
<HuiDatePicker v-model="range" type="daterange" style="width: 320px" />
<HuiButton>按鈕</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">small</span>
<HuiDatePicker v-model="value" size="small" clearable style="width: 220px" />
<HuiDatePicker v-model="range" type="daterange" size="small" style="width: 320px" />
<HuiButton size="small">按鈕</HuiButton>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
禁用和唯讀
disabled 和 readonly 都不能打開面板、不能輸入,鍵盤也不響應。禁用時用禁用色和禁用底色顯示;唯讀時顏色不變,只是滑鼠指標是普通箭頭、懸停時邊框不變色。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(new Date())
const range = ref<[Date, Date] | null>([new Date(), new Date(Date.now() + 7 * 24 * 3600 * 1000)])
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">disabled</span>
<HuiDatePicker v-model="value" disabled style="width: 240px" />
<HuiDatePicker v-model="range" type="daterange" disabled style="width: 300px" />
<HuiDatePicker type="date" disabled placeholder="沒有值" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">readonly</span>
<HuiDatePicker v-model="value" readonly style="width: 240px" />
<HuiDatePicker v-model="range" type="daterange" readonly style="width: 300px" />
<HuiDatePicker type="date" readonly placeholder="沒有值" style="width: 240px" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 64px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
自訂圖示
prefixIcon 預設是 'calendar',傳空字串不顯示前綴圖示。suffixIcon 預設不顯示,顯示在下拉箭頭前面。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">預設</span>
<HuiDatePicker v-model="value" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">不要前綴</span>
<HuiDatePicker v-model="value" prefix-icon="" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">換前綴</span>
<HuiDatePicker v-model="value" type="datetime" prefix-icon="clock" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">只要後綴</span>
<HuiDatePicker v-model="value" prefix-icon="" suffix-icon="calendar" style="width: 240px" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 64px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
事件
操作下面的選擇器,最近觸發的事件按時間倒序列在下面:選日期、改時間觸發 change,打開、關閉面板觸發 visibleChange,翻頁、選年月觸發 panelChange,點清除按鈕觸發 clear。模板裡寫成 @visible-change、@panel-change。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker } from '@nscode/handy-ui'
import type { DatePickerValue } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
const logs = ref<string[]>([])
const log = (text: string) => {
logs.value = [text, ...logs.value].slice(0, 6)
}
const onChange = (val: DatePickerValue) => {
log(`change:${val instanceof Date ? val.toLocaleString() : String(val)}`)
}
const onVisibleChange = (visible: boolean) => log(`visibleChange:${visible}`)
const onPanelChange = (date: Date, mode: string, view: string) => {
log(
`panelChange:${date.getFullYear()} 年 ${date.getMonth() + 1} 月,mode=${mode},view=${view}`
)
}
</script>
<template>
<HuiDatePicker
v-model="value"
type="datetime"
clearable
style="width: 240px"
@change="onChange"
@focus="log('focus')"
@blur="log('blur')"
@clear="log('clear')"
@visible-change="onVisibleChange"
@panel-change="onPanelChange"
/>
<div class="event-log">
<div v-for="(item, index) in logs" :key="index">{{ item }}</div>
<div v-if="!logs.length">還沒有觸發事件</div>
</div>
</template>
<style scoped>
.event-log {
margin-top: 12px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
line-height: 1.8;
}
</style>
方法
透過 DatePickerExpose 類型的 ref 呼叫 focus()、blur()、open()、close()、clear()、pick()。pick() 和點日期一樣發出 update:modelValue、change,不帶時間的類型選完關閉面板。clear() 與清除圖示共用入口,單值和範圍都請求 null,順序為 update:modelValue → clear → change;clearable 只控制圖示展示,方法不受該開關限制。禁用、唯讀或卸載後清除無效。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
import type { DatePickerExpose } from '@nscode/handy-ui'
const pickerRef = ref<DatePickerExpose>()
const value = ref<Date | null>(null)
// 打開面板,3 秒後自動關閉
const openForAWhile = () => {
pickerRef.value?.open()
setTimeout(() => pickerRef.value?.close(), 3000)
}
const pickNewYear = () => {
pickerRef.value?.pick(new Date(new Date().getFullYear() + 1, 0, 1))
}
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiDatePicker ref="pickerRef" v-model="value" clearable style="width: 240px" />
<HuiSpace wrap>
<HuiButton @click="pickerRef?.focus()">聚焦(focus)</HuiButton>
<HuiButton @click="openForAWhile">打開 3 秒(open / close)</HuiButton>
<HuiButton @click="pickerRef?.close()">關閉(close)</HuiButton>
<HuiButton @click="pickerRef?.clear()">清除(clear,發出 null)</HuiButton>
<HuiButton @click="pickNewYear">選中明年元旦(pick)</HuiButton>
</HuiSpace>
<p>JSON:{{ JSON.stringify({ date: value }) }}</p>
</HuiSpace>
</template>
鍵盤操作
焦點在輸入框上時:
| 按鍵 | 作用 |
|---|---|
Enter | 打開、關閉面板。輸入框裡改過文字時,讓輸入生效並關閉 |
↓ | 面板沒打開時打開;已經打開時焦點進入面板,落在選中的日期上(沒有值時是今天) |
Esc | 關閉面板 |
Tab | 關閉面板,焦點照常移到頁面上的下一個元素 |
空格 | editable 為 false 時打開、關閉面板 |
焦點在面板裡時:
| 按鍵 | 作用 |
|---|---|
← → | 日期格子裡移一天,月份、年份格子裡移一格 |
↑ ↓ | 日期格子裡移一週,月份、年份格子裡移一行(4 格) |
Enter、空格 | 選中焦點所在的格子,或者按下焦點所在的按鈕(翻頁箭頭、年月標題、今天、快捷選項) |
Esc | 關閉面板,焦點回到輸入框 |
Tab | 在翻頁箭頭、年月標題、格子區域、時間輸入框、“今天”、快捷選項之間移動。從最後一個(Shift+Tab 時是第一個)移出時關閉面板,焦點回到輸入框 |
- 方向鍵只移動焦點,不改值,按
Enter才選中。日期格子移到別的月份時面板跟著翻頁,月份、年份格子在目前這一頁裡移動(跳過禁用的格子),翻頁用頂部的按鈕。 - 每個格子區域只占一個
Tab位置,Tab進來時落在上次移到的格子、選中的格子或今天(本月、本年)。 - 禁用的日期也能移過去,按
Enter沒有反應;整月、整年都禁用的月份、年份格子跳過。 - 選完面板關閉時焦點回到輸入框。帶時間的類型、範圍類型選第一個日期時面板不關,焦點留在選中的格子上。
- 範圍類型選了開始日期以後,用方向鍵移到別的日期時也會預覽將要選中的範圍。
- 輸入框是
role="combobox",帶aria-haspopup="dialog"和aria-expanded;面板是role="dialog",名稱取占位文字,開啟過以後輸入框用aria-controls指向它。editable為false時這幾個屬性在role="button"的外框上。
自持與受控日期
未傳modelValue或只監聽更新時,從空值開始自行保存日期。傳入屬性即受控,包括顯式undefined;顯示、面板選中和表單change驗證讀取父實際值。update:modelValue與change報告本次請求,父拒絕或正規化後不把候選當成已接受值;範圍首端、輸入文字和時間編輯另存草稿。清除統一請求null,父拒絕時保留實際日期。移除屬性恢復此前內部值。
<script setup lang="ts">
import { computed, ref } from 'vue'
import { HuiButton, HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
import type { DatePickerExpose, DatePickerType, DatePickerValue } from '@nscode/handy-ui'
const types: DatePickerType[] = [
'year',
'month',
'date',
'datetime',
'daterange',
'datetimerange'
]
const type = ref<DatePickerType>('date')
const redraw = ref(0)
const local = ref<DatePickerExpose>()
const last = ref('尚無請求')
const value = ref<DatePickerValue>(null)
const fixed = computed<DatePickerValue>(() =>
type.value.endsWith('range')
? [new Date(2026, 10, 20), new Date(2026, 10, 21)]
: new Date(2026, 10, 20)
)
const showRequest = (next: DatePickerValue) => {
last.value = JSON.stringify(next)
}
</script>
<template>
<HuiSpace class="date-picker-q1-demo" direction="vertical" fill>
<HuiSpace wrap>
<HuiButton
v-for="item in types"
:key="item"
size="small"
:type="type === item ? 'primary' : 'default'"
@click="type = item"
>{{ item }}</HuiButton
>
<HuiButton class="date-redraw" @click="redraw++">父重新呈現 {{ redraw }}</HuiButton>
<HuiButton class="date-clear-local" @click="local?.clear()">清除第一項</HuiButton>
</HuiSpace>
<HuiSpace wrap :key="type">
<div class="date-local">
<p>未傳,自行保存</p>
<HuiDatePicker ref="local" :type="type" clearable />
</div>
<div class="date-listener">
<p>僅監聽更新</p>
<HuiDatePicker :type="type" clearable @update:model-value="showRequest" />
</div>
<div class="date-empty">
<p>顯式 undefined,保持空</p>
<HuiDatePicker
:type="type"
:model-value="undefined"
clearable
@update:model-value="showRequest"
/>
</div>
<div class="date-fixed">
<p>固定父值,拒絕更新</p>
<HuiDatePicker
:type="type"
:model-value="fixed"
clearable
@update:model-value="showRequest"
/>
</div>
<div class="date-bound">
<p>雙向綁定</p>
<HuiDatePicker v-model="value" :type="type" clearable />
</div>
</HuiSpace>
<p class="date-status">綁定值:{{ JSON.stringify(value) }};最近請求:{{ last }}</p>
</HuiSpace>
</template>
Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| modelValue | DatePickerValue | undefined | 未傳或僅監聽從null自持;傳入屬性即受控(含undefined),顯示和驗證讀父實際值。 |
| type | 'year' | 'month' | 'date' | 'datetime' | 'daterange' | 'datetimerange' | 'date' | 選擇器類型 |
| size | 'large' | 'default' | 'small' | — | 尺寸。不寫時依次取表單、HuiConfigProvider 的 size,都沒有時是 'default' |
| placeholder | string | 隨類型和語言 | 提示文字。不傳時按類型:選擇年份 / 選擇月份 / 選擇日期 / 選擇日期時間 |
| startPlaceholder | string | — | 範圍類型開始一側的提示文字,不傳時是“開始日期”(datetimerange 是“開始時間”) |
| endPlaceholder | string | — | 範圍類型結束一側的提示文字,不傳時是“結束日期”(datetimerange 是“結束時間”) |
| disabled | boolean | false | 是否禁用 |
| readonly | boolean | false | 是否唯讀 |
| editable | boolean | true | 輸入框能不能手動輸入 |
| clearable | boolean | false | 是否顯示清除按鈕 |
| format | string | — | 顯示格式,也用來解析手動輸入。不傳時按類型取預設格式 |
| valueFormat | string | — | 不傳發 Date;x 發毫秒數字,X 發秒數字,其他格式發字串 |
| pickerOptions | DatePickerOptions | — | 禁用日期、快捷選項、單元格類名、每週第一天 |
| popperClass | string | — | 加在下拉面板上的類名 |
| rangeSeparator | string | 隨語言(中文:至) | 範圍類型的分隔符 |
| defaultValue | Date | [Date, Date] | — | 沒有值時面板預設顯示的月份 |
| defaultTime | Date | [Date, Date] | — | 帶時間的類型選中日期時用的時分秒,範圍類型傳兩個 |
| unlinkPanels | boolean | false | 範圍類型左右面板各自翻頁 |
| prefixIcon | string | 'calendar' | 前綴圖示名,傳空字串不顯示 |
| suffixIcon | string | — | 後綴圖示名 |
| validateEvent | boolean | true | 值變化或整個控制項失焦時驗證;內部移焦、關閉面板後回到觸發器不觸發失焦驗證 |
DatePickerOptions
| 屬性 | 類型 | 說明 |
|---|---|---|
| disabledDate | (date: Date) => boolean | 回傳 true 的日期不能選,對手動輸入也生效 |
| shortcuts | DatePickerShortcut[] | DatePickerOption[] | 快捷選項,寫法見上文“快捷選項” |
| cellClassName | (date: Date) => string | 給日期格子加類名 |
| firstDayOfWeek | number | 每週第一天,1 到 7,7 是週日,預設 7 |
Events
| 事件 | 回呼參數 | 說明 |
|---|---|---|
| update:modelValue | (value: DatePickerValue) | 點日期、改時間、手動輸入生效、點快捷選項、清除時觸發,v-model 用的就是它 |
| change | (value: DatePickerValue) | 報告本次日期/時間或清除請求;不代表父元件已接受,顯示和驗證取實際值 |
| focus | (event: FocusEvent) | 焦點從控制項外進入觸發器或面板時觸發;內部移焦不重複觸發 |
| blur | (event: FocusEvent) | 焦點離開觸發器和面板組成的整個控制項時觸發;內部移焦、關閉面板後回到觸發器不觸發 |
| clear | — | 點清除按鈕後觸發 |
| visibleChange | (visible: boolean) | 面板真的打開或關閉時觸發,狀態沒變不觸發 |
| panelChange | (value: Date, mode: 'year' | 'month', view: 'year' | 'month' | 'date') | 翻頁、選年、選月後觸發。value 是面板顯示月份的 1 日,mode 是變的是年還是月,view 是面板目前的視圖 |
Slots
沒有插槽。
公開方法
| 方法 | 簽名 | 說明 |
|---|---|---|
| focus | () => void | 讓實際觸發器獲得焦點,不打開面板;面板內呼叫時回到觸發器 |
| blur | () => void | 讓目前位於觸發器或面板內的焦點離開整個控制項 |
| open | () => void | 打開面板,禁用、唯讀時不打開 |
| close | () => void | 關閉面板 |
| clear | () => void | 請求清除為 null;禁用、唯讀或卸載後無效,不受 clearable 圖示開關限制 |
| pick | (value: Date | [Date, Date]) => void | 直接選中一個值。範圍類型必須傳陣列,類型不符時不處理;不帶時間的類型選完會關閉面板 |
類型
公開類型使用元件前綴。相容映射:DateCell → DatePickerCell;PickerOption → DatePickerOption;PickerOptions → DatePickerOptions;PickerShortcut → DatePickerShortcut。舊名仍可從 @nscode/handy-ui 匯入,已標記 @deprecated,將在 1.0 刪除。
import type {
DatePickerType,
DatePickerValue,
DatePickerOptions,
DatePickerShortcut,
DatePickerOption,
DatePickerExpose
} from '@nscode/handy-ui'
type DatePickerType = 'year' | 'month' | 'date' | 'datetime' | 'daterange' | 'datetimerange'
type DatePickerValue =
| Date
| string
| number
| [Date | string | number, Date | string | number]
| null
interface DatePickerOptions {
disabledDate?: (date: Date) => boolean
shortcuts?: DatePickerShortcut[] | DatePickerOption[]
cellClassName?: (date: Date) => string
firstDayOfWeek?: number
}
interface DatePickerShortcut {
text: string
value: Date | [Date, Date]
}
interface DatePickerOption {
text: string
onClick: (picker: DatePickerExpose) => void
}
interface DatePickerExpose {
focus: () => void
blur: () => void
open: () => void
close: () => void
clear: () => void
pick: (date: Date | [Date, Date]) => void
}
/** @deprecated 請使用DatePickerExpose,將在1.0移除。 */
type DatePickerInstance = DatePickerExpose