Skip to content

Commit

Permalink
feat: add integer only keyboard type for text-field and for all edita…
Browse files Browse the repository at this point in the history
…ble text components
  • Loading branch information
NickIliev committed Dec 12, 2019
1 parent c5b7f43 commit 954e1c6
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export abstract class EditableTextBase extends TextBase implements EditableTextB
export const placeholderColorProperty = new CssProperty<Style, Color>({ name: "placeholderColor", cssName: "placeholder-color", equalityComparer: Color.equals, valueConverter: (v) => new Color(v) });
placeholderColorProperty.register(Style);

const keyboardTypeConverter = makeParser<KeyboardType>(makeValidator<KeyboardType>("datetime", "phone", "number", "url", "email"));
const keyboardTypeConverter = makeParser<KeyboardType>(makeValidator<KeyboardType>("datetime", "phone", "number", "url", "email", "integer"));

export const keyboardTypeProperty = new Property<EditableTextBase, KeyboardType>({ name: "keyboardType", valueConverter: keyboardTypeConverter });
keyboardTypeProperty.register(EditableTextBase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {
[keyboardTypeProperty.getDefault](): number {
return this.nativeTextViewProtected.getInputType();
}
[keyboardTypeProperty.setNative](value: "datetime" | "phone" | "number" | "url" | "email" | number) {
[keyboardTypeProperty.setNative](value: "datetime" | "phone" | "number" | "url" | "email" | "integer" | number) {
let newInputType;

switch (value) {
case "datetime":
newInputType = android.text.InputType.TYPE_CLASS_DATETIME | android.text.InputType.TYPE_DATETIME_VARIATION_NORMAL;
Expand All @@ -271,6 +272,10 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {
newInputType = android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
break;

case "integer":
newInputType = android.text.InputType.TYPE_CLASS_NUMBER | android.text.InputType.TYPE_NUMBER_VARIATION_PASSWORD;
break;

default:
newInputType = value;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class EditableTextBase extends TextBase {
//@endprivate
}

export type KeyboardType = "datetime" | "phone" | "number" | "url" | "email";
export type KeyboardType = "datetime" | "phone" | "number" | "url" | "email" | "integer";
export type ReturnKeyType = "done" | "next" | "go" | "search" | "send";
export type UpdateTextTrigger = "focusLost" | "textChanged";
export type AutocapitalizationType = "none" | "words" | "sentences" | "allcharacters";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {
this.notify({ eventName: EditableTextBase.blurEvent, object: this });
}

[keyboardTypeProperty.getDefault](): "datetime" | "phone" | "number" | "url" | "email" | string {
[keyboardTypeProperty.getDefault](): "datetime" | "phone" | "number" | "url" | "email" | "integer" | string {
let keyboardType = this.nativeTextViewProtected.keyboardType;
switch (keyboardType) {
case UIKeyboardType.NumbersAndPunctuation:
Expand All @@ -28,11 +28,14 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {
case UIKeyboardType.EmailAddress:
return "email";

case UIKeyboardType.NumberPad:
return "integer";

default:
return keyboardType.toString();
}
}
[keyboardTypeProperty.setNative](value: "datetime" | "phone" | "number" | "url" | "email" | string) {
[keyboardTypeProperty.setNative](value: "datetime" | "phone" | "number" | "url" | "email" | "integer" | string) {
let newKeyboardType: UIKeyboardType;
switch (value) {
case "datetime":
Expand All @@ -55,6 +58,10 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {
newKeyboardType = UIKeyboardType.EmailAddress;
break;

case "integer":
newKeyboardType = UIKeyboardType.NumberPad;
break;

default:
let kt = +value;
if (!isNaN(kt)) {
Expand Down
6 changes: 6 additions & 0 deletions nativescript-core/ui/enums/enums.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export module KeyboardType {
* iOS: [UIKeyboardTypeEmailAddress](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType)
*/
export const email: BaseKeyboardType

/**
* Android: [TYPE_CLASS_NUMBER](http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_NUMBER | [TYPE_NUMBER_VARIATION_PASSWORD](android type_text_variation_password))
* iOS: [UIKeyboardTypeNumberPad](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType)
*/
export const integer: BaseKeyboardType
}

/**
Expand Down
1 change: 1 addition & 0 deletions nativescript-core/ui/enums/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export module KeyboardType {
export const number = "number";
export const url = "url";
export const email = "email";
export const integer = "integer";
}

export module ReturnKeyType {
Expand Down
3 changes: 3 additions & 0 deletions nativescript-core/ui/text-field/text-field.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export class TextField extends TextFieldBase {
case "email":
inputType = android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
break;
case "integer":
inputType = android.text.InputType.TYPE_CLASS_NUMBER | android.text.InputType.TYPE_NUMBER_VARIATION_PASSWORD;
break;
default:
break;
}
Expand Down

0 comments on commit 954e1c6

Please sign in to comment.