# React 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
import { CScheduler, type CSchedulerEventFormSlotProps } from '@coreui/react-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 WorkshopForm = ({
  event, labels, setValues, values
}: CSchedulerEventFormSlotProps) => {
  const measured = minutesBetween(values.start, values.end)
  const duration = measured > 0 ? measured : minutesBetween(event.start, event.end)
  const durations = DURATIONS.includes(duration) ? DURATIONS : [...DURATIONS.filter(minutes => minutes < duration), duration, ...DURATIONS.filter(minutes => minutes > duration)]

  return (
    <>
      <label className="scheduler-dialog-field">
        <span className="scheduler-dialog-label">{labels.startLabel}</span>
        <input
          className="form-control form-control-sm"
          onChange={({ target }) => setValues(target.value ? { start: target.value, end: addMinutes(target.value, duration) } : { start: '' })}
          required
          type="datetime-local"
          value={values.start}
        />
      </label>
      <label className="scheduler-dialog-field">
        <span className="scheduler-dialog-label">Duration</span>
        <select
          className="form-select form-select-sm"
          onChange={({ target }) => setValues({ end: addMinutes(values.start, Number(target.value)) })}
          value={duration}
        >
          {durations.map(minutes => <option key={minutes} value={minutes}>{minutes} min</option>)}
        </select>
      </label>
    </>
  )
}

export const SchedulerEditDialogExample = () => (
  <CScheduler
    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 component in `slots.eventForm`. It renders inside the dialog's form
and receives these 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). |

Define the component outside the component that renders the scheduler. A
component written inline in the page's render is a new type each time the
page renders, for example after it stores the change from `onEventChange`, so
React remounts the form and the field being typed in loses focus. The values
survive the remount, because the scheduler holds them.

```tsx
import { CScheduler, type CSchedulerEventFormSlotProps } from '@coreui/react-scheduler'

const WorkshopForm = ({ labels, setValues, values }: CSchedulerEventFormSlotProps) => (
  <label className="scheduler-dialog-field">
    <span className="scheduler-dialog-label">{labels.startLabel}</span>
    <input
      className="form-control form-control-sm"
      onChange={event => setValues({ start: event.target.value })}
      type="datetime-local"
      value={values.start}
    />
  </label>
)

<CScheduler slots={{ eventForm: WorkshopForm }} />
```

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