Skip to content

Commit 0997d53

Browse files
eddyverbruggen@gmail.comvakrilov
authored andcommitted
Feature request NativeScript#3614: TextField maxLength property support.
1 parent 4862443 commit 0997d53

5 files changed

Lines changed: 46 additions & 7 deletions

File tree

tns-core-modules/ui/editable-text-base/editable-text-base-common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export abstract class EditableTextBase extends TextBase implements EditableTextB
1313
public editable: boolean;
1414
public autocorrect: boolean;
1515
public hint: string;
16+
public maxLength: number;
1617

1718
public abstract dismissSoftInput();
1819
public abstract _setInputType(inputType: number): void;
@@ -49,4 +50,6 @@ export const autocorrectProperty = new Property<EditableTextBase, boolean>({ nam
4950
autocorrectProperty.register(EditableTextBase);
5051

5152
export const hintProperty = new Property<EditableTextBase, string>({ name: "hint", defaultValue: "" });
52-
hintProperty.register(EditableTextBase);
53+
54+
export const maxLengthProperty = new Property<EditableTextBase, number>({ name: "maxLength", defaultValue: -1 });
55+
maxLengthProperty.register(EditableTextBase);

tns-core-modules/ui/editable-text-base/editable-text-base.android.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
EditableTextBase as EditableTextBaseCommon, keyboardTypeProperty,
33
returnKeyTypeProperty, editableProperty,
44
autocapitalizationTypeProperty, autocorrectProperty, hintProperty,
5-
textProperty, placeholderColorProperty, Color, textTransformProperty
5+
textProperty, placeholderColorProperty, Color, textTransformProperty, maxLengthProperty
66
} from "./editable-text-base-common";
77

88
import { ad } from "../../utils/utils";
@@ -429,4 +429,8 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {
429429
[textTransformProperty.setNative](value: "default") {
430430
//
431431
}
432-
}
432+
433+
[maxLengthProperty.setNative](value: number) {
434+
this.nativeView.setFilters([new android.text.InputFilter.LengthFilter(+value)]);
435+
}
436+
}

tns-core-modules/ui/editable-text-base/editable-text-base.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ export class EditableTextBase extends TextBase {
4545
*/
4646
hint: string;
4747

48+
/**
49+
* Limits input to a certain number of characters.
50+
*/
51+
maxLength: number;
52+
4853
/**
4954
* Hides the soft input method, ususally a soft keyboard.
5055
*/
@@ -71,6 +76,7 @@ export const autocapitalizationTypeProperty: Property<EditableTextBase, Autocapi
7176
export const autocorrectProperty: Property<EditableTextBase, boolean>;
7277
export const hintProperty: Property<EditableTextBase, string>;
7378
export const placeholderColorProperty: CssProperty<Style, Color>;
79+
export const maxLengthProperty: Property<EditableTextBase, number>;
7480

7581
//@private
7682
/**

tns-core-modules/ui/text-field/text-field.ios.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
TextFieldBase, secureProperty, textProperty, hintProperty, colorProperty, placeholderColorProperty,
2+
TextFieldBase, secureProperty, textProperty, hintProperty, colorProperty, placeholderColorProperty, maxLengthProperty,
33
Length, paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty, _updateCharactersInRangeReplacementString, Color, layout
44
} from "./text-field-common";
55

@@ -67,6 +67,10 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
6767
public textFieldShouldChangeCharactersInRangeReplacementString(textField: UITextField, range: NSRange, replacementString: string): boolean {
6868
const owner = this._owner.get();
6969
if (owner) {
70+
if (owner.maxLength > -1 && owner.maxLength <= textField.text.length && replacementString.length > range.length) {
71+
return false;
72+
}
73+
7074
if (owner.updateTextTrigger === "textChanged") {
7175
if (textField.secureTextEntry && this.firstEdit) {
7276
textProperty.nativeValueChange(owner, replacementString);
@@ -128,6 +132,7 @@ class UITextFieldImpl extends UITextField {
128132
export class TextField extends TextFieldBase {
129133
private _ios: UITextField;
130134
private _delegate: UITextFieldDelegateImpl;
135+
private _maxLength: number = -1;
131136
nativeView: UITextField;
132137

133138
constructor() {
@@ -234,4 +239,11 @@ export class TextField extends TextFieldBase {
234239
[paddingLeftProperty.setNative](value: Length) {
235240
// Padding is realized via UITextFieldImpl.textRectForBounds method
236241
}
242+
243+
[maxLengthProperty.getDefault](): number {
244+
return this._maxLength;
245+
}
246+
[maxLengthProperty.setNative](value: number) {
247+
this._maxLength = +value;
248+
}
237249
}

tns-core-modules/ui/text-view/text-view.ios.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import {
33
EditableTextBase, editableProperty, hintProperty, textProperty, colorProperty, placeholderColorProperty,
44
borderTopWidthProperty, borderRightWidthProperty, borderBottomWidthProperty, borderLeftWidthProperty,
5-
paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty,
5+
paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty, maxLengthProperty,
66
Length, _updateCharactersInRangeReplacementString, Color, layout
77
} from "../editable-text-base";
88

@@ -51,8 +51,14 @@ class UITextViewDelegateImpl extends NSObject implements UITextViewDelegate {
5151

5252
public textViewShouldChangeTextInRangeReplacementText(textView: UITextView, range: NSRange, replacementString: string): boolean {
5353
const owner = this._owner.get();
54-
if (owner && owner.formattedText) {
55-
_updateCharactersInRangeReplacementString(owner.formattedText, range.location, range.length, replacementString);
54+
if (owner) {
55+
if (owner.maxLength > -1 && owner.maxLength <= textView.text.length && replacementString.length > range.length) {
56+
return false;
57+
}
58+
59+
if (owner.formattedText) {
60+
_updateCharactersInRangeReplacementString(owner.formattedText, range.location, range.length, replacementString);
61+
}
5662
}
5763

5864
return true;
@@ -63,6 +69,7 @@ export class TextView extends EditableTextBase implements TextViewDefinition {
6369
private _ios: UITextView;
6470
private _delegate: UITextViewDelegateImpl;
6571
private _isShowingHint: boolean;
72+
private _maxLength: number = -1;
6673

6774
constructor() {
6875
super();
@@ -268,6 +275,13 @@ export class TextView extends EditableTextBase implements TextViewDefinition {
268275
let left = layout.toDeviceIndependentPixels(this.effectivePaddingLeft + this.effectiveBorderLeftWidth);
269276
this.nativeView.textContainerInset = { top: inset.top, left: left, bottom: inset.bottom, right: inset.right };
270277
}
278+
279+
[maxLengthProperty.getDefault](): number {
280+
return this._maxLength;
281+
}
282+
[maxLengthProperty.setNative](value: number) {
283+
this._maxLength = +value;
284+
}
271285
}
272286

273287
// TextView.prototype.recycleNativeView = true;

0 commit comments

Comments
 (0)