Skip to main content

Modal API

The modal object provides methods for displaying various types of dialogs and modals to the user. This API is built on top of Ant Design's Modal component and provides Promise-based methods for handling user interactions.

Available Methods

modal.showForm

Displays a Shesha form inside a modal dialog. The method returns a Promise that resolves when the form is submitted successfully or rejects when the modal is cancelled.

modal.showForm<T = any>(args: ShowFormModalArgs) => Promise<T>

Arguments:

PropertyTypeDescriptionRequired
formIdstring | FormIdentifierThe identifier of the form to display. Can be a string path or an object with name and module properties.Yes
titlestringThe title displayed in the modal header.No
widthModalWidthThe modal width. Can be 'small' (40%), 'medium' (60%), 'large' (80%), 'full' (100%), or a custom value like '800px' or '60%'.No
modeFormModeThe form mode. Can be 'edit' or 'readonly'. Defaults to 'edit'.No
formArgumentsanyArguments to pass to the form.No
initialValuesanyInitial values for the form fields.No
showCloseIconbooleanWhether to show the close icon in the modal header. Defaults to true.No
footerButtonsstringFooter buttons configuration. Can be 'default', 'custom', or 'none'. Defaults to 'default'.No

Example:

// Show a form and wait for the result
try {
const result = await modal.showForm({
formId: { name: 'person-details', module: 'my-app' },
title: 'Edit Person',
width: 'large',
mode: 'edit',
initialValues: { firstName: 'John', lastName: 'Doe' }
});
console.log('Form submitted with:', result);
} catch (error) {
console.log('Form was cancelled');
}

modal.confirm

Displays a confirmation dialog with Yes/No buttons. Returns a Promise that resolves to true if the user clicks "Yes", or false if they click "No" or close the dialog.

modal.confirm(args: ConfirmModalArgs) => Promise<boolean>

Arguments:

PropertyTypeDescriptionRequired
titlestringThe dialog title. Defaults to 'Confirm'.No
contentstringThe message content to display.Yes
okTextstringText for the OK/Yes button. Defaults to 'Yes'.No
cancelTextstringText for the Cancel/No button. Defaults to 'No'.No
okTypestringType of the OK button. Can be 'primary', 'default', 'dashed', 'link', or 'text'. Defaults to 'primary'.No

Example:

const confirmed = await modal.confirm({
title: 'Delete Record',
content: 'Are you sure you want to delete this record? This action cannot be undone.',
okText: 'Delete',
cancelText: 'Cancel',
okType: 'primary'
});

if (confirmed) {
// Proceed with deletion
await deleteRecord();
} else {
// User cancelled
console.log('Deletion cancelled');
}

modal.success

Displays a success message dialog with an OK button.

modal.success(args: AlertModalArgs) => Promise<void>

Arguments:

PropertyTypeDescriptionRequired
titlestringThe dialog title. Defaults to 'Success'.No
contentstringThe message content to display.Yes
okTextstringText for the OK button. Defaults to 'OK'.No

Example:

await modal.success({
title: 'Operation Complete',
content: 'The record has been successfully saved.'
});

modal.error

Displays an error message dialog with an OK button.

modal.error(args: AlertModalArgs) => Promise<void>

Arguments:

PropertyTypeDescriptionRequired
titlestringThe dialog title. Defaults to 'Error'.No
contentstringThe error message to display.Yes
okTextstringText for the OK button. Defaults to 'OK'.No

Example:

await modal.error({
title: 'Operation Failed',
content: 'Unable to save the record. Please try again later.'
});

modal.warning

Displays a warning message dialog with an OK button.

modal.warning(args: AlertModalArgs) => Promise<void>

Arguments:

PropertyTypeDescriptionRequired
titlestringThe dialog title. Defaults to 'Warning'.No
contentstringThe warning message to display.Yes
okTextstringText for the OK button. Defaults to 'OK'.No

Example:

await modal.warning({
title: 'Unsaved Changes',
content: 'You have unsaved changes that will be lost if you navigate away.'
});

modal.info

Displays an informational message dialog with an OK button.

modal.info(args: AlertModalArgs) => Promise<void>

Arguments:

PropertyTypeDescriptionRequired
titlestringThe dialog title. Defaults to 'Information'.No
contentstringThe informational message to display.Yes
okTextstringText for the OK button. Defaults to 'OK'.No

Example:

await modal.info({
title: 'Update Available',
content: 'A new version of the application is available. Please refresh to update.'
});

modal.showContent

Displays a modal with custom content (HTML or React elements). The method returns a Promise that resolves when the modal is closed with a positive result or rejects when cancelled.

modal.showContent<T = any>(args: ShowContentModalArgs) => Promise<T>

Arguments:

PropertyTypeDescriptionRequired
titlestringThe modal title.No
contentstringThe custom content to display. Can be an HTML string or React elements.Yes
widthModalWidthThe modal width. Can be 'small', 'medium', 'large', 'full', or a custom value.No
showCloseIconbooleanWhether to show the close icon in the modal header. Defaults to true.No
footerstringCustom footer content.No

Example:

const result = await modal.showContent({
title: 'Custom Dialog',
content: '<div style="padding: 20px;"><h3>Hello World</h3><p>This is custom HTML content.</p></div>',
width: 'medium'
});

Common Patterns

Sequential Dialogs

You can chain multiple modal calls together:

const confirmed = await modal.confirm({
title: 'Submit Form',
content: 'Are you ready to submit?'
});

if (confirmed) {
try {
await submitData();
await modal.success({
title: 'Success',
content: 'Your submission has been received.'
});
} catch (error) {
await modal.error({
title: 'Failed',
content: 'Submission failed. Please try again.'
});
}
}

Form Result Handling

When using showForm, the promise resolves with the form's submitted values:

try {
const result = await modal.showForm({
formId: 'create-user',
title: 'Create New User'
});

// result contains the form data
console.log('New user created:', result);

await modal.success({
title: 'User Created',
content: `User ${result.firstName} ${result.lastName} has been created successfully.`
});
} catch {
console.log('User creation cancelled');
}

Error Handling with Modals

try {
await performRiskyOperation();
} catch (error) {
await modal.error({
title: 'Operation Failed',
content: error.message || 'An unexpected error occurred.'
});
}