# 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
<div id="schedulerEditDialog"></div>
```

```js
const pad = n => String(n).padStart(2, '0')
const day = offset => {
  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, minutes) => {
  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 field = (caption, control) => {
  const label = document.createElement('label')
  label.className = 'scheduler-dialog-field'
  const text = document.createElement('span')
  text.className = 'scheduler-dialog-label'
  text.textContent = caption
  label.append(text, control)
  return label
}

const workshopForm = ({ labels, setValues, values }) => {
  let { start } = values
  let duration = (new Date(values.end).getTime() - new Date(values.start).getTime()) / 60_000

  const startInput = document.createElement('input')
  startInput.className = 'form-control form-control-sm'
  startInput.required = true
  startInput.type = 'datetime-local'
  startInput.value = start
  startInput.addEventListener('input', () => {
    start = startInput.value
    setValues(start ? { start, end: addMinutes(start, duration) } : { start })
  })

  const durationSelect = document.createElement('select')
  durationSelect.className = 'form-select form-select-sm'
  const presets = [30, 60, 90, 120]
  const durations = presets.includes(duration) ? presets : [...presets.filter(minutes => minutes < duration), duration, ...presets.filter(minutes => minutes > duration)]
  for (const minutes of durations) {
    durationSelect.add(new Option(`${minutes} min`, String(minutes)))
  }

  durationSelect.value = String(duration)
  durationSelect.addEventListener('change', () => {
    duration = Number(durationSelect.value)
    setValues({ end: addMinutes(start, duration) })
  })

  const element = document.createElement('div')
  element.style.display = 'contents'
  element.append(field(labels.startLabel, startInput), field('Duration', durationSelect))
  return { element }
}

new coreui.Scheduler(document.getElementById('schedulerEditDialog'), {
  dayStartHour: 7,
  dayEndHour: 18,
  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'
    }
  ],
  slots: { eventForm: workshopForm }
})
```

## Custom form

Pass a function in `slots.eventForm`. The scheduler calls it each time the
dialog opens and places the returned `element` inside the dialog's form; an
optional `dispose` runs when the dialog closes. The function receives:

| 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). |

The function runs once per opening: `setValues` does not call it again, so
the elements and their state are yours to update.

```js
new coreui.Scheduler(element, {
  slots: {
    eventForm: ({ labels, setValues, values }) => {
      const input = document.createElement('input')
      input.type = 'datetime-local'
      input.value = values.start
      input.addEventListener('input', () => setValues({ start: input.value }))
      return { element: input }
    }
  }
})
```

## 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.
