Skip to Content
TelamonJS

ConfirmActionDialog

Confirmation dialog for an async action, with validated inputs, free content, a dialog- or caller-controlled pending state and inline errors

The dialog behind confirmDialog on the row and menu actions of a ListPage, available for any other button. It stays open while the action runs, closes when it succeeds and shows the error when it fails, so the user can retry without retyping what they filled in.

import { Button, ConfirmActionDialog } from '@telamonjs/ui'

<ConfirmActionDialog
  title='Reject request?'
  description='The requester is notified with your reason.'
  confirmText='Reject'
  variant='destructive'
  inputs={[{ name: 'reason', label: 'Reason', type: 'textarea', required: true }]}
  onConfirm={({ reason }) => requestsApi.reject(request.id, reason)}
>
  <Button variant='destructive'>Reject</Button>
</ConfirmActionDialog>

Behavior

  • Pending: while the promise returned by onConfirm is pending, both buttons are disabled, the confirm button shows a spinner (and pendingText, if set) and the dialog cannot be dismissed.
  • Success: when onConfirm resolves, the dialog closes.
  • Failure: when it rejects, the dialog stays open and shows the error's message (or a localized fallback). Resolve false instead to stay open without a message — for an error you already reported, such as in a toast.
  • Inputs: text, textarea, select and date (the native date input, value YYYY-MM-DD). A required input left empty (or blank), a trimmed value outside minLength/maxLength, a date outside min/max and an input whose validate returns a message block the confirm and are marked invalid. Values reach onConfirm keyed by name; every opening starts again from defaultValue.
  • Live counts: a minLength/maxLength input shows how many characters are missing (or over) as the user types, before any attempt, so the reason is visible without clicking.
  • disableUntilValid keeps the confirm disabled while any input fails. Every failing field then shows its message live: muted while untouched, as an error once edited or after an attempt. Without it the confirm stays enabled and messages show from the first attempt.
  • confirmDisabled disables only the confirm button, from your own state.
  • Focus: with inputs, the first field is focused on open, so a reason can be typed right away; without inputs, focus starts on Cancel, the safe default for a destructive confirmation.

Pass open / onOpenChange to control it (for example from a menu item), or a children trigger and nothing else to let it own its state. children is always the trigger; content inside the dialog goes in body.

Caller-controlled pending

When you already run the action yourself — typically a React Query mutation — pass pending. Its presence (even false) hands the lifecycle to you: confirming calls onConfirm and never closes the dialog, so it stays locked on pendingText until you close it when the mutation settles. While pending is true both buttons are disabled and the dialog cannot be dismissed. A rejected promise from onConfirm still shows its message.

const publish = useMutation({
  mutationFn: () => api.publish(id),
  onSuccess: () => setOpen(false),
})

<ConfirmActionDialog
  open={open}
  onOpenChange={setOpen}
  title={<>Publish <strong>{name}</strong>?</>}
  description='Candidates see it right away.'
  pending={publish.isPending}
  pendingText='Publishing...'
  onConfirm={() => publish.mutate()}
/>

Free content and custom validation

title and description take JSX as well as text or msg() (the description renders inside a <p>, so keep it phrasing content). body renders anything between the description and the buttons. For fields, prefer inputs with validate: it receives the field value and every value, and returns the message to show (text or msg()) or undefined. Messages appear from the first confirm attempt on and update as the user types.

<ConfirmActionDialog
  title='Write off invoice?'
  variant='destructive'
  body={<p className='text-sm'>Balance: {formatMoney(invoice.balance)}</p>}
  inputs={[
    { name: 'date', label: 'Effective date', type: 'date', required: true },
    {
      name: 'word',
      label: 'Type WRITE OFF to confirm',
      type: 'text',
      validate: value => (value === 'WRITE OFF' ? undefined : 'Type WRITE OFF'),
    },
  ]}
  onConfirm={({ date }) => invoicesApi.writeOff(invoice.id, date)}
>
  <Button variant='destructive'>Write off</Button>
</ConfirmActionDialog>

For anything inputs cannot express, render your own fields in body and gate the button with confirmDisabled.

Input options

OptionApplies toWhat it does
requiredallblocks while empty or blank
minLength / maxLengthtext, textarealength of the trimmed value; live count while it fails. minLength checks an optional field only once it is filled
min / maxdateYYYY-MM-DD, set on the native input (the picker greys out other days) and validated
validate(value, values)allyour rule: return the message, or undefined
optionsselectstrings, or { value, label } when the value is a code (the label is I18nText)
placeholderallthe select prompt, or the native placeholder
descriptionallhelp text under the field, announced with it
rowstextareavisible rows
suggestionstexta native <datalist>: suggestions, any value still accepted
inputMode / autoCompletetext, textareanative attributes ('decimal', 'off'…)
<ConfirmActionDialog
  title='Remove the record?'
  variant='destructive'
  disableUntilValid
  inputs={[
    { name: 'reason', label: 'Reason', type: 'textarea', required: true, minLength: 5, rows: 3,
      description: 'Kept in the audit trail.' },
  ]}
  onConfirm={({ reason }) => recordsApi.remove(id, reason.trim())}
>
  <Button variant='destructive'>Remove</Button>
</ConfirmActionDialog>

Props

PropTypeDefault
onConfirm(values: Record<string, string>) => unknownrequired
open / onOpenChangeboolean / (open: boolean) => voiduncontrolled
childrenReactNode — the element that opens it—
titleI18nText | ReactNodelocalized "Confirm action"
descriptionI18nText | ReactNode—
bodyReactNode — between the description and the buttons—
confirmText / cancelTextI18nTextlocalized "Confirm" / "Cancel"
pendingTextI18nText — confirm label while pendingconfirmText
pendingboolean — caller-controlled pending, no auto-closedialog-owned
confirmDisabledbooleanfalse
variant'default' | 'destructive''default'
inputsConfirmDialogInput[] — see Input options—
disableUntilValidboolean — confirm off while an input fails, messages livefalse

Migrating a local copy

Apps that kept their own confirmation dialog (controlled, pending, JSX title, free content) can switch to this one by renaming two props: the local children (content) becomes body, and hard-coded "Confirm"/"Cancel" labels become the localized defaults. open, onOpenChange, title, description, confirmText, pendingText, pending, variant, confirmDisabled and onConfirm keep their meaning.

A local "reason" dialog (a textarea whose confirm unlocks at N characters) becomes an input with required, minLength: N and disableUntilValid: the live count replaces the hand-written check, and the trimmed value is what the rule measures.

In row actions

ActionConfig.confirmDialog renders this dialog with the same title, description, button texts, variant, inputs (every input option) and disableUntilValid. Its inputs reach the action as a second argument, only when inputs are declared (handlers written for the row alone keep working):

actionsConfig: {
  custom: [
    {
      id: 'reject',
      label: 'Reject',
      handler: (row, ctx) => requestsApi.reject(row.id, ctx?.inputs.reason ?? ''),
      confirmDialog: {
        title: row => `Reject ${row.number}?`,
        variant: 'destructive',
        inputs: [{ name: 'reason', label: 'Reason', type: 'textarea', required: true }],
      },
    },
  ],
},

A failing row action shows its error toast as before and keeps the dialog open for a retry.

On this page