下面演示了结合单元格来使用日历组件的用法,日期选择完成后会触发 onConfirm 事件。
function formatFullDate(date?: Date) {
  if (date) {
    return `${date.getFullYear()}/${formatDate(date)}`
  }
}
function SingleCalendar() {
  const [open, setOpen] = useState(false)
  const [value, setValue] = useState<Date>()
  const [formatValue, setFormatValue] = useState<string>()
  return (
    <>
      <Cell title="选择单个日期" isLink children={formatValue} onClick={() => setOpen(true)} />
      <Calendar
        type="single"
        value={value}
        poppable
        showPopup={open}
        onClose={setOpen}
        onChange={setValue}
        onConfirm={(newValue) => {
          setOpen(false)
          setFormatValue(formatFullDate(newValue))
        }}
      ></Calendar>
    </>
  )
}设置 type 为 multiple 后可以选择多个日期,此时 onConfirm 事件处理 value 数组结构,数组包含若干个选中的日期。
function formatMultiple(dates: Date[]) {
  if (dates.length) {
    return `选择了 ${dates.length} 个日期`
  }
}
function MultipleCalendar() {
  const [open, setOpen] = useState(false)
  const [value, setValue] = useState<Date[]>()
  const [formatValue, setFormatValue] = useState<string>()
  return (
    <>
      <Cell title="选择多个日期" isLink children={formatValue} onClick={() => setOpen(true)} />
      <Calendar
        type="multiple"
        value={value}
        poppable
        showPopup={open}
        onClose={setOpen}
        onChange={setValue}
        onConfirm={(newValue) => {
          setFormatValue(formatMultiple(newValue))
          setOpen(false)
        }}
      ></Calendar>
    </>
  )
}设置 type 为 range 后可以选择日期区间,此时 onConfirm 事件处理 value 数组结构,数组第一项为开始时间,第二项为结束时间。
function formatRange(dateRange: Date[]) {
  if (dateRange.length) {
    const [start, end] = dateRange
    return `${formatDate(start)} - ${formatDate(end)}`
  }
}
function RangeCalendar() {
  const [open, setOpen] = useState(false)
  const [value, setValue] = useState<Date[]>()
  const [formatValue, setFormatValue] = useState<string>()
  return (
    <>
      <Cell title="选择日期区间" isLink children={formatValue} onClick={() => setOpen(true)} />
      <Calendar
        type="range"
        value={value}
        onChange={setValue}
        poppable
        showPopup={open}
        onClose={setOpen}
        onConfirm={(newValue) => {
          setFormatValue(formatRange(newValue))
          setOpen(false)
        }}
      ></Calendar>
    </>
  )
}将 showConfirm 设置为 false 可以隐藏确认按钮,这种情况下选择完成后会立即触发 confirm 事件。
function SingleQuicklyCalendar() {
  const [open, setOpen] = useState(false)
  const [value, setValue] = useState<Date>()
  const [formatValue, setFormatValue] = useState<string>()
  return (
    <>
      <Cell title="选择单个日期" isLink children={formatValue} onClick={() => setOpen(true)} />
      <Calendar
        type="single"
        value={value}
        onChange={setValue}
        poppable
        showPopup={open}
        showConfirm={false}
        onClose={setOpen}
        onConfirm={(newValue) => {
          setFormatValue(formatFullDate(newValue))
          setOpen(false)
        }}
      />
    </>
  )
}通过 min 和 max 定义日历的范围。
function CustomRangeCalendar() {
  const [minDate] = useState(new Date(2010, 0, 1))
  const [maxDate] = useState(new Date(2010, 0, 31))
  const [open, setOpen] = useState(false)
  const [value, setValue] = useState<Date[]>()
  const [formatValue, setFormatValue] = useState<string>()
  return (
    <>
      <Cell title="自定义日期范围" isLink children={formatValue} onClick={() => setOpen(true)} />
      <Calendar
        type="range"
        min={minDate}
        max={maxDate}
        value={value}
        onChange={setValue}
        poppable
        showPopup={open}
        onClose={setOpen}
        onConfirm={(newValue) => {
          setFormatValue(formatRange(newValue))
          setOpen(false)
        }}
      ></Calendar>
    </>
  )
}通过 confirmText 设置按钮文字,通过 confirmDisabledText 设置按钮禁用时的文字。
function CustomConfirmCalendar() {
  const [open, setOpen] = useState(false)
  const [value, setValue] = useState<Date[]>()
  const [formatValue, setFormatValue] = useState<string>()
  return (
    <>
      <Cell title="自定义按钮" isLink children={formatValue} onClick={() => setOpen(true)} />
      <Calendar
        type="range"
        value={value}
        onChange={(newValue) => {
          setValue(newValue)
        }}
        poppable
        showPopup={open}
        confirmDisabledText="请选择结束时间"
        onClose={setOpen}
        onConfirm={(newValue) => {
          setFormatValue(formatRange(newValue))
          setOpen(false)
        }}
      ></Calendar>
    </>
  )
}通过传入 formatter 函数来对日历上每一格的内容进行格式化。
const dayFormatter = (day: Calendar.DayObject) => {
  if (!day.value) {
    return day
  }
  const month = day.value.getMonth() + 1
  const date = day.value.getDate()
  if (month === 5) {
    if (date === 1) {
      day.top = "劳动节"
    } else if (date === 4) {
      day.top = "青年节"
    } else if (date === 11) {
      day.children = "今天"
    }
  }
  if (day.type === "start") {
    day.bottom = "入店"
  } else if (day.type === "end") {
    day.bottom = "离店"
  } else if (day.type === "active") {
    day.bottom = "入店/离店"
  }
  return day
}
function CustomDayCalendar() {
  const [open, setOpen] = useState(false)
  const [minDate] = useState(new Date(2010, 4, 1))
  const [maxDate] = useState(new Date(2010, 4, 31))
  const [value, setValue] = useState<Date[]>()
  const [formatValue, setFormatValue] = useState<string>()
  return (
    <>
      <Cell title="自定义日期文案" isLink children={formatValue} onClick={() => setOpen(true)} />
      <Calendar
        type="range"
        min={minDate}
        max={maxDate}
        formatter={dayFormatter}
        value={value}
        onChange={setValue}
        poppable
        showPopup={open}
        onClose={setOpen}
        onConfirm={(newValue) => {
          setFormatValue(formatRange(newValue))
          setOpen(false)
        }}
      ></Calendar>
    </>
  )
}通过 placement 属性自定义弹出层的弹出位置,可选值为 top、left、right。
function CustomPositionCalendar() {
  const [open, setOpen] = useState(false)
  const [value, setValue] = useState<Date>()
  const [formatValue, setFormatValue] = useState<string>()
  return (
    <>
      <Cell title="自定义弹出位置" isLink children={formatValue} onClick={() => setOpen(true)} />
      <Calendar
        popupPlacement="right"
        type="single"
        value={value}
        onChange={setValue}
        poppable
        showPopup={open}
        onClose={setOpen}
        onConfirm={(newValue) => {
          setFormatValue(formatFullDate(newValue))
          setOpen(false)
        }}
      ></Calendar>
    </>
  )
}通过 firstDayOfWeek 属性设置一周从哪天开始。
function CustomFirstDayOfWeekCalendar() {
  const [open, setOpen] = useState(false)
  const [value, setValue] = useState<Date>()
  const [formatValue, setFormatValue] = useState<string>()
  return (
    <>
      <Cell title="自定义周起始日" isLink children={formatValue} onClick={() => setOpen(true)} />
      <Calendar
        popupCloseIcon={false}
        popupRounded={false}
        type="single"
        value={value}
        onChange={setValue}
        poppable
        showPopup={open}
        onClose={setOpen}
        firstDayOfWeek={1}
        onConfirm={(newValue) => {
          setFormatValue(formatFullDate(newValue))
          setOpen(false)
        }}
      ></Calendar>
    </>
  )
}将 poppable 设置为 false,日历会直接展示在页面内,而不是以弹层的形式出现。
function TiledCalendar() {
  const [minDate] = useState(new Date(2012, 1, 10))
  const [maxDate] = useState(new Date(2012, 10, 20))
  const [value, setValue] = useState<Date>()
  return (
    <Calendar
      style={{ height: "500px" }}
      title="日历"
      min={minDate}
      max={maxDate}
      value={value}
      onChange={setValue}
    />
  )
}通过 Calendar.Footer Calendar.Button 手动控制 Footer DOM。
function CustomConfirmCalendar() {
  const [open, setOpen] = useState(false)
  const [value, setValue] = useState<Date[]>()
  const [confirm, setConfirm] = useState("请选择结束时间")
  const [formatValue, setFormatValue] = useState<string>()
  return (
    <>
      <Cell title="自定义按钮" isLink children={formatValue} onClick={() => setOpen(true)} />
      <Calendar
        type="range"
        value={value}
        onChange={(newValue) => {
          setValue(newValue)
          setConfirm(newValue.length === 2 ? "完成" : "请选择结束时间")
        }}
        poppable
        showPopup={open}
        onClose={setOpen}
        onConfirm={(newValue) => {
          setFormatValue(formatRange(newValue))
          setOpen(false)
        }}
      >
        <Calendar.Footer>
          <Calendar.Button type="confirm">{confirm}</Calendar.Button>
        </Calendar.Footer>
      </Calendar>
    </>
  )
}| 参数 | 说明 | 类型 | 默认值 | 
|---|---|---|---|
| type | 选择类型:single 表示选择单个日期,multiple 表示选择多个日期,range 表示选择日期区间 | string | single | 
| defaultValue | 默认选中的日期,type 为 multiple 或 range 时为数组,传入 null 表示默认不选择 | Date | Date[] | null | 今天 | 
| value | 选中的日期,type 为 multiple 或 range 时为数组,传入 null 表示默认不选择 | Date | Date[] | null | 今天 | 
| formatter | 日期格式化函数 | (day: Calendar.DayObject) => Calendar.DayObject | - | 
| title | 日历标题 | ReactNode |  |