React Scheduler - Jotai 教程
本教程演示如何将 DHTMLX React Scheduler 连接到一个 Jotai 存储。你将把事件和 UI 状态(view/date/config)保存在原子中,通过 data.save 路由 Scheduler 编辑,并通过基于快照的历史实现 撤销/重做。
注释
完整的源代码可在 GitHub 上找到。
先决条件
- Node.js(推荐 LTS)
- React + TypeScript 基础
- 熟悉 Jotai 的 atoms 与
useAtom/useSetAtom。如需快速回顾,请参阅 Jotai 文档:https://jotai.org/
快速设置 - 创建项目
创建一个 Vite + React + TypeScript 项目:
npm create vite@latest scheduler-jotai-demo -- --template react-ts
cd scheduler-jotai-demo
npm install
安装 Jotai:
npm install jotai
安装 Material UI(用于演示工具栏):
npm install @mui/material @mui/icons-material @emotion/react @emotion/styled
安装 React Scheduler
按 React Scheduler 安装指南 所述安装 React Scheduler。
在本教程中,我们使用评估包:
npm install @dhtmlx/trial-react-scheduler
或
yarn add @dhtmlx/trial-react-scheduler
如果你已经使用 Professional 套件,在命令和导入中将 @dhtmlx/trial-react-scheduler 替换为 @dhx/react-scheduler。
运行开发服务器:
npm run dev
注释
为了让 Scheduler 占满整页,请从 src/App.css 删除默认样式并添加:
#root {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
body {
margin: 0;
}
定义共享类型
创建 src/types.ts。这些类型在原子和组件之间共享:
export type SchedulerView = "day" | "week" | "month";
export type SchedulerEventId = string | number;
export interface SchedulerEvent {
id: SchedulerEventId;
start_date: string;
end_date: string;
text: string;
// Scheduler may attach extra fields (e.g. custom props). Keep the demo permissive.
[key: string]: unknown;
}
export type SchedulerConfig = Record<string, unknown>;
export interface SchedulerSnapshot {
events: SchedulerEvent[];
config: SchedulerConfig;
}
SchedulerEvent使用索引签名,以便 Scheduler 可以在运行时附加额外字段。SchedulerSnapshot捕捉撤销/重做所需的数据(事件 + 配置)。
设置示例数据
创建 src/seed/data.ts,包含若干事件和初始 UI 状态。请注意,seedDate 以 数字(时间戳)的形式存储,以确保原子状态可序列化。
import type { SchedulerEvent, SchedulerView } from "../types";
export const seedEvents: SchedulerEvent[] = [
{ id: 1, start_date: "2025-08-11T02:00:00Z", end_date: "2025-08-11T10:20:00Z", text: "Product Strategy Hike" },
{ id: 2, start_date: "2025-08-12T06:00:00Z", end_date: "2025-08-12T11:00:00Z", text: "Tranquil Tea Time" },
{ id: 3, start_date: "2025-08-15T03:00:00Z", end_date: "2025-08-15T08:00:00Z", text: "Demo and Showcase" },
];
export const seedDate = Date.parse("2025-08-15T00:00:00Z");
export const seedView: SchedulerView = "week";
注释
伴随的演示包括额外的事件,以获得更丰富的视觉效果。
创建 Jotai 原子和操作
创建 src/schedulerAtoms.ts。本设置存储:
events(Scheduler 数据)currentDate(时间戳)view(day | week | month)config(Scheduler 配置对象,包含readonly)- 用于撤销/重做的
past/future快照
在本示例中,撤销/重做仅跟踪 事件和配置的变更。日期导航和视图切换未加入历史记录。
import { atom } from "jotai";
import { seedDate, seedEvents, seedView } from "./seed/data";
import type {
SchedulerConfig,
SchedulerEvent,
SchedulerEventId,
SchedulerSnapshot,
SchedulerView,
} from "./types";
interface SchedulerState {
events: SchedulerEvent[];
currentDate: number;
view: SchedulerView;
config: SchedulerConfig;
}
export type SchedulerAction =
| {
type: "updateEvent";
payload: Partial<SchedulerEvent> & Pick<SchedulerEvent, "id">;
}
| {
type: "createEvent";
payload: Omit<SchedulerEvent, "id"> & Partial<Pick<SchedulerEvent, "id">>;
}
| { type: "deleteEvent"; payload: SchedulerEventId }
| { type: "setCurrentDate"; payload: number }
| { type: "setView"; payload: SchedulerView }
| { type: "updateConfig"; payload: Partial<SchedulerConfig> }
| { type: "undo" }
| { type: "redo" };
const schedulerStateAtom = atom<SchedulerState>({
events: seedEvents as unknown as SchedulerEvent[],
currentDate: seedDate,
view: seedView,
config: {},
});
const pastAtom = atom<SchedulerSnapshot[]>([]);
const futureAtom = atom<SchedulerSnapshot[]>([]);
const MAX_HISTORY_SIZE = 50;
const deepCopy = <T,>(value: T): T => {
return JSON.parse(JSON.stringify(value)) as T;
};
const createSnapshot = (state: SchedulerState): SchedulerSnapshot => ({
events: deepCopy(state.events),
config: deepCopy(state.config),
});
export const schedulerActionsAtom = atom(
null,
(get, set, action: SchedulerAction): SchedulerEvent | void => {
const currentState = get(schedulerStateAtom);
const past = get(pastAtom);
const future = get(futureAtom);
const pushHistory = () => {
set(pastAtom, [...past.slice(-MAX_HISTORY_SIZE + 1), createSnapshot(currentState)]);
set(futureAtom, []);
};
if (action.type === "setCurrentDate") {
set(schedulerStateAtom, { ...currentState, currentDate: action.payload });
return;
}
if (action.type === "setView") {
set(schedulerStateAtom, { ...currentState, view: action.payload });
return;
}
if (action.type === "createEvent") {
pushHistory();
const id = action.payload.id != null ? action.payload.id : `id_${Date.now().toString()}`;
const newEvent: SchedulerEvent = { ...action.payload, id } as SchedulerEvent;
set(schedulerStateAtom, {
...currentState,
events: [...currentState.events, newEvent],
});
return newEvent;
}
if (action.type === "updateEvent") {
const index = currentState.events.findIndex((event) => String(event.id) === String(action.payload.id));
if (index === -1) return;
pushHistory();
set(schedulerStateAtom, {
...currentState,
events: [
...currentState.events.slice(0, index),
{ ...currentState.events[index], ...action.payload },
...currentState.events.slice(index + 1),
],
});
return;
}
if (action.type === "deleteEvent") {
const exists = currentState.events.some((event) => String(event.id) === String(action.payload));
if (!exists) return;
pushHistory();
set(schedulerStateAtom, {
...currentState,
events: currentState.events.filter((event) => String(event.id) !== String(action.payload)),
});
return;
}
if (action.type === "updateConfig") {
pushHistory();
set(schedulerStateAtom, {
...currentState,
config: { ...currentState.config, ...action.payload },
});
return;
}
if (action.type === "undo") {
if (past.length === 0) return;
const previous = past[past.length - 1];
set(pastAtom, past.slice(0, -1));
set(futureAtom, [createSnapshot(currentState), ...future]);
set(schedulerStateAtom, {
...currentState,
events: previous.events,
config: previous.config,
});
return;
}
if (action.type === "redo") {
if (future.length === 0) return;
const next = future[0];
set(futureAtom, future.slice(1));
set(pastAtom, [...past, createSnapshot(currentState)]);
set(schedulerStateAtom, {
...currentState,
events: next.events,
config: next.config,
});
}
}
);
export const schedulerStateViewAtom = atom((get) => get(schedulerStateAtom));
export const canUndoAtom = atom((get) => get(pastAtom).length > 0);
export const canRedoAtom = atom((get) => get(futureAtom).length > 0);
构建控制工具栏
创建 src/components/Toolbar.tsx。该工具栏将:
- 切换视图(day/week/month)
- 导航上一个/今天/下一个
- 撤销/重做,含禁用状态
- 切换只读模式
import { ButtonGroup, Button, Typography, Stack, FormControlLabel, Switch } from "@mui/material";
import UndoIcon from "@mui/icons-material/Undo";
import RedoIcon from "@mui/icons-material/Redo";
import React from "react";
import type { SchedulerView } from "../types";
export interface ToolbarProps {
currentView: SchedulerView;
currentDate: Date;
isReadOnly: boolean;
canUndo: boolean;
canRedo: boolean;
onUndo?: () => void;
onRedo?: () => void;
onNavigate?: (action: "prev" | "next" | "today") => void;
onReadOnlyChange?: (value: boolean) => void;
setView: (view: SchedulerView) => void;
}
export default React.memo(function Toolbar({
currentView,
currentDate,
isReadOnly,
canUndo,
canRedo,
onUndo,
onRedo,
onNavigate,
onReadOnlyChange,
setView,
}: ToolbarProps) {
return (
<Stack direction="row" justifyContent="space-between" alignItems="center" sx={{ m: 2 }}>
<Stack direction="row" gap={1}>
{(["day", "week", "month"] as const).map((label) => (
<Button key={label} variant={currentView === label ? "contained" : "outlined"} onClick={() => setView(label)}>
{label.charAt(0).toUpperCase() + label.slice(1)}
</Button>
))}
<ButtonGroup>
<Button onClick={() => onUndo?.()} disabled={!canUndo}>
<UndoIcon />
</Button>
<Button onClick={() => onRedo?.()} disabled={!canRedo}>
<RedoIcon />
</Button>
</ButtonGroup>
<FormControlLabel
label="Read-only"
control={
<Switch
checked={isReadOnly}
onChange={(event) => onReadOnlyChange?.(event.target.checked)}
inputProps={{ "aria-label": "Toggle read-only" }}
/>
}
/>
</Stack>
<Typography variant="subtitle1" sx={{ ml: 1 }}>
{new Date(currentDate)?.toLocaleDateString(undefined, { weekday: "short", month: "short", day: "numeric" })}
</Typography>
<ButtonGroup>
<Button onClick={() => onNavigate?.("prev")}>
<
</Button>
<Button onClick={() => onNavigate?.("today")}>
Today
</Button>
<Button onClick={() => onNavigate?.("next")}>
>
</Button>
</ButtonGroup>
</Stack>
);
});
将 Scheduler 连接到 Jotai
创建 src/components/Scheduler.tsx。该组件将:
- 从原子中读取
events/view/currentDate/config - 通过
data.save桥接 Scheduler 的 CRUD(create/update/delete) - 连接
undo/redo、导航以及只读切换 - 隐藏内置 Scheduler 导航栏并使用自定义工具栏
import { useCallback, useMemo } from "react";
import { useAtomValue, useSetAtom } from "jotai";
import ReactScheduler from "@dhtmlx/trial-react-scheduler";
import "@dhtmlx/trial-react-scheduler/dist/react-scheduler.css";
import Toolbar from "./Toolbar";
import {
canRedoAtom,
canUndoAtom,
schedulerActionsAtom,
schedulerStateViewAtom,
} from "../schedulerAtoms";
import type { SchedulerEvent, SchedulerEventId, SchedulerView } from "../types";
type SaveAction = "create" | "update" | "delete";
type SaveEntity = "event";
export default function DemoJotaiScheduler() {
const state = useAtomValue(schedulerStateViewAtom);
const dispatchAction = useSetAtom(schedulerActionsAtom);
const canUndo = useAtomValue(canUndoAtom);
const canRedo = useAtomValue(canRedoAtom);
const { events, view, currentDate, config } = state;
const activeDate = useMemo(() => new Date(currentDate), [currentDate]);
const isReadOnly = Boolean((config as { readonly?: unknown }).readonly);
const setCurrentDate = useCallback(
(dateMs: number) => dispatchAction({ type: "setCurrentDate", payload: dateMs }),
[dispatchAction]
);
const setView = useCallback(
(nextView: SchedulerView) => dispatchAction({ type: "setView", payload: nextView }),
[dispatchAction]
);
const undo = useCallback(() => dispatchAction({ type: "undo" }), [dispatchAction]);
const redo = useCallback(() => dispatchAction({ type: "redo" }), [dispatchAction]);
const updateReadOnly = useCallback(
(value: boolean) => dispatchAction({ type: "updateConfig", payload: { readonly: value } }),
[dispatchAction]
);
const handleDateNavigation = useCallback(
(action: "prev" | "next" | "today") => {
if (action === "today") {
setCurrentDate(Date.now());
return;
}
const step = action === "next" ? 1 : -1;
const date = new Date(currentDate);
if (view === "day") {
date.setDate(date.getDate() + step);
} else if (view === "week") {
date.setDate(date.getDate() + step * 7);
} else {
date.setMonth(date.getMonth() + step);
}
setCurrentDate(date.getTime());
},
[currentDate, view, setCurrentDate]
);
const handleViewChange = useCallback(
(mode: string, date: Date) => {
const nextView: SchedulerView = mode === "day" || mode === "week" || mode === "month" ? mode : "month";
setView(nextView);
setCurrentDate(date.getTime());
},
[setView, setCurrentDate]
);
// Scheduler <-> Jotai data bridge
const dataBridge = useMemo(
() => ({
save: (entity: SaveEntity, action: SaveAction, payload: unknown, id: unknown) => {
if (entity !== "event") return;
switch (action) {
case "update": {
const eventData =
payload && typeof payload === "object" ? (payload as Partial<SchedulerEvent>) : ({} as Partial<SchedulerEvent>);
const eventId = eventData.id ?? id;
if (eventId == null) {
console.warn("Update called without an id", { payload, id });
return;
}
const updatedEvent = { ...eventData, id: eventId } as Partial<SchedulerEvent> & Pick<SchedulerEvent, "id">;
dispatchAction({ type: "updateEvent", payload: updatedEvent });
return updatedEvent;
}
case "create": {
const eventData =
payload && typeof payload === "object"
? (payload as Omit<SchedulerEvent, "id"> & Partial<Pick<SchedulerEvent, "id">>)
: null;
if (!eventData) {
console.warn("Create called without event payload", { payload });
return;
}
return dispatchAction({ type: "createEvent", payload: eventData });
}
case "delete": {
const deleteId =
payload && typeof payload === "object"
? ((payload as { id?: unknown }).id ?? id)
: payload ?? id;
if (deleteId == null) {
console.warn("Delete called without an id", { payload, id });
return;
}
dispatchAction({ type: "deleteEvent", payload: deleteId as SchedulerEventId });
return deleteId;
}
default:
console.warn(`Unknown action: ${action}`);
return;
}
},
}),
[dispatchAction]
);
const memoizedXY = useMemo(() => ({ nav_height: 0 }), []);
return (
<div style={{ height: "100vh", display: "flex", flexDirection: "column" }}>
<Toolbar
currentView={view}
currentDate={activeDate}
isReadOnly={isReadOnly}
canUndo={canUndo}
canRedo={canRedo}
onUndo={undo}
onRedo={redo}
onNavigate={handleDateNavigation}
onReadOnlyChange={updateReadOnly}
setView={setView}
/>
<ReactScheduler
events={events}
view={view}
date={activeDate}
xy={memoizedXY}
config={config}
data={dataBridge}
onViewChange={handleViewChange}
/>
</div>
);
}
在应用中渲染 Scheduler
更新 src/App.tsx:
import Scheduler from "./components/Scheduler";
import "./App.css";
function App() {
return <Scheduler />;
}
export default App;