Skip to content
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
2 changes: 2 additions & 0 deletions api-reports/NativeScript.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2496,6 +2496,8 @@ export class TextBase extends View implements AddChildFromBuilder {
export class TextField extends EditableTextBase {
android: any /* android.widget.EditText */;

closeOnReturn: boolean;

ios: any /* UITextField */;

// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions nativescript-core/ui/text-field/text-field-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ export * from "../editable-text-base";
export class TextFieldBase extends EditableTextBase implements TextFieldDefinition {
public static returnPressEvent = "returnPress";
public secure: boolean;
public closeOnReturn: boolean;
}

TextFieldBase.prototype.recycleNativeView = "auto";

export const secureProperty = new Property<TextFieldBase, boolean>({ name: "secure", defaultValue: false, valueConverter: booleanConverter });
secureProperty.register(TextFieldBase);

export const closeOnReturnProperty = new Property<TextFieldBase, boolean>({ name: "closeOnReturn", defaultValue: true, valueConverter: booleanConverter });
closeOnReturnProperty.register(TextFieldBase);
5 changes: 5 additions & 0 deletions nativescript-core/ui/text-field/text-field.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ export class TextField extends EditableTextBase {
* Gets or sets if a text field is for password entry.
*/
secure: boolean;

/**
* Gets or sets if a text field should dismiss on return.
*/
closeOnReturn: boolean;
}
4 changes: 3 additions & 1 deletion nativescript-core/ui/text-field/text-field.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
// Called when the user presses the return button.
const owner = this._owner.get();
if (owner) {
owner.dismissSoftInput();
if (owner.closeOnReturn) {
owner.dismissSoftInput();
}
owner.notify({ eventName: TextField.returnPressEvent, object: owner });
}

Expand Down
9 changes: 9 additions & 0 deletions tests/app/ui/text-field/text-field-tests-native.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ export function getNativeTextAlignment(textField: textFieldModule.TextField): st
return "unexpected value";
}

export function getNativeFocus(textField: textFieldModule.TextField): boolean {
//
return true;
}

export function typeTextNatively(textField: textFieldModule.TextField, text: string): void {
textField.android.requestFocus();
textField.android.setText(text);
textField.android.clearFocus();
}

export function typeTextNativelyWithReturn(textField: textFieldModule.TextField, text: string): void {
//
}
3 changes: 3 additions & 0 deletions tests/app/ui/text-field/text-field-tests-native.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ export declare function getNativeColor(textField: textFieldModule.TextField): co
export declare function getNativePlaceholderColor(textField: textFieldModule.TextField): colorModule.Color;
export declare function getNativeBackgroundColor(textField: textFieldModule.TextField): colorModule.Color;
export declare function getNativeTextAlignment(textField: textFieldModule.TextField): string;
export declare function getNativeFocus(textField: textFieldModule.TextField): boolean;
export declare function typeTextNatively(textField: textFieldModule.TextField, text: string): void;
export declare function typeTextNativelyWithReturn(textField: textFieldModule.TextField, text: string): void;

12 changes: 12 additions & 0 deletions tests/app/ui/text-field/text-field-tests-native.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,21 @@ export function getNativeTextAlignment(textField: textFieldModule.TextField): st
}
}

export function getNativeFocus(textField: textFieldModule.TextField): boolean {
return textField.nativeView.isFirstResponder;
}

export function typeTextNatively(textField: textFieldModule.TextField, text: string): void {
textField.ios.text = text;

// Setting the text will not trigger the delegate method, so we have to do it by hand.
textField.ios.delegate.textFieldDidEndEditing(textField.ios);
}

export function typeTextNativelyWithReturn(textField: textFieldModule.TextField, text: string): void {
textField.nativeView.becomeFirstResponder();

textField.ios.text = text;

textField.ios.delegate.textFieldShouldReturn(textField.ios);
}
98 changes: 96 additions & 2 deletions tests/app/ui/text-field/text-field-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Page } from "@nativescript/core/ui/page";
import { StackLayout } from "@nativescript/core/ui/layouts/stack-layout";
import { Color } from "@nativescript/core/color";
import {
getNativeText, getNativeHint, typeTextNatively, getNativeSecure,
getNativeText, getNativeHint, typeTextNatively, typeTextNativelyWithReturn, getNativeSecure,
getNativeFontSize, getNativeColor, getNativeBackgroundColor,
getNativeTextAlignment, getNativePlaceholderColor
getNativeTextAlignment, getNativePlaceholderColor, getNativeFocus
} from "./text-field-tests-native";
import { FormattedString } from "@nativescript/core/text/formatted-string";
import { Span } from "@nativescript/core/text/span";
Expand Down Expand Up @@ -400,6 +400,100 @@ export var testBindSecureToBindingConext = function () {
});
};

// iOS only
export var testBindCloseOnReturnToBindingConext = function () {
helper.buildUIAndRunTest(_createTextFieldFunc(), function (views: Array<View>) {
if (!isIOS) {
TKUnit.assert(true === true);

return;
}
var textField = <TextField>views[0];
var page = <Page>views[1];

var model = new Observable();
model.set("closeOnReturn", false);
page.bindingContext = model;

var options: BindingOptions = {
sourceProperty: "closeOnReturn",
targetProperty: "closeOnReturn"
};

textField.bind(options);
TKUnit.assert(textField.closeOnReturn === false, "Actual: " + textField.closeOnReturn + "; Expected: " + false);
typeTextNativelyWithReturn(textField, "Should not close textfield");
TKUnit.assert(getNativeFocus(textField) === true, "Actual: " + getNativeFocus(textField) + "; Expected: " + true);

model.set("closeOnReturn", true);
TKUnit.assert(textField.closeOnReturn === true, "Actual: " + textField.closeOnReturn + "; Expected: " + true);
typeTextNativelyWithReturn(textField, "Should close textfield");
TKUnit.assert(getNativeFocus(textField) === false, "Actual: " + getNativeFocus(textField) + "; Expected: " + false);
});
};

// iOS only
export var testDontCloseOnReturn = function () {
helper.buildUIAndRunTest(_createTextFieldFunc(), function (views: Array<View>) {
if (!isIOS) {
TKUnit.assert(true === true);

return;
}
var textField = <TextField>views[0];

// >> setting-closeOnReturn-property
textField.closeOnReturn = false;
// << setting-closeOnReturn-property

typeTextNativelyWithReturn(textField, "Should not close textfield");

var expectedValue = true;
var actualValue = getNativeFocus(textField);
TKUnit.assert(actualValue === expectedValue, "Actual: " + actualValue + "; Expected: " + expectedValue);
});
};

// iOS only
export var testCloseOnReturn = function () {
helper.buildUIAndRunTest(_createTextFieldFunc(), function (views: Array<View>) {
if (!isIOS) {
TKUnit.assert(true === true);

return;
}
var textField = <TextField>views[0];

// >> setting-closeOnReturn-property
textField.closeOnReturn = true;
// << setting-closeOnReturn-property

typeTextNativelyWithReturn(textField, "Should close textfield");

var expectedValue = false;
var actualValue = getNativeFocus(textField);
TKUnit.assert(actualValue === expectedValue, "Actual: " + actualValue + "; Expected: " + expectedValue);
});
};

// iOS only
export var testCloseOnReturnByDefault = function () {
helper.buildUIAndRunTest(_createTextFieldFunc(), function (views: Array<View>) {
if (!isIOS) {
TKUnit.assert(true === true);

return;
}
var textField = <TextField>views[0];

typeTextNativelyWithReturn(textField, "Should close textfield by default");

var expectedValue = false;
var actualValue = getNativeFocus(textField);
TKUnit.assert(actualValue === expectedValue, "Actual: " + actualValue + "; Expected: " + expectedValue);
});
};

var expectedFontSize = 42;
export var testLocalFontSizeFromCss = function () {
helper.buildUIAndRunTest(_createTextFieldFunc(), function (views: Array<View>) {
Expand Down