Skip to content

Dialogs

promptFields() shows a dialog built from a list of fields and resolves to their values, or null when the player closes it.

ts
import { promptFields } from '@vttforge/core';

const answer = await promptFields(
  [
    { name: 'name', type: 'text', label: 'MY_SYSTEM.Name', value: 'Rope', required: true },
    { name: 'quantity', type: 'number', label: 'MY_SYSTEM.Quantity', value: 1, min: 1 },
    { name: 'kind', type: 'select', label: 'MY_SYSTEM.Kind', options: { stowed: 'Stowed', equipped: 'Equipped' } },
    { name: 'magic', type: 'checkbox', label: 'MY_SYSTEM.Magic' },
  ],
  { title: 'MY_SYSTEM.NewGear', ok: 'MY_SYSTEM.Create' },
);
if (!answer) return;
// answer: { name: string; quantity: number; kind: string; magic: boolean }

The result is typed from the fields: a number field comes back as a number, a checkbox as a boolean, the rest as string. Foundry's form reader does the casting from the input type. The first field has the focus when the dialog opens.

Every field is a form group made with Foundry's own input helpers, so it looks like the rest of the interface and works in both themes. Labels, hints, select option labels, the title and the button label may be localization keys or plain text.

Field types

TypeOptionsValue
textvalue, placeholder, requiredstring
textareavalue, placeholder, rowsstring
numbervalue, min, max, step (default 1, 'any' for decimals)number
checkboxvalueboolean
selectvalue, options as { value: label } or a list of { value, label, group? }, blankstring

Every field takes name, label and an optional hint. Two fields with the same name are refused, since the result has one key per name.

Dialog options

OptionWhat it does
titleWindow title
okLabel of the confirm button. Default: Foundry's
iconIcon class of the confirm button
contentHTML shown above the fields, cleaned with foundry.utils.cleanHTML first
modalBlock the rest of the interface until answered
rejectCloseReject instead of resolving null when the dialog is dismissed

A custom dialog

promptFieldGroup(field) returns the form group for one field, for a DialogV2 you configure yourself: extra buttons, your own content around the inputs, a different callback.

MIT licensed.