Calendar 日历

示例

基础用法

<Calendar />

周一为起始日

<Calendar weekStartsOn="monday" />

单选

<Calendar mode="single" />

范围选择

<Calendar mode="range" />

限制范围

<Calendar
  mode="range"
  min={dayjs().add(-1, 'year').toDate()}
  max={dayjs().add(1, 'year').toDate()}
/>

自定义

<Calendar mode="single">
  {#snippet header(year, month, add)}
    <div class="flex items-center justify-stretch">
      <Button
        class="h-auto p-2 text-sm text-blue-800"
        color="text"
        onclick={() => add(-1, 'year')}>上一年</Button
      >
      <Button
        class="h-auto p-2 text-sm text-blue-600"
        color="text"
        onclick={() => add(-1, 'month')}>上一月</Button
      >
      <span class="flex-1 text-center">{year}-{month + 1}</span>
      <Button
        class="h-auto p-2 text-sm text-blue-600"
        color="text"
        onclick={() => add(1, 'month')}>下一月</Button
      >
      <Button
        class="h-auto p-2 text-sm text-blue-800"
        color="text"
        onclick={() => add(1, 'year')}>下一年</Button
      >
    </div>
  {/snippet}
  {#snippet cell(date: Date)}
    <div class="flex flex-col items-center">
      <span>
        {date.getDate()}
      </span>
      <span class="text-xs">
        {#if dayjs().isSame(date, 'day')}
          今天
        {:else if dayjs(date).day() === 0}
          周日
        {:else if dayjs(date).day() === 6}
          周六
        {:else}
          &nbsp;
        {/if}
      </span>
    </div>
  {/snippet}
</Calendar>

Calendar

属性

属性 说明 类型 默认值
value 选中的日期 [CalendarValue]() $bindable()
mode 选择模式 'single' | 'range' 'single'
page 选中的年月 [year: number, month: number] 当前年月
max 最大值 Date undefined
min 最小值 Date undefined
weekStartsOn 每周以周几作为第一天 'sunday' | 'monday' 'sunday'
header 自定义头部 [CalendarHeader]() undefined
cell 自定义日期单元格 Snippet<[date: Date]> undefined

类型定义

CalendarValue

/** mode = 'single' */
type CalendarSingleValue = Date | undefined
/** mode = 'range' */
type CalendarRangeValue = [begin?: Date, end?: Date]
/** CalendarValue */
type CalendarValue = CalendarSingleValue | CalendarRangeValue

CalendarHeader

// CalendarHeader
Snippet<
  [
    /** 年 */
    year: number,
    /** 月 */
    month: number,
    /** 增加 */
    add: (value: number, unit: 'year' | 'month') => void,
    /** 判定按钮是否禁用 */
    isDisabled: (
      button: 'prev-year' | 'prev-month' | 'next-month' | 'next-year'
    ) => boolean,
  ]
>

事件

事件 说明 类型
onChange 变更事件 (value?: Date | [begin?: Date, end?: Date]) => void
onPageChange 年月变更事件 ([year: number, month: number]) => void