UI APIs

prompt

Display a prompt dialog with text input for collecting user input.

Overview

my.prompt displays a dialog that prompts the user to enter text. The dialog includes a text input field, confirmation and cancel buttons, and returns the user's input through the success callback.

Sample Code

my.prompt({
  title: 'Title',
  message: 'Explain the current status and prompt the user solution. It is best not to exceed 3 lines.',
  placeholder: 'Leave a message to a friend',
  okButtonText: 'Confirm',
  cancelButtonText: 'Cancel',
  success: (result) => {
    my.alert({
      title: JSON.stringify(result),
    });
  },
});

Parameters

PropertyTypeRequiredDescription
titleStringNoTitle of prompt box.
messageStringYesText of prompt box, which is "Enter contents here" by default.
placeholderStringNoPrompt text for the entry box.
alignStringNoMessage alignment. Valid values are:
left
center
right

The default value is center for both iOS and Android.
okButtonTextStringNoOK button text, which is OK by default.
cancelButtonTextStringNoCancel button text, which is Cancel by default.
successFunctionNoCallback function upon call success.
failFunctionNoCallback function upon call failure.
completeFunctionNoCallback function upon call completion (to be executed upon either call success or failure).

Success Callback Function

The incoming parameter is of the Object type with the following attributes:

PropertyTypeDescription
okBooleanClick OK to return true; click Cancel to return false.
inputValueStringWhen OK is true, return the user's entry.

Usage Notes

  • The message parameter is required
  • Use placeholder to provide helpful hints about what should be entered
  • The align property controls message text alignment (left, center, or right)
  • Check result.ok to determine if the user confirmed or cancelled:
    • true = OK button clicked, inputValue contains user input
    • false = Cancel button clicked
  • Default button texts are "OK" and "Cancel" if not customized

Examples

Basic Prompt

my.prompt({
  title: 'Enter Name',
  message: 'Please enter your full name',
  placeholder: 'John Doe',
  success: (result) => {
    if (result.ok) {
      console.log('User entered:', result.inputValue);
    } else {
      console.log('User cancelled');
    }
  },
});

Custom Alignment and Buttons

my.prompt({
  title: 'Feedback',
  message: 'How can we improve?',
  placeholder: 'Share your thoughts',
  align: 'left',
  okButtonText: 'Submit',
  cancelButtonText: 'Skip',
  success: (result) => {
    if (result.ok && result.inputValue) {
      // Process feedback
      console.log('Feedback:', result.inputValue);
    }
  },
});

Validation Example

my.prompt({
  title: 'Enter Email',
  message: 'Please provide your email address',
  placeholder: 'email@example.com',
  success: (result) => {
    if (result.ok) {
      const email = result.inputValue;
      if (email && email.includes('@')) {
        console.log('Valid email:', email);
      } else {
        my.alert({
          title: 'Invalid Email',
          content: 'Please enter a valid email address',
        });
      }
    }
  },
});