ProSchedulerSign in

Scheduler · CRUD

Bulk actions

selectMultipleEvents: clicks select, Delete removes the selection (onEventsDelete, then onEventDeleted for each), Escape clears it and a double click opens the editor. Controlled here with selectedEvents so the toolbar can act on it.

Live demo

Select three sessions and recolour them · Select two and press Delete

Fullscreen
import { useState } from "react"
import { ProScheduler, type SchedulerEvent } from "@andreisaac/pro-scheduler"
import { studioResources, studioSessions } from "./data/studio"
import { Choice, Log, Toolbar, useEvents, useLog } from "./kit"

const palette = ["#2563eb", "#059669", "#d97706", "#db2777"]

/** Selection plus the bulk actions a host builds around it. */
function useBulk<T extends SchedulerEvent>(initial: () => T[]) {
  const { events, setEvents, handlers } = useEvents<T>(initial)
  const [selected, setSelected] = useState<SchedulerEvent[]>([])
  const { lines, log } = useLog(5)
  const ids = new Set(selected.map((e) => e.id))
  const update = (change: (e: T) => T) => {
    setEvents((list) => list.map((e) => (ids.has(e.id) ? change(e) : e)))
    setSelected([])
  }
  return { events, handlers, selected, setSelected, lines, log, ids, update, setEvents }
}

export function BulkActionsDemo() {
  const bulk = useBulk(() => studioSessions())
  return (
    <>
      <Toolbar label="Bulk actions">
        <span className="text-sm text-muted-foreground">{bulk.selected.length} selected</span>
        {palette.map((color) => (
          <Choice
            key={color}
            onClick={() => {
              bulk.update((e) => ({ ...e, color }))
              bulk.log(`recoloured ${bulk.selected.length}`)
            }}
          >
            <span className="inline-block size-3 rounded-full" style={{ backgroundColor: color }} />
          </Choice>
        ))}
        <Choice onClick={() => bulk.setSelected([])}>Clear</Choice>
      </Toolbar>
      <ProScheduler
        data={bulk.events}
        resources={studioResources}
        view={{ scheduler: { type: "week", startTime: "08:00", endTime: "19:00" } }}
        selectMultipleEvents
        selectedEvents={bulk.selected}
        onSelectedEventsChange={bulk.setSelected}
        onEventsDelete={(events) => bulk.log(`deleting ${events.length} events`)}
        height={580}
        {...bulk.handlers}
      />
      <Log lines={bulk.lines} empty="Click events to select them; press Delete to remove them." />
    </>
  )
}

About this demo

selectMultipleEvents: clicks select, Delete removes the selection (onEventsDelete, then onEventDeleted for each), Escape clears it and a double click opens the editor. Controlled here with selectedEvents so the toolbar can act on it.

Things to try

  • Select three sessions and recolour them
  • Select two and press Delete

The props behind it are explained in Selection, drag and drop, links.

Use ProScheduler in your project