Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: autofillType property for edit text base #9478

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

Merged
merged 2 commits into from
Aug 11, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/core/core-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export namespace CoreTypes {
export const email = 'email';
export const integer = 'integer';
}
export type AutofillType = 'username' | 'password' | 'none' | string;
export module AutofillType {
export const username = 'username';
export const password = 'password';
export const none = 'none';
}

export type ReturnKeyButtonType = 'done' | 'next' | 'go' | 'search' | 'send';
export module ReturnKeyType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export abstract class EditableTextBase extends TextBase implements EditableTextB
public returnKeyType: CoreTypes.ReturnKeyButtonType;
public updateTextTrigger: CoreTypes.UpdateTextTriggerType;
public autocapitalizationType: CoreTypes.AutocapitalizationInputType;
public autofillType: CoreTypes.AutofillType;
public editable: boolean;
public autocorrect: boolean;
public hint: string;
Expand Down Expand Up @@ -50,6 +51,11 @@ placeholderColorProperty.register(Style);

const keyboardTypeConverter = makeParser<CoreTypes.KeyboardInputType>(makeValidator<CoreTypes.KeyboardInputType>(CoreTypes.KeyboardType.datetime, CoreTypes.KeyboardType.phone, CoreTypes.KeyboardType.number, CoreTypes.KeyboardType.url, CoreTypes.KeyboardType.email, CoreTypes.KeyboardType.integer), true);

const autofillTypeConverter = makeParser<CoreTypes.AutofillType>(makeValidator<CoreTypes.AutofillType>(CoreTypes.AutofillType.username, CoreTypes.AutofillType.password, CoreTypes.AutofillType.none), true);

export const autofillTypeProperty = new Property<EditableTextBase, CoreTypes.AutofillType>({ name: 'autofillType', valueConverter: autofillTypeConverter });
autofillTypeProperty.register(EditableTextBase);

export const keyboardTypeProperty = new Property<EditableTextBase, CoreTypes.KeyboardInputType>({ name: 'keyboardType', valueConverter: keyboardTypeConverter });
keyboardTypeProperty.register(EditableTextBase);

Expand Down
56 changes: 55 additions & 1 deletion packages/core/ui/editable-text-base/index.android.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { EditableTextBase as EditableTextBaseCommon, keyboardTypeProperty, returnKeyTypeProperty, editableProperty, autocapitalizationTypeProperty, autocorrectProperty, hintProperty, placeholderColorProperty, maxLengthProperty } from './editable-text-base-common';
import { EditableTextBase as EditableTextBaseCommon, autofillTypeProperty, keyboardTypeProperty, returnKeyTypeProperty, editableProperty, autocapitalizationTypeProperty, autocorrectProperty, hintProperty, placeholderColorProperty, maxLengthProperty } from './editable-text-base-common';
import { textTransformProperty, textProperty, resetSymbol } from '../text-base';
import { Color } from '../../color';
import { ad } from '../../utils';
import { CoreTypes } from '../../core-types';
import { Device } from '../../platform';
import lazy from '../../utils/lazy';

export * from './editable-text-base-common';

const sdkVersion = lazy(() => parseInt(Device.sdkVersion));

//https://github.com/NativeScript/NativeScript/issues/2942
export let dismissKeyboardTimeoutId: NodeJS.Timer;
export let dismissKeyboardOwner: WeakRef<EditableTextBase>;
Expand Down Expand Up @@ -138,6 +143,8 @@ function initializeEditTextListeners(): void {
EditTextListeners = EditTextListenersImpl;
}

let apiLevel: number;

export abstract class EditableTextBase extends EditableTextBaseCommon {
/* tslint:disable */
_dirtyTextAccumulator: string;
Expand All @@ -157,6 +164,9 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {
}

public createNativeView() {
if (!apiLevel) {
apiLevel = sdkVersion();
}
return new android.widget.EditText(this._context);
}

Expand Down Expand Up @@ -293,6 +303,50 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {
this._setInputType(newInputType);
}

[autofillTypeProperty.setNative](value: CoreTypes.AutofillType) {
if (apiLevel < 26) {
return;
}
let newOptions;
switch (value) {
case 'phone':
newOptions = 'phone'; // android.view.View.AUTOFILL_HINT_PHONE
break;
case 'postalCode':
newOptions = 'postalCode'; // android.view.View.AUTOFILL_HINT_POSTAL_CODE
break;
case 'creditCardNumber':
newOptions = 'creditCardNumber'; // android.view.View.AUTOFILL_HINT_CREDIT_CARD_NUMBER
break;
case 'email':
newOptions = 'emailAddress'; // android.view.View.AUTOFILL_HINT_EMAIL_ADDRESS
break;
case 'name':
newOptions = 'name'; // android.view.View.AUTOFILL_HINT_NAME
break;
case 'username':
newOptions = 'username'; // android.view.View.AUTOFILL_HINT_USERNAME
break;
case 'password':
newOptions = 'password'; // android.view.View.AUTOFILL_HINT_PASSWORD
break;
case 'none':
newOptions = null;
break;
default: {
newOptions = value;
break;
}
}
if (newOptions) {
const array = Array.create(java.lang.String, 1);
array[0] = newOptions;
this.nativeTextViewProtected.setAutofillHints(array);
} else {
this.nativeTextViewProtected.setAutofillHints(null);
}
}

[returnKeyTypeProperty.getDefault](): 'done' | 'next' | 'go' | 'search' | 'send' | string {
const ime = this.nativeTextViewProtected.getImeOptions();
switch (ime) {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/ui/editable-text-base/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export class EditableTextBase extends TextBase {
*/
autocapitalizationType: CoreTypes.AutocapitalizationInputType;

/**
* Gets or sets the autofill type.
*/
autofillType: CoreTypes.AutofillType;

/**
* Gets or sets whether the instance is editable.
*/
Expand Down
36 changes: 35 additions & 1 deletion packages/core/ui/editable-text-base/index.ios.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EditableTextBase as EditableTextBaseCommon, keyboardTypeProperty, returnKeyTypeProperty, autocapitalizationTypeProperty, autocorrectProperty } from './editable-text-base-common';
import { EditableTextBase as EditableTextBaseCommon, autofillTypeProperty, keyboardTypeProperty, returnKeyTypeProperty, autocapitalizationTypeProperty, autocorrectProperty } from './editable-text-base-common';
import { FormattedString } from '../text-base/formatted-string';
import { CoreTypes } from '../../core-types';

export * from './editable-text-base-common';

Expand Down Expand Up @@ -71,6 +72,39 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {

this.nativeTextViewProtected.keyboardType = newKeyboardType;
}
[autofillTypeProperty.setNative](value: CoreTypes.AutofillType) {
let newTextContentType: string;
switch (value) {
case 'phone':
newTextContentType = UITextContentTypeTelephoneNumber;
break;
case 'postalCode':
newTextContentType = UITextContentTypePostalCode;
break;
case 'creditCardNumber':
newTextContentType = UITextContentTypeCreditCardNumber;
break;
case 'email':
newTextContentType = UITextContentTypeEmailAddress;
break;
case 'name':
newTextContentType = UITextContentTypeName;
break;
case 'username':
newTextContentType = UITextContentTypeUsername;
break;
case 'password':
newTextContentType = UITextContentTypePassword;
break;
case 'none':
newTextContentType = null;
default:
newTextContentType = value;
break;
}

this.nativeTextViewProtected.textContentType = newTextContentType;
}

[returnKeyTypeProperty.getDefault](): 'done' | 'next' | 'go' | 'search' | 'send' | string {
const returnKeyType = this.nativeTextViewProtected.returnKeyType;
Expand Down