Skip to content

Commit c834181

Browse files
NickIlievdtopuzov
authored andcommitted
feat: add hints options for username and password fields (#6416)
* feat: add hints options for username and paossword fileds in the login dialog * refactor arguments checks * parsing of login options moved to common file * add zero login options check * parsing optimizations * api refernces improvments * refactor: argiment passing * review code improvments
1 parent 11d3884 commit c834181

4 files changed

Lines changed: 67 additions & 56 deletions

File tree

‎tns-core-modules/ui/dialogs/dialogs-common.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { Color } from "../../color";
44
import { Page } from "../page";
55
import { isIOS } from "../../platform";
66
import * as frameModule from "../frame";
7+
import { LoginOptions } from "./dialogs";
8+
import { isObject, isString } from "../../utils/types";
79

810
export const STRING = "string";
911
export const PROMPT = "Prompt";
@@ -152,3 +154,34 @@ export function getTextFieldColor(): Color {
152154
export function isDialogOptions(arg): boolean {
153155
return arg && (arg.message || arg.title);
154156
}
157+
158+
export function parseLoginOptions(args: any[]): LoginOptions {
159+
// Handle options object first
160+
if (args.length === 1 && isObject(args[0])) {
161+
return args[0];
162+
}
163+
164+
let options: LoginOptions = { title: LOGIN, okButtonText: OK, cancelButtonText: CANCEL };
165+
166+
if (isString(args[0])) {
167+
options.message = args[0];
168+
}
169+
170+
if (isString(args[1])) {
171+
options.userNameHint = args[1];
172+
}
173+
174+
if (isString(args[2])) {
175+
options.passwordHint = args[2];
176+
}
177+
178+
if (isString(args[3])) {
179+
options.userName = args[3];
180+
}
181+
182+
if (isString(args[4])) {
183+
options.password = args[4];
184+
}
185+
186+
return options;
187+
}

‎tns-core-modules/ui/dialogs/dialogs.android.ts‎

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Android specific dialogs functions implementation.
33
*/
44
import { DialogOptions, ConfirmOptions, PromptOptions, PromptResult, LoginOptions, LoginResult, ActionOptions } from ".";
5-
import { getLabelColor, getButtonColors, isDialogOptions, inputType, capitalizationType, ALERT, OK, CONFIRM, CANCEL, PROMPT, LOGIN } from "./dialogs-common";
5+
import { getLabelColor, getButtonColors, isDialogOptions, inputType, capitalizationType, ALERT, OK, CONFIRM, CANCEL, PROMPT, parseLoginOptions } from "./dialogs-common";
66
import { android as androidApp } from "../../application";
77

88
export * from "./dialogs-common";
@@ -223,31 +223,8 @@ export function prompt(arg: any): Promise<PromptResult> {
223223
});
224224
}
225225

226-
export function login(arg: any): Promise<LoginResult> {
227-
let options: LoginOptions;
228-
const defaultOptions = { title: LOGIN, okButtonText: OK, cancelButtonText: CANCEL };
229-
230-
if (arguments.length === 1) {
231-
if (isString(arguments[0])) {
232-
options = defaultOptions;
233-
options.message = arguments[0];
234-
} else {
235-
options = arguments[0];
236-
}
237-
} else if (arguments.length === 2) {
238-
if (isString(arguments[0]) && isString(arguments[1])) {
239-
options = defaultOptions;
240-
options.message = arguments[0];
241-
options.userName = arguments[1];
242-
}
243-
} else if (arguments.length === 3) {
244-
if (isString(arguments[0]) && isString(arguments[1]) && isString(arguments[2])) {
245-
options = defaultOptions;
246-
options.message = arguments[0];
247-
options.userName = arguments[1];
248-
options.password = arguments[2];
249-
}
250-
}
226+
export function login(...args: any[]): Promise<LoginResult> {
227+
let options: LoginOptions = parseLoginOptions(args);
251228

252229
return new Promise<LoginResult>((resolve, reject) => {
253230
try {
@@ -256,10 +233,15 @@ export function login(arg: any): Promise<LoginResult> {
256233
const alert = createAlertDialog(options);
257234

258235
const userNameInput = new android.widget.EditText(context);
236+
237+
userNameInput.setHint(options.userNameHint ? options.userNameHint : "");
259238
userNameInput.setText(options.userName ? options.userName : "");
260239

261240
const passwordInput = new android.widget.EditText(context);
262241
passwordInput.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
242+
passwordInput.setTypeface(android.graphics.Typeface.DEFAULT);
243+
244+
passwordInput.setHint(options.userNameHint ? options.userNameHint : "");
263245
passwordInput.setText(options.password ? options.password : "");
264246

265247
const layout = new android.widget.LinearLayout(context);
@@ -278,11 +260,9 @@ export function login(arg: any): Promise<LoginResult> {
278260
});
279261

280262
showDialog(alert);
281-
282263
} catch (ex) {
283264
reject(ex);
284265
}
285-
286266
});
287267
}
288268

‎tns-core-modules/ui/dialogs/dialogs.d.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,20 @@ export function prompt(options: PromptOptions): Promise<PromptResult>;
9898
/**
9999
* The login() method displays a login dialog box that prompts the visitor for user name and password.
100100
* @param message The text to display in the dialog box.
101+
* @param userNameHint The default text to display as a hint in the username input. Optional.
102+
* @param passwordHint The default text to display as a hint in the password input. Optional.
101103
* @param userName The default text to display in the user name input box. Optional.
102104
* @param password The default text to display in the password input box. Optional.
103105
*/
104-
export function login(message: string, userName?: string, password?: string): Promise<LoginResult>;
106+
export function login(message: string, userNameHint?: string, passwordHint?: string, userName?: string, password?: string): Promise<LoginResult>;
107+
108+
/**
109+
* The login() method displays a login dialog box that prompts the visitor for user name and password.
110+
* @param message The text to display in the dialog box.
111+
* @param userNameHint The default text to display as a hint in the username input. Optional.
112+
* @param passwordHint The default text to display as a hint in the password input. Optional.
113+
*/
114+
export function login(message: string, userNameHint?: string, passwordHint?: string): Promise<LoginResult>;
105115

106116
/**
107117
* The login() method displays a login dialog box that prompts the visitor for user name and password.
@@ -223,6 +233,16 @@ export interface PromptOptions extends ConfirmOptions {
223233
* Provides options for the login dialog.
224234
*/
225235
export interface LoginOptions extends ConfirmOptions {
236+
/**
237+
* Gets or sets the default text to display as hint in the user name input box.
238+
*/
239+
userNameHint?: string;
240+
241+
/**
242+
* Gets or sets the default text to display as hint in the password input box.
243+
*/
244+
passwordHint?: string;
245+
226246
/**
227247
* Gets or sets the default text to display in the user name input box.
228248
*/

‎tns-core-modules/ui/dialogs/dialogs.ios.ts‎

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import { View, ios as iosView } from "../core/view";
55
import { ConfirmOptions, PromptOptions, PromptResult, LoginOptions, LoginResult, ActionOptions } from ".";
6-
import { getCurrentPage, getLabelColor, getButtonColors, getTextFieldColor, isDialogOptions, inputType, capitalizationType, ALERT, OK, CONFIRM, CANCEL, PROMPT, LOGIN } from "./dialogs-common";
6+
import { getCurrentPage, getLabelColor, getButtonColors, getTextFieldColor, isDialogOptions, inputType, capitalizationType, ALERT, OK, CONFIRM, CANCEL, PROMPT, parseLoginOptions } from "./dialogs-common";
77
import { isString, isDefined, isFunction } from "../../utils/types";
88
import { getRootView } from "../../application";
99

@@ -145,32 +145,8 @@ export function prompt(arg: any): Promise<PromptResult> {
145145
});
146146
}
147147

148-
export function login(): Promise<LoginResult> {
149-
let options: LoginOptions;
150-
151-
let defaultOptions = { title: LOGIN, okButtonText: OK, cancelButtonText: CANCEL };
152-
153-
if (arguments.length === 1) {
154-
if (isString(arguments[0])) {
155-
options = defaultOptions;
156-
options.message = arguments[0];
157-
} else {
158-
options = arguments[0];
159-
}
160-
} else if (arguments.length === 2) {
161-
if (isString(arguments[0]) && isString(arguments[1])) {
162-
options = defaultOptions;
163-
options.message = arguments[0];
164-
options.userName = arguments[1];
165-
}
166-
} else if (arguments.length === 3) {
167-
if (isString(arguments[0]) && isString(arguments[1]) && isString(arguments[2])) {
168-
options = defaultOptions;
169-
options.message = arguments[0];
170-
options.userName = arguments[1];
171-
options.password = arguments[2];
172-
}
173-
}
148+
export function login(...args: any[]): Promise<LoginResult> {
149+
let options: LoginOptions = parseLoginOptions(args);
174150

175151
return new Promise<LoginResult>((resolve, reject) => {
176152
try {
@@ -182,6 +158,7 @@ export function login(): Promise<LoginResult> {
182158

183159
alertController.addTextFieldWithConfigurationHandler((arg: UITextField) => {
184160
arg.placeholder = "Login";
161+
arg.placeholder = options.userNameHint ? options.userNameHint : "";
185162
arg.text = isString(options.userName) ? options.userName : "";
186163

187164
if (textFieldColor) {
@@ -192,6 +169,7 @@ export function login(): Promise<LoginResult> {
192169
alertController.addTextFieldWithConfigurationHandler((arg: UITextField) => {
193170
arg.placeholder = "Password";
194171
arg.secureTextEntry = true;
172+
arg.placeholder = options.passwordHint ? options.passwordHint : "";
195173
arg.text = isString(options.password) ? options.password : "";
196174

197175
if (textFieldColor) {

0 commit comments

Comments
 (0)