Skip to content

Calendar 日历

介绍

日历用于选择单个、多个日期或日期范围。

引入

ts
import { IBestCalendar, IBestCalendarDialog, CalendarConfirmResultType } from "@ibestservices/ibest-ui";

代码演示

选择单个日期

选择单个日期

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  @State visible: boolean = false
  @State selectDate: string = "请选择日期"
  onDialogConfirm(value: Array<CalendarConfirmResultType>): void {
    console.log("onConfirm", JSON.stringify(value))
    this.selectDate = value.map(item => item.dateStr).join(",")
  }
  build() {
    Column(){
      IBestCell({
        title: '选择单个日期',
        value: this.selectDate,
        onClickCell: () => {
          this.visible = true
        }
      })
      IBestCalendarDialog({
        visible: $visible,
        onConfirm: (value: Array<CalendarConfirmResultType>): void => this.onDialogConfirm(value)
      })
    }
  }
}

选择多个日期

选择多个日期

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  @State visible: boolean = false
  @State selectDate: string = "请选择日期"
  onDialogConfirm(value: Array<CalendarConfirmResultType>): void {
    console.log("onConfirm", JSON.stringify(value)) 
    this.selectDate = value.map(item => item.dateStr).join(",")
  }
  build() {
    Column(){
      IBestCell({
        title: '选择多个日期',
        value: this.selectDate,
        onClickCell: () => {
          this.visible = true
        }
      })
      IBestCalendarDialog({
        visible: $visible,
        type: "danger",
        selectType: "multiple",
        onConfirm: (value: Array<CalendarConfirmResultType>): void => this.onDialogConfirm(value)
      })
    }
  }
}

选择日期区间

日期区间

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  @State visible: boolean = false
  @State selectDate: string = "请选择日期"
  onDialogConfirm(value: Array<CalendarConfirmResultType>): void {
    console.log("onConfirm", JSON.stringify(value)) 
    this.selectDate = value.map(item => item.dateStr).join(",")
  }
  build() {
    Column(){
      IBestCell({
        title: '选择日期区间',
        value: this.selectDate,
        onClickCell: () => {
          this.visible = true
        }
      })
      IBestCalendarDialog({
        visible: $visible,
        selectType: "range",
        onConfirm: (value: Array<CalendarConfirmResultType>): void => this.onDialogConfirm(value)
      })
    }
  }
}

自定义选中样式

自定义选中样式

TIP

通过 type 属性可修改选中样式,支持 primarysuccesswarningdanger 四种选中类型,默认为 primary,也可通过selectedColor属性自定义选中颜色,同时设置时selectedColor优先。

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  @State visible: boolean = false
  @State selectDate: string = "请选择日期"
  onDialogConfirm(value: Array<CalendarConfirmResultType>): void {
    console.log("onConfirm", JSON.stringify(value)) 
    this.selectDate = value.map(item => item.dateStr).join(",")
  }
  build() {
    Column(){
      IBestCell({
        title: '选择日期',
        value: this.selectDate,
        onClickCell: () => {
          this.visible = true
        }
      })
      IBestCalendarDialog({
        visible: $visible,
        type: "danger",
        selectedColor: "#58db6b",
        onConfirm: (value: Array<CalendarConfirmResultType>): void => this.onDialogConfirm(value)
      })
    }
  }
}

显示农历

显示农历

TIP

通过 isShowLunar 属性可设置显示农历,通过 cornerRadius 属性可设置弹框圆角。

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  @State visible: boolean = false
  @State selectDate: string = "请选择日期"
  onDialogConfirm(value: Array<CalendarConfirmResultType>): void {
    console.log("onConfirm", JSON.stringify(value)) 
    this.selectDate = value.map(item => item.dateStr).join(",")
  }
  build() {
    Column(){
      IBestCell({
        title: '选择日期',
        value: this.selectDate,
        onClickCell: () => {
          this.visible = true
        }
      })
      IBestCalendarDialog({
        visible: $visible,
        isShowLunar: true,
        cornerRadius: 20,
        onConfirm: (value: Array<CalendarConfirmResultType>): void => this.onDialogConfirm(value)
      })
    }
  }
}

自定义日期范围

自定义日期范围

TIP

通过 minDate 属性可设置最小日期,maxDate 属性可设置最大日期。

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  @State visible: boolean = false
  @State selectDate: string = "请选择日期"
  onDialogConfirm(value: Array<CalendarConfirmResultType>): void {
    console.log("onConfirm", JSON.stringify(value)) 
    this.selectDate = value.map(item => item.dateStr).join(",")
  }
  build() {
    Column(){
      IBestCell({
        title: '选择日期',
        value: this.selectDate,
        onClickCell: () => {
          this.visible = true
        }
      })
      IBestCalendarDialog({
        visible: $visible,
        selectType: "multiple",
        selectedStyleType: "circle",
        minDate: new Date(),
        maxDate: new Date("2025-01-01"),
        confirmBtnColor: "#ed4040",
        confirmBtnText: "完成",
        onConfirm: (value: Array<CalendarConfirmResultType>): void => this.onDialogConfirm(value)
      })
    }
  }
}

区间最大范围

区间最大范围

TIP

通过 maxRange 属性可设置区间最大范围。

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  @State visible: boolean = false
  @State selectDate: string = "请选择日期"
  onDialogConfirm(value: Array<CalendarConfirmResultType>): void {
    console.log("onConfirm", JSON.stringify(value)) 
    this.selectDate = value.map(item => item.dateStr).join(",")
  }
  build() {
    Column(){
      IBestCell({
        title: '选择日期区间',
        value: this.selectDate,
        onClickCell: () => {
          this.visible = true
        }
      })
      IBestCalendarDialog({
        visible: $visible,
        type: "danger",
        selectType: "range",
        maxRange: 7,
        onConfirm: (value: Array<CalendarConfirmResultType>): void => this.onDialogConfirm(value)
      })
    }
  }
}

自定义周起始日期

自定义周起始日期

TIP

通过 weekFirstDay 属性可以自定义周起始日期。

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  @State visible: boolean = false
  @State selectDate: string = "请选择日期"
  onDialogConfirm(value: Array<CalendarConfirmResultType>): void {
    console.log("onConfirm", JSON.stringify(value)) 
    this.selectDate = value.map(item => item.dateStr).join(",")
  }
  build() {
    Column(){
      IBestCell({
        title: '选择日期',
        value: this.selectDate,
        onClickCell: () => {
          this.visible = true
        }
      })
      IBestCalendarDialog({
        visible: $visible,
        weekFirstDay: "日",
        onConfirm: (value: Array<CalendarConfirmResultType>): void => this.onDialogConfirm(value)
      })
    }
  }
}

显示月份背景水印

显示月份背景水印

TIP

通过 isShowMark 属性可以显示月份背景水印。

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  @State visible: boolean = false
  @State selectDate: string = "请选择日期"
  onDialogConfirm(value: Array<CalendarConfirmResultType>): void {
    console.log("onConfirm", JSON.stringify(value)) 
    this.selectDate = value.map(item => item.dateStr).join(",")
  }
  build() {
    Column(){
      IBestCell({
        title: '选择日期',
        value: this.selectDate,
        onClickCell: () => {
          this.visible = true
        }
      })
      IBestCalendarDialog({
        visible: $visible,
        weekFirstDay: "日",
        onConfirm: (value: Array<CalendarConfirmResultType>): void => this.onDialogConfirm(value)
      })
    }
  }
}

平铺展示

平铺展示

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  build() {
    Column(){
      IBestCalendar({
        isShowLunar: true,
        weekFirstDay: "日",
        selectedStyleType: "normal",
        selectType: "multiple",
        selectedColor: "#ed4040",
        isShowMark: true,
        onConfirm: (value: Array<CalendarConfirmResultType>) => {
            let text = value.map(item => item.dateStr).join(",")
            promptAction.showDialog({
                message: `当前日期为:${text}`,
                alignment: DialogAlignment.Center
            })
        }
      })
    }
  }
}

API

Calendar @Props

参数说明类型默认值
type主题类型,可选值为 primary success warning dangerstringprimary
defaultSelectedDate默认选中日期Array[string][]
weekFirstDay周起始日期string
isShowLunar是否显示农历booleanfalse
selectType选择类型,可选值为 single multiple rangestringsingle
selectedStyleType选中样式,可选值为 normal circlestringnormal
selectedColor自定义选中颜色string
maxRangeselectTyperange 时,最多可选天数,-1为无限制number-1
minDate最小可选日期Date
maxDate最大可选日期Date
isShowMark是否显示月份背景水印booleanfalse
isShowHeader是否显示头部booleantrue
isShowConfirmBtn是否显示底部确定按钮booleanfalse
confirmBtnColor确认按钮颜色stringtype主题色
confirmBtnText确认按钮文案string确认

CalendarDialog @Props

TIP

CalendarDialog包含Calendar除 isShowConfirmBtn 以外所有属性,CalendarDialog的 isShowConfirmBtn 属性默认为selectType不为single时显示,以下仅列Calendar不包含的属性

参数说明类型默认值
visible弹框展示状态booleanfalse
isModal是否为模态窗口booleantrue
offsetY弹框底部偏移量number0

Events

事件名说明回调参数
onConfirm选择日期后的回调,selectTypemultiple 时需点击按钮触发value: Array[CalendarConfirmResultType]