Skip to content

Commit

Permalink
feat(modals): add inputText property to prompt for prefilled text (#2475
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jcesarmobile authored Feb 24, 2020
1 parent 8002791 commit a05311d
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static void prompt(final Context context,
final String message,
final Dialogs.OnResultListener listener) {

prompt(context, message, null, null, null, null, listener);
prompt(context, message, null, null, null, null, null, listener);
}

public static void prompt(final Context context,
Expand All @@ -146,11 +146,13 @@ public static void prompt(final Context context,
final String okButtonTitle,
final String cancelButtonTitle,
final String inputPlaceholder,
final String inputText,
final Dialogs.OnResultListener listener) {
final String promptTitle = title == null ? "Prompt" : title;
final String promptOkButtonTitle = okButtonTitle == null ? "OK" : okButtonTitle;
final String promptCancelButtonTitle = cancelButtonTitle == null ? "Cancel" : cancelButtonTitle;
final String promptInputPlaceholder = inputPlaceholder == null ? "" : inputPlaceholder;
final String promptInputText = inputText == null ? "" : inputText;

new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
Expand All @@ -159,6 +161,7 @@ public void run() {
final EditText input = new EditText(context);

input.setHint(promptInputPlaceholder);
input.setText(promptInputText);

builder
.setMessage(message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void prompt(final PluginCall call) {
final String okButtonTitle = call.getString("okButtonTitle", "OK");
final String cancelButtonTitle = call.getString("cancelButtonTitle", "Cancel");
final String inputPlaceholder = call.getString("inputPlaceholder", "");
final String inputText = call.getString("inputText", "");

if(title == null || message == null) {
call.error("Please provide a title or message for the alert");
Expand All @@ -89,7 +90,7 @@ public void prompt(final PluginCall call) {
return;
}

Dialogs.prompt(c, message, title, okButtonTitle, cancelButtonTitle, inputPlaceholder, new Dialogs.OnResultListener() {
Dialogs.prompt(c, message, title, okButtonTitle, cancelButtonTitle, inputPlaceholder, inputText, new Dialogs.OnResultListener() {
@Override
public void onResult(boolean value, boolean didCancel, String inputValue) {
JSObject ret = new JSObject();
Expand Down
1 change: 1 addition & 0 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,7 @@ export interface PromptOptions {
okButtonTitle?: string;
cancelButtonTitle?: string;
inputPlaceholder?: string;
inputText?: string;
}

export interface ConfirmOptions {
Expand Down
2 changes: 1 addition & 1 deletion core/src/web/modals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ModalsPluginWeb extends WebPlugin implements ModalsPlugin {
}

async prompt(options: PromptOptions): Promise<PromptResult> {
const val = window.prompt(options.message, options.inputPlaceholder || '');
const val = window.prompt(options.message, options.inputText || '');
return Promise.resolve({
value: val,
cancelled: val === null
Expand Down
2 changes: 1 addition & 1 deletion electron/src/electron/modals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class ModalsPluginElectron extends WebPlugin implements ModalsPlugin {
}

async prompt(options: PromptOptions): Promise<PromptResult> {
const val = window.prompt(options.message, options.inputPlaceholder || '');
const val = window.prompt(options.message, options.inputText || '');
return Promise.resolve({
value: val,
cancelled: val === null
Expand Down
2 changes: 2 additions & 0 deletions ios/Capacitor/Capacitor/Plugins/Modals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ public class CAPModalsPlugin : CAPPlugin {
let okButtonTitle = call.options["okButtonTitle"] as? String ?? "OK"
let cancelButtonTitle = call.options["cancelButtonTitle"] as? String ?? "Cancel"
let inputPlaceholder = call.options["inputPlaceholder"] as? String ?? ""
let inputText = call.options["inputText"] as? String ?? ""

let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)

DispatchQueue.main.async {

alert.addTextField { (textField) in
textField.placeholder = inputPlaceholder
textField.text = inputText
}

alert.addAction(UIAlertAction(title: okButtonTitle, style: UIAlertAction.Style.default, handler: { (action) -> Void in
Expand Down

0 comments on commit a05311d

Please sign in to comment.