# Vue Scheduler 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 <kbd>F2</kbd> 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, <kbd>Escape</kbd>, 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.

```html
<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:

| Name | Type | Description |
| --- | --- | --- |
| `allDay` | `boolean` | Whether the edited occurrence is all-day. |
| `event` | `SchedulerEvent` | The event being edited, without the changes made in the dialog. |
| `labels` | `SchedulerLabels` | The labels of this scheduler, including your overrides. |
| `recurring` | `boolean` | Whether the occurrence belongs to a recurring event. |
| `setValues` | `(values) => void` | Merges the given keys into the dialog values. |
| `values` | `EventFormValues` | The current dialog values (below). |

```vue
<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

| Key | Type | Description |
| --- | --- | --- |
| `start` | `string` | Start as wall-clock time in the scheduler `timeZone` (`YYYY-MM-DDTHH:mm`), or a date (`YYYY-MM-DD`) for an all-day event. |
| `end` | `string` | End, in the same form as `start`. |
| `title` | `string \| undefined` | Event title. A blank title is saved as `undefined`. |
| `color` | `string \| undefined` | Event color. |
| `rrule` | `string \| undefined` | Repeat rule without the `RRULE:` prefix. An empty rule is saved as `undefined`. |
| `meta` | `object \| undefined` | Your 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.
