跳转到正文

日历 ​

日历(Calendar)以月视图的形式展示一段时间范围内的日历事件,支持多日历集(Category)分组、事件着色、头部导航与自定义操作栏,常用于待办、日程、排班等场景。

使用方法 ​

通过 new zui.Calendar(element, options) 在指定容器内渲染一个日历,最少只需要传入 events 数据。本例展示本月的工作和个人日程。6 日的原型评审和设计交接各有开始时间,驻场调研和版本发布则通过 allDay: true 标记为全天。

引入 ​

通过 npm ​

标准 npm 包从 zui 导入日历,并通过 zui/css 加载样式。以下示例沿用上方的 #calendarBasic 容器,与全局对象用法选择一种初始化方式即可。

js
import {Calendar} from 'zui';
import 'zui/css';

const calendar = new Calendar('#calendarBasic', {
    events: [{id: 'meeting', title: '项目周会', start: new Date().setHours(9, 30, 0, 0)}],
});

通过全局对象 ​

js
const calendar = new zui.Calendar(element, options);

头部与操作栏 ​

通过 headerTitle 设置标题,通过 headerActions 配置头部右侧的操作按钮(基于工具栏)。点击“今日安排”可查看示例会议的时间。

日历集分组 ​

通过 categories 定义多个日历集,每个事件通过 category 字段关联到对应日历集;未指定 category 的事件会归入 defaultCategory(默认 DEFAULT)。日历集的 color 会作为关联事件的默认颜色。

自定义事件样式 ​

通过事件自身的 color 与 background 可覆盖日历集的默认颜色,通过 icon 可在事件前添加图标。

html
<div id="calendarStyle"></div>

<script>
new zui.Calendar('#calendarStyle', {
    headerTitle: '自定义事件样式',
    events: [
        {id: 'report', title: '提交发布验收报告', start: new Date().setHours(9, 30, 0, 0), icon: {"className": "example-icon example-icon-bell", "style": {"display": "inline-block", "width": "1em", "height": "1em", "backgroundColor": "currentColor", "mask": "url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%3E%3Cpath%20d%3D%22M5%208a7%207%200%200%201%2014%200v8l3%203H2l3-3zM9%2022h6%22%20fill%3D%22none%22%20stroke%3D%22black%22%20stroke-width%3D%221.7%22%20stroke-linecap%3D%22round%22%20stroke-linejoin%3D%22round%22%2F%3E%3C%2Fsvg%3E) center / contain no-repeat"}}, color: 'var(--color-danger-500)'},
        {id: 'accepted', title: '客户门户验收通过', start: new Date().setHours(14, 0, 0, 0), background: 'var(--color-success-500)', color: '#fff'},
    ],
});
</script>

自定义事件渲染 ​

通过 eventRender 拦截每个事件项的渲染,返回新的列表项属性进行覆盖;返回 false 可隐藏该事件。下面用 text 为事件文字添加日历集名称,并隐藏已取消的评审。

html
<div id="calendarRender"></div>

<script>
new zui.Calendar('#calendarRender', {
    categories: [{id: 'work', name: '工作'}],
    events: [
        {id: 'meeting', title: '项目周会', category: 'work', start: new Date().setHours(9, 30, 0, 0)},
        {id: 'cancelled', title: '已取消的评审', category: 'work', start: new Date().setHours(14, 0, 0, 0), data: {cancelled: true}},
    ],
    eventRender(event, category) {
        if (event.data?.cancelled) return false;
        return {text: `[${category.name}] ${event.title}`};
    },
});
</script>

点击交互 ​

onClickDay 与 onClickEvent 分别响应日期格与事件项的点击。回调函数内 this 指向 Calendar 实例,可以调用其方法获取上下文。点击日期、事件或切换月份后,下方文字会显示结果。

html
<div id="calendarInteractions"></div>
<p id="calendarInteractionStatus" role="status">点击日期或事件查看结果。</p>

<script>
const calendarStatus = document.getElementById('calendarInteractionStatus');
new zui.Calendar('#calendarInteractions', {
    categories: [{id: 'work', name: '工作'}],
    events: [
        {id: 'meeting', title: '项目周会', category: 'work', start: new Date().setHours(9, 30, 0, 0)},
    ],
    onClickDay(date) {
        const dayEvents = this.getDayEvents(date);
        calendarStatus.textContent = `${date.toLocaleDateString()}:共 ${dayEvents.length} 个事件。`;
    },
    onClickEvent(event, category) {
        calendarStatus.textContent = `已选择:${event.title}(${category.name})。`;
    },
    onSwitchDate(date) {
        calendarStatus.textContent = `当前月份:${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}。`;
    },
});
</script>

选项 ​

名称类型默认值说明
dateDateLike当前时间初始展示的日期
view'month' | 'week' | 'day''month'视图模式(当前实现以月视图为主)
eventsCalendarEvent[][]日历事件数据
categoriesCalendarCategory[][]日历集数据
defaultCategorystring'DEFAULT'未指定 category 的事件归属的日历集 ID
readonlybooleanfalse是否只读
weekStartnumber1一周从星期几开始(0 表示周日,1 表示周一)
dateFormatstring | (date) => string'yyyy-MM-dd'日期格式
monthFormatstring | (date) => string'yyyy-MM'月份格式
maxEventCountnumber5单日格内最多展示的事件数,超出折叠为「更多」
headerTitleCustomContentType—头部标题,可为字符串、VNode 或渲染函数
headerActionsToolbarSetting—头部右侧操作栏,参考 工具栏
headerPropsPartial<CalendarHeaderProps>—头部组件附加属性
actionsToolbarSetting—全局操作配置
eventRenderfunction—自定义事件项渲染,返回 false 可隐藏
onClickEvent(event, category, e) => void—事件项点击回调
onClickDay(date, e) => void—日期格点击回调
onSwitchDate(date, mode) => void—切换日期回调

实例方法 ​

方法说明
switchDate(date)切换当前展示的日期
getDayEvents(date)获取指定日期的全部事件
getEvent(id)按 ID 查找事件
getCategory(id)按 ID 查找日历集
modifyEvents(events)增量更新事件(与已有数据按 id 合并)
modifyCategories(categories)增量更新日历集
clickEvent(eventID, mouseEvent)触发事件项点击行为,等价于用户点击对应事件
clickDay(dateStr, mouseEvent)触发日期格点击行为,等价于用户点击对应日期

数据结构 ​

文档中的 DateLike 类型来自 @zui/helpers,可接受以下任意形式:

  • Date 对象
  • 时间戳数字(毫秒,例如 Date.now())
  • 可被 new Date(...) 识别的字符串(例如 '2026-05-28')

CalendarEvent ​

ts
type CalendarEvent = {
    /** 唯一标识 */
    id: string;
    /** 标题 */
    title: string;
    /** 所属日历集 ID */
    category?: string;
    /** 是否全天事件 */
    allDay?: boolean;
    /** 描述 */
    desc?: string;
    /** 开始时间 */
    start: DateLike;
    /** 结束时间 */
    end?: DateLike;
    /** 文字颜色 */
    color?: string;
    /** 背景颜色 */
    background?: string;
    /** 图标 */
    icon?: IconType;
    /** 是否只读 */
    readonly?: boolean;
    /** 是否隐藏 */
    hidden?: boolean;
    /** 排序 */
    order?: number;
    /** 自定义数据 */
    data?: Record<string, unknown>;
};

CalendarCategory ​

ts
type CalendarCategory = {
    /** 唯一标识 */
    id: string;
    /** 名称 */
    name?: string;
    /** 描述 */
    desc?: string;
    /** 默认文字颜色 */
    color?: string;
    /** 默认背景颜色 */
    background?: string;
    /** 默认图标 */
    icon?: IconType;
    /** 该日历集下的事件(与 `events` 选项中的事件一并合并) */
    events?: CalendarEvent[];
    /** 是否只读 */
    readonly?: boolean;
    /** 排序 */
    order?: number;
    /** 是否隐藏 */
    hidden?: boolean;
    /** 自定义数据 */
    data?: Record<string, unknown>;
};

CSS 类 ​

类作用
calendar日历根元素
calendar-header头部容器
calendar-header-title头部标题
calendar-header-nav头部导航按钮(今天 / 上月 / 下月)
calendar-header-actions头部右侧操作栏
calendar-body日历主体容器
calendar-month-view月视图容器
calendar-month-view-row月视图周行
calendar-month-view-day月视图日期格
calendar-month-view-day.is-today当天日期格
calendar-month-view-day.is-out-month非当月日期格
calendar-event-list-item事件项
calendar-event-list-item-more「更多事件」入口

CSS 变量 ​

变量作用默认值
--calendar-event-color事件项的默认主题色var(--color-primary-500)