ProSchedulerSign in

Timeline · Templating

Cell content

renderCell gets the date and the row's resource; here the first row counts how many people are free each day.

Live demo

Move an assignment and watch the counts

Fullscreen
import { useMemo } from "react"
import { ProScheduler } from "@andreisaac/pro-scheduler"
import { monthStart, studioAssignments, studioResources } from "./data/studio"
import { useEvents } from "./kit"

const day = (base: Date, n: number) => new Date(base.getFullYear(), base.getMonth(), base.getDate() + n)

export function TimelineCellDemo() {
  const { events, handlers } = useEvents(studioAssignments)

  // How many of the eight are free each day, shown in every row's cell.
  const freeByDay = useMemo(() => {
    const counts = new Map<string, number>()
    const month = monthStart()
    for (let i = 0; i < 31; i++) {
      const d = day(month, i)
      const next = day(month, i + 1).getTime()
      const busy = new Set(
        events
          .filter((e) => new Date(e.start).getTime() < next && new Date(e.end!).getTime() > d.getTime())
          .map((e) => e.resource)
      )
      counts.set(d.toDateString(), studioResources.length - busy.size)
    }
    return counts
  }, [events])

  return (
    <ProScheduler
      data={events}
      resources={studioResources}
      view={{ timeline: { type: "month" } }}
      renderCell={({ date, resource }) =>
        resource?.id === studioResources[0].id ? (
          <span className="absolute top-0.5 right-1 text-[10px] text-muted-foreground tabular-nums">
            {freeByDay.get(date.toDateString())} free
          </span>
        ) : null
      }
      height={520}
      {...handlers}
    />
  )
}

About this demo

renderCell gets the date and the row's resource; here the first row counts how many people are free each day.

Things to try

  • Move an assignment and watch the counts

The props behind it are explained in Custom rendering.

Use ProScheduler in your project