Skip to content

Implemented the prompt types - "password" and "number". #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ Displays a native dialog box that is more customizable than the browser's `promp

- __defaultText__: Default textbox input value (`String`) (Optional, Default: empty string)

- __inputType__: Textbox input type (`String`) (Optional, Default: 'text'. Valid values : 'text', 'password', 'number')

### promptCallback

The `promptCallback` executes when the user presses one of the buttons
Expand All @@ -178,7 +180,8 @@ contains the following properties:
onPrompt, // callback to invoke
'Registration', // title
['Ok','Exit'], // buttonLabels
'Jane Doe' // defaultText
'Jane Doe', // defaultText
'text' // inputType
);

### Supported Platforms
Expand Down
11 changes: 9 additions & 2 deletions src/android/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ else if (action.equals("confirm")) {
return true;
}
else if (action.equals("prompt")) {
this.prompt(args.getString(0), args.getString(1), args.getJSONArray(2), args.getString(3), callbackContext);
this.prompt(args.getString(0), args.getString(1), args.getJSONArray(2), args.getString(3), args.getString(4), callbackContext);
return true;
}
else if (action.equals("activityStart")) {
Expand Down Expand Up @@ -278,9 +278,10 @@ public void onCancel(DialogInterface dialog)
* @param message The message the dialog should display
* @param title The title of the dialog
* @param buttonLabels A comma separated list of button labels (Up to 3 buttons)
* @param inputType The type of prompt input.
* @param callbackContext The callback context.
*/
public synchronized void prompt(final String message, final String title, final JSONArray buttonLabels, final String defaultText, final CallbackContext callbackContext) {
public synchronized void prompt(final String message, final String title, final JSONArray buttonLabels, final String defaultText, final String inputType, final CallbackContext callbackContext) {

final CordovaInterface cordova = this.cordova;

Expand All @@ -295,6 +296,12 @@ But for some android versions is not visible (for example 5.1.1).
int promptInputTextColor = resources.getColor(android.R.color.primary_text_light);
promptInput.setTextColor(promptInputTextColor);
promptInput.setText(defaultText);

if (inputType.equals("password")) promptInput.setInputType(0x00000081);
else if (inputType.equals("number")) promptInput.setInputType(0x00000002);

promptInput.setSelection(promptInput.getText().length()); // Moves the focus to the end of the text.

AlertDialog.Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
dlg.setMessage(message);
dlg.setTitle(title);
Expand Down
17 changes: 12 additions & 5 deletions src/ios/CDVNotification.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ @implementation CDVNotification
* defaultText The input text for the textbox (if textbox exists).
* callbackId The commmand callback id.
* dialogType The type of alert view [alert | prompt].
* inputType The type of prompt input.
*/
- (void)showDialogWithMessage:(NSString*)message title:(NSString*)title buttons:(NSArray*)buttons defaultText:(NSString*)defaultText callbackId:(NSString*)callbackId dialogType:(NSString*)dialogType
- (void)showDialogWithMessage:(NSString*)message title:(NSString*)title buttons:(NSArray*)buttons defaultText:(NSString*)defaultText callbackId:(NSString*)callbackId dialogType:(NSString*)dialogType inputType:(NSString*)inputType
{

int count = (int)[buttons count];
Expand Down Expand Up @@ -92,6 +93,11 @@ - (void)showDialogWithMessage:(NSString*)message title:(NSString*)title buttons:
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.text = defaultText;
}];

if ([inputType isEqualToString:@"password"])
[alertController.textFields objectAtIndex:0].secureTextEntry = YES;
else if ([inputType isEqualToString:@"number"])
[alertController.textFields objectAtIndex:0].keyboardType = UIKeyboardTypeNumberPad;
}

if(!alertList)
Expand Down Expand Up @@ -142,7 +148,7 @@ - (void)alert:(CDVInvokedUrlCommand*)command
NSString* title = [command argumentAtIndex:1];
NSString* buttons = [command argumentAtIndex:2];

[self showDialogWithMessage:message title:title buttons:@[buttons] defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
[self showDialogWithMessage:message title:title buttons:@[buttons] defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT inputType:@""];
}

- (void)confirm:(CDVInvokedUrlCommand*)command
Expand All @@ -152,7 +158,7 @@ - (void)confirm:(CDVInvokedUrlCommand*)command
NSString* title = [command argumentAtIndex:1];
NSArray* buttons = [command argumentAtIndex:2];

[self showDialogWithMessage:message title:title buttons:buttons defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
[self showDialogWithMessage:message title:title buttons:buttons defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT inputType:@""];
}

- (void)prompt:(CDVInvokedUrlCommand*)command
Expand All @@ -162,8 +168,9 @@ - (void)prompt:(CDVInvokedUrlCommand*)command
NSString* title = [command argumentAtIndex:1];
NSArray* buttons = [command argumentAtIndex:2];
NSString* defaultText = [command argumentAtIndex:3];

[self showDialogWithMessage:message title:title buttons:buttons defaultText:defaultText callbackId:callbackId dialogType:DIALOG_TYPE_PROMPT];
NSString* inputType = [command argumentAtIndex:4];

[self showDialogWithMessage:message title:title buttons:buttons defaultText:defaultText callbackId:callbackId dialogType:DIALOG_TYPE_PROMPT inputType:inputType];
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/windows/NotificationProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ if (typeof toStaticHTML !== 'undefined') {

// Windows does not provide native UI for promp dialog so we use some
// simple html-based implementation until it is available
function createPromptDialog (title, message, buttons, defaultText, callback) {
function createPromptDialog (title, message, buttons, defaultText, inputType, callback) {

var isPhone = cordova.platformId === 'windows' && WinJS.Utilities.isPhone;
var isWindows = !!cordova.platformId.match(/windows/);
Expand Down Expand Up @@ -81,6 +81,7 @@ function createPromptDialog (title, message, buttons, defaultText, callback) {
dlg.querySelector('#lbl-message').appendChild(document.createTextNode(message));
dlg.querySelector('#prompt-input').setAttribute('placeholder', defaultText);
dlg.querySelector('#prompt-input').setAttribute('value', defaultText);
dlg.querySelector('#prompt-input').setAttribute('type', inputType);

function makeButtonCallback (idx) {
return function () {
Expand Down Expand Up @@ -174,9 +175,10 @@ module.exports = {
var title = args[1];
var buttons = args[2];
var defaultText = args[3];
var inputType = args[4];

try {
createPromptDialog(title, message, buttons, defaultText, function (evt) {
createPromptDialog(title, message, buttons, defaultText, inputType, function (evt) {
isAlertShowing = false;
if (win) {
win(evt);
Expand Down
9 changes: 7 additions & 2 deletions www/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,16 @@ module.exports = {
* @param {String} title Title of the dialog (default: "Prompt")
* @param {Array} buttonLabels Array of strings for the button labels (default: ["OK","Cancel"])
* @param {String} defaultText Textbox input value (default: empty string)
* @param {String} inputType Textbox input type (default: empty string)
*/
prompt: function (message, resultCallback, title, buttonLabels, defaultText) {
prompt: function (message, resultCallback, title, buttonLabels, defaultText, inputType) {
var _message = (typeof message === 'string' ? message : JSON.stringify(message));
var _title = (typeof title === 'string' ? title : 'Prompt');
var _buttonLabels = (buttonLabels || ['OK', 'Cancel']);
var _inputType = (inputType || 'text');

var _allowedInputType = ['text', 'password', 'number'];
if (_allowedInputType.includes(_inputType) === false) _inputType = 'text';

// Strings are deprecated!
if (typeof _buttonLabels === 'string') {
Expand All @@ -92,7 +97,7 @@ module.exports = {
_buttonLabels = convertButtonLabels(_buttonLabels);

var _defaultText = (defaultText || '');
exec(resultCallback, null, 'Notification', 'prompt', [_message, _title, _buttonLabels, _defaultText]);
exec(resultCallback, null, 'Notification', 'prompt', [_message, _title, _buttonLabels, _defaultText, _inputType]);
},

/**
Expand Down