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
onConfirmis pending, both buttons are disabled, the confirm button shows a spinner (andpendingText, if set) and the dialog cannot be dismissed. - Success: when
onConfirmresolves, the dialog closes. - Failure: when it rejects, the dialog stays open and shows the error's
message(or a localized fallback). Resolvefalseinstead to stay open without a message — for an error you already reported, such as in a toast. - Inputs:
text,textarea,selectanddate(the native date input, valueYYYY-MM-DD). Arequiredinput left empty (or blank), a trimmed value outsideminLength/maxLength, adateoutsidemin/maxand an input whosevalidatereturns a message block the confirm and are marked invalid. Values reachonConfirmkeyed byname; every opening starts again fromdefaultValue. - Live counts: a
minLength/maxLengthinput shows how many characters are missing (or over) as the user types, before any attempt, so the reason is visible without clicking. disableUntilValidkeeps 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.confirmDisableddisables 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
| Option | Applies to | What it does |
|---|---|---|
required | all | blocks while empty or blank |
minLength / maxLength | text, textarea | length of the trimmed value; live count while it fails. minLength checks an optional field only once it is filled |
min / max | date | YYYY-MM-DD, set on the native input (the picker greys out other days) and validated |
validate(value, values) | all | your rule: return the message, or undefined |
options | select | strings, or { value, label } when the value is a code (the label is I18nText) |
placeholder | all | the select prompt, or the native placeholder |
description | all | help text under the field, announced with it |
rows | textarea | visible rows |
suggestions | text | a native <datalist>: suggestions, any value still accepted |
inputMode / autoComplete | text, textarea | native 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
| Prop | Type | Default |
|---|---|---|
onConfirm | (values: Record<string, string>) => unknown | required |
open / onOpenChange | boolean / (open: boolean) => void | uncontrolled |
children | ReactNode — the element that opens it | — |
title | I18nText | ReactNode | localized "Confirm action" |
description | I18nText | ReactNode | — |
body | ReactNode — between the description and the buttons | — |
confirmText / cancelText | I18nText | localized "Confirm" / "Cancel" |
pendingText | I18nText — confirm label while pending | confirmText |
pending | boolean — caller-controlled pending, no auto-close | dialog-owned |
confirmDisabled | boolean | false |
variant | 'default' | 'destructive' | 'default' |
inputs | ConfirmDialogInput[] — see Input options | — |
disableUntilValid | boolean — confirm off while an input fails, messages live | false |
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.