Vue Scheduler Edit Dialog

Edit dialog

Render your own fields in the edit dialog while the scheduler keeps the dialog, the recurring scope and the save.

Clicking an event, or pressing F2 on it, opens the edit dialog. By default it edits the title, start, end, color and repeat rule. You can render your own fields in their place. The dialog itself stays the same: its heading, focus handling, Escape, the Applies to select of a recurring event, and the Delete, Cancel and Save buttons.

The example below books workshop jobs: the form edits only the start and a duration, and the end follows from them.

vue
<script setup lang="ts">
import { CScheduler, type EventFormValues, type SchedulerEvent } from '@coreui/vue-scheduler'

const DURATIONS = [30, 60, 90, 120]

const pad = (n: number) => String(n).padStart(2, '0')
const day = (offset: number) => {
  const date = new Date()
  date.setDate(date.getDate() - ((date.getDay() + 6) % 7) + offset)
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
}

const addMinutes = (value: string, minutes: number) => {
  const date = new Date(value)
  date.setMinutes(date.getMinutes() + minutes)
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(date.getMinutes())}`
}

const minutesBetween = (start: string, end: string) => (new Date(end).getTime() - new Date(start).getTime()) / 60_000

const durationOf = (values: EventFormValues, event: SchedulerEvent) => {
  const measured = minutesBetween(values.start, values.end)
  return measured > 0 ? measured : minutesBetween(event.start, event.end)
}

const durationsFor = (duration: number) =>
  DURATIONS.includes(duration) ? DURATIONS : [...DURATIONS.filter(minutes => minutes < duration), duration, ...DURATIONS.filter(minutes => minutes > duration)]

const moveStart = (start: string, values: EventFormValues, event: SchedulerEvent): Partial<EventFormValues> =>
  start ? { start, end: addMinutes(start, durationOf(values, event)) } : { start }

const events = [
  { id: 'service', title: 'Annual service', start: `${day(1)}T09:00`, end: `${day(1)}T10:30` },
  { id: 'tyres', title: 'Tyre change', start: `${day(2)}T13:00`, end: `${day(2)}T13:30`, color: '#2eb85c' }
]
</script>

<template>
  <CScheduler :day-start-hour="7" :day-end-hour="18" :events="events">
    <template #event-form="{ event, labels, setValues, values }">
      <label class="scheduler-dialog-field">
        <span class="scheduler-dialog-label">{{ labels.startLabel }}</span>
        <input
          class="form-control form-control-sm"
          required
          type="datetime-local"
          :value="values.start"
          @input="setValues(moveStart(($event.target as HTMLInputElement).value, values, event))"
        />
      </label>
      <label class="scheduler-dialog-field">
        <span class="scheduler-dialog-label">Duration</span>
        <select
          class="form-select form-select-sm"
          :value="durationOf(values, event)"
          @change="setValues({ end: addMinutes(values.start, Number(($event.target as HTMLSelectElement).value)) })"
        >
          <option v-for="minutes in durationsFor(durationOf(values, event))" :key="minutes" :value="minutes">{{ minutes }} min</option>
        </select>
      </label>
    </template>
  </CScheduler>
</template>

Custom form

Fill the event-form slot. It renders inside the dialog’s form and receives these slot props:

NameTypeDescription
allDaybooleanWhether the edited occurrence is all-day.
eventSchedulerEventThe event being edited, without the changes made in the dialog.
labelsSchedulerLabelsThe labels of this scheduler, including your overrides.
recurringbooleanWhether the occurrence belongs to a recurring event.
setValues(values) => voidMerges the given keys into the dialog values.
valuesEventFormValuesThe current dialog values (below).
<CScheduler :events="events">
  <template #event-form="{ labels, setValues, values }">
    <label class="scheduler-dialog-field">
      <span class="scheduler-dialog-label">{{ labels.startLabel }}</span>
      <input
        class="form-control form-control-sm"
        type="datetime-local"
        :value="values.start"
        @input="setValues({ start: ($event.target as HTMLInputElement).value })"
      />
    </label>
  </template>
</CScheduler>

Values

KeyTypeDescription
startstringStart as wall-clock time in the scheduler timeZone (YYYY-MM-DDTHH:mm), or a date (YYYY-MM-DD) for an all-day event.
endstringEnd, in the same form as start.
titlestring | undefinedEvent title. A blank title is saved as undefined.
colorstring | undefinedEvent color.
rrulestring | undefinedRepeat rule without the RRULE: prefix. An empty rule is saved as undefined.
metaobject | undefinedYour own data on the event. setValues({ meta }) replaces the whole object, so spread event.meta to keep the rest.

Saving

Save writes the values over the event and keeps everything they do not carry, such as id, resourceId, allDay and exdates. An end at or before the start keeps the duration the event had. The change reaches onEventChange and dataSource.persistEvents like any other edit, and for a recurring event it applies to the scope chosen in the dialog.

Cancel, Escape and a click on the backdrop discard the values. The next time the dialog opens, they start again from the event.

Validation

Your fields sit inside the dialog’s <form>, so the browser validates them before Save: required, min, max, pattern or setCustomValidity() on your inputs keep the dialog open until they are valid.

Focus and styling

When the dialog opens, focus moves to the first element of your form that can take it, or to Cancel when there is none. Wrap a field in .scheduler-dialog-field with a .scheduler-dialog-label caption to match the spacing and typography of the built-in fields.