Skip to content
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

Add password dialog to apputils #7855

Merged
merged 2 commits into from Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/source/developer/ui_helpers.rst
Expand Up @@ -28,6 +28,7 @@ package within the ``InputDialog`` namespace. There are four helpers:
* ``getItem`` : request a item from a list; the list may be editable.
* ``getNumber`` : request a number; if the user input is not a valid number, NaN is returned.
* ``getText`` : request a short text.
* ``getPassword`` : request a short password.

All dialogs are built on the standard ``Dialog``. Therefore the helper functions each return
a ``Promise`` resolving in a ``Dialog.IResult`` object.
Expand Down Expand Up @@ -66,6 +67,12 @@ a ``Promise`` resolving in a ``Dialog.IResult`` object.
console.log('text ' + value.value);
});

// Request a text
InputDialog.getPassword({ title: 'Input password' }).then(value => {
console.log('A password was input');
});


File Dialogs
''''''''''''

Expand Down
53 changes: 53 additions & 0 deletions packages/apputils/src/inputdialog.ts
Expand Up @@ -190,6 +190,27 @@ export namespace InputDialog {
focusNodeSelector: 'input'
});
}

/**
* Create and show a input dialog for a password.
*
* @param options - The dialog setup options.
*
* @returns A promise that resolves with whether the dialog was accepted
*/
export function getPassword(
options: ITextOptions
): Promise<Dialog.IResult<string>> {
return showDialog({
...options,
body: new InputPasswordDialog(options),
buttons: [
Dialog.cancelButton({ label: options.cancelLabel }),
Dialog.okButton({ label: options.okLabel })
],
focusNodeSelector: 'input'
});
}
}

/**
Expand Down Expand Up @@ -312,6 +333,38 @@ class InputTextDialog extends InputDialogBase<string> {
}
}

/**
* Widget body for input password dialog
*/
class InputPasswordDialog extends InputDialogBase<string> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be simpler to just inherit InputTextDialog ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that it would change the length of the code. The boolean input dialog is also basically the same. All that differ is the constructor.

/**
* InputPasswordDialog constructor
*
* @param options Constructor options
*/
constructor(options: InputDialog.ITextOptions) {
super(options.label);

this._input = document.createElement('input', {});
this._input.classList.add('jp-mod-styled');
this._input.type = 'password';
this._input.value = options.text ? options.text : '';
if (options.placeholder) {
this._input.placeholder = options.placeholder;
}

// Initialize the node
this.node.appendChild(this._input);
}

/**
* Get the text specified by the user
*/
getValue(): string {
return this._input.value;
}
}

/**
* Widget body for input list dialog
*/
Expand Down