Scheduler · CRUD
Custom editor
With showDefaultEditor={false}, clicks and drafts come to you. Returning false from onEventCreate cancels the automatic create so your form decides.
Live demo
Drag across an empty slot · Click an event and change its person
Fullscreenimport { useState } from "react"
import { ProScheduler, type SchedulerEvent } from "@andreisaac/pro-scheduler"
import { studioResources, studioSessions } from "./data/studio"
import { useEvents } from "./kit"
type Draft = { id?: string | number; title: string; start: Date; end: Date; resource?: string | number }
export function CustomEditorDemo() {
const { events, setEvents } = useEvents<SchedulerEvent>(studioSessions)
const [draft, setDraft] = useState<Draft | null>(null)
const toInput = (d: Date) =>
new Date(d.getTime() - d.getTimezoneOffset() * 60_000).toISOString().slice(0, 16)
const save = () => {
if (!draft) return
setEvents((list) =>
draft.id != null
? list.map((e) => (e.id === draft.id ? { ...e, ...draft } : e))
: [...list, { ...draft, id: `custom-${list.length + 1}` }]
)
setDraft(null)
}
return (
<div className="demo-split demo-split--wide">
<ProScheduler
data={events}
resources={studioResources}
view={{ scheduler: { type: "week", startTime: "07:00", endTime: "20:00" } }}
showDefaultEditor={false}
onEventClick={(event) =>
setDraft({
id: event.id,
title: event.title,
start: new Date(event.start),
end: new Date(event.end ?? event.start),
resource: Array.isArray(event.resource) ? event.resource[0] : event.resource,
})
}
// Take over creation: open our form with the drafted slot, and cancel
// the scheduler's own create.
onEventCreate={(slot) => {
setDraft({ title: "", start: slot.start, end: slot.end, resource: slot.resource })
return false
}}
onEventUpdated={(event) => setEvents((list) => list.map((e) => (e.id === event.id ? event : e)))}
height={560}
/>
<aside className="demo-side">
{draft ? (
<form
className="demo-form"
onSubmit={(e) => {
e.preventDefault()
save()
}}
>
<h4>{draft.id != null ? "Edit booking" : "New booking"}</h4>
<label>
Title
<input required value={draft.title} onChange={(e) => setDraft({ ...draft, title: e.target.value })} />
</label>
<label>
Person
<select
value={String(draft.resource ?? "")}
onChange={(e) => setDraft({ ...draft, resource: e.target.value || undefined })}
>
<option value="">Unassigned</option>
{studioResources.map((r) => (
<option key={r.id} value={r.id}>
{r.name}
</option>
))}
</select>
</label>
<label>
Starts
<input
type="datetime-local"
value={toInput(draft.start)}
onChange={(e) => setDraft({ ...draft, start: new Date(e.target.value) })}
/>
</label>
<label>
Ends
<input
type="datetime-local"
value={toInput(draft.end)}
onChange={(e) => setDraft({ ...draft, end: new Date(e.target.value) })}
/>
</label>
<div className="demo-form__actions">
<button type="submit">Save</button>
{draft.id != null && (
<button
type="button"
onClick={() => {
setEvents((list) => list.filter((e) => e.id !== draft.id))
setDraft(null)
}}
>
Delete
</button>
)}
<button type="button" onClick={() => setDraft(null)}>
Cancel
</button>
</div>
</form>
) : (
<p className="demo-muted">Click an event, or drag across an empty slot, to open this form.</p>
)}
</aside>
</div>
)
}// data/studio.ts — sample data, relative to the current week or month.
import type { SchedulerEvent, SchedulerResource } from "@andreisaac/pro-scheduler"
export type StudioResource = SchedulerResource & {
id: string
name: string
role: string
color: string
}
export type StudioEvent = SchedulerEvent & {
id: string
title: string
start: Date
end?: Date
resource: string
client: string
kind: "Shoot" | "Edit" | "Review" | "Pitch" | "Travel" | "Workshop"
}
export const studioResources: StudioResource[] = [
{ id: "ada", name: "Ada Moreno", role: "Director", color: "#2563eb" },
{ id: "ben", name: "Ben Okafor", role: "Producer", color: "#059669" },
{ id: "chloe", name: "Chloé Martin", role: "Editor", color: "#d97706" },
{ id: "dev", name: "Dev Patel", role: "Colourist", color: "#7c3aed" },
{ id: "elin", name: "Elin Berg", role: "Sound", color: "#db2777" },
{ id: "farah", name: "Farah Haddad", role: "Camera", color: "#0891b2" },
{ id: "gus", name: "Gus Almeida", role: "Motion", color: "#65a30d" },
{ id: "hana", name: "Hana Sato", role: "Designer", color: "#dc2626" },
]
/** Monday 00:00 of the week containing `date`. */
export function weekStart(date = new Date()): Date {
const d = new Date(date.getFullYear(), date.getMonth(), date.getDate())
const offset = (d.getDay() + 6) % 7
d.setDate(d.getDate() - offset)
return d
}
/** A date `day` days after this week's Monday, at `hour:minute`. */
function at(base: Date, day: number, hour: number, minute = 0): Date {
return new Date(base.getFullYear(), base.getMonth(), base.getDate() + day, hour, minute)
}
type Template = [
title: string,
client: string,
kind: StudioEvent["kind"],
resource: string,
startDay: number,
startHour: number,
endDay: number,
endHour: number,
]
// Timed work in and around the current week: what the day/week views show.
const weekTemplates: Template[] = [
["Kickoff call", "Harbor Coffee", "Pitch", "ben", 0, 9, 0, 10],
["Location scout", "Harbor Coffee", "Travel", "ada", 0, 11, 0, 14],
["Rough cut review", "Aurora Bank", "Review", "chloe", 0, 15, 0, 16.5],
["Studio shoot", "Kinetic Shoes", "Shoot", "farah", 1, 8, 1, 13],
["Grade: episode 3", "Northline TV", "Edit", "dev", 1, 10, 1, 15],
["Voice-over session", "Aurora Bank", "Edit", "elin", 1, 14, 1, 16],
["Storyboard pass", "Harbor Coffee", "Workshop", "hana", 2, 9.5, 2, 12],
["Client review", "Kinetic Shoes", "Review", "ben", 2, 13, 2, 14],
["Title animation", "Northline TV", "Edit", "gus", 2, 10, 2, 17],
["Exterior shoot", "Harbor Coffee", "Shoot", "ada", 3, 7, 3, 12],
["Mix: final", "Aurora Bank", "Edit", "elin", 3, 13, 3, 17],
["Pitch rehearsal", "Lumen Labs", "Pitch", "ben", 3, 16, 3, 17],
["Pitch", "Lumen Labs", "Pitch", "ada", 4, 10, 4, 11.5],
["Conform & deliver", "Northline TV", "Edit", "chloe", 4, 9, 4, 13],
["Team retro", "Studio", "Workshop", "hana", 4, 15, 4, 16],
["Product close-ups", "Kinetic Shoes", "Shoot", "farah", 7, 9, 7, 14],
["Colour review", "Kinetic Shoes", "Review", "dev", 8, 11, 8, 12],
["Rough cut", "Harbor Coffee", "Edit", "chloe", 8, 9, 8, 17],
["Previous week wrap", "Aurora Bank", "Review", "ben", -3, 14, -3, 15],
["Pickup shots", "Aurora Bank", "Shoot", "farah", -2, 9, -2, 12],
]
function build(templates: Template[], prefix: string, base: Date): StudioEvent[] {
return templates.map(([title, client, kind, resource, sd, sh, ed, eh], index) => ({
id: `${prefix}-${index + 1}`,
title,
client,
kind,
resource,
start: at(base, sd, Math.floor(sh), Math.round((sh % 1) * 60)),
end: at(base, ed, Math.floor(eh), Math.round((eh % 1) * 60)),
}))
}
/** Timed sessions around the current week — day, week and agenda views. */
export function studioSessions(base = weekStart()): StudioEvent[] {
return build(weekTemplates, "s", base)
}About this demo
With showDefaultEditor={false}, clicks and drafts come to you. Returning false from onEventCreate cancels the automatic create so your form decides.
Things to try
- Drag across an empty slot
- Click an event and change its person
The props behind it are explained in Drag, resize & create.