mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
Feature request #3614: TextField maxLength
property support.
This commit is contained in:

committed by
vakrilov

parent
4862443bea
commit
0997d537d0
@ -13,6 +13,7 @@ export abstract class EditableTextBase extends TextBase implements EditableTextB
|
||||
public editable: boolean;
|
||||
public autocorrect: boolean;
|
||||
public hint: string;
|
||||
public maxLength: number;
|
||||
|
||||
public abstract dismissSoftInput();
|
||||
public abstract _setInputType(inputType: number): void;
|
||||
@ -49,4 +50,6 @@ export const autocorrectProperty = new Property<EditableTextBase, boolean>({ nam
|
||||
autocorrectProperty.register(EditableTextBase);
|
||||
|
||||
export const hintProperty = new Property<EditableTextBase, string>({ name: "hint", defaultValue: "" });
|
||||
hintProperty.register(EditableTextBase);
|
||||
|
||||
export const maxLengthProperty = new Property<EditableTextBase, number>({ name: "maxLength", defaultValue: -1 });
|
||||
maxLengthProperty.register(EditableTextBase);
|
||||
|
@ -2,7 +2,7 @@
|
||||
EditableTextBase as EditableTextBaseCommon, keyboardTypeProperty,
|
||||
returnKeyTypeProperty, editableProperty,
|
||||
autocapitalizationTypeProperty, autocorrectProperty, hintProperty,
|
||||
textProperty, placeholderColorProperty, Color, textTransformProperty
|
||||
textProperty, placeholderColorProperty, Color, textTransformProperty, maxLengthProperty
|
||||
} from "./editable-text-base-common";
|
||||
|
||||
import { ad } from "../../utils/utils";
|
||||
@ -429,4 +429,8 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {
|
||||
[textTransformProperty.setNative](value: "default") {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
[maxLengthProperty.setNative](value: number) {
|
||||
this.nativeView.setFilters([new android.text.InputFilter.LengthFilter(+value)]);
|
||||
}
|
||||
}
|
||||
|
@ -45,6 +45,11 @@ export class EditableTextBase extends TextBase {
|
||||
*/
|
||||
hint: string;
|
||||
|
||||
/**
|
||||
* Limits input to a certain number of characters.
|
||||
*/
|
||||
maxLength: number;
|
||||
|
||||
/**
|
||||
* Hides the soft input method, ususally a soft keyboard.
|
||||
*/
|
||||
@ -71,6 +76,7 @@ export const autocapitalizationTypeProperty: Property<EditableTextBase, Autocapi
|
||||
export const autocorrectProperty: Property<EditableTextBase, boolean>;
|
||||
export const hintProperty: Property<EditableTextBase, string>;
|
||||
export const placeholderColorProperty: CssProperty<Style, Color>;
|
||||
export const maxLengthProperty: Property<EditableTextBase, number>;
|
||||
|
||||
//@private
|
||||
/**
|
||||
|
@ -1,5 +1,5 @@
|
||||
import {
|
||||
TextFieldBase, secureProperty, textProperty, hintProperty, colorProperty, placeholderColorProperty,
|
||||
TextFieldBase, secureProperty, textProperty, hintProperty, colorProperty, placeholderColorProperty, maxLengthProperty,
|
||||
Length, paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty, _updateCharactersInRangeReplacementString, Color, layout
|
||||
} from "./text-field-common";
|
||||
|
||||
@ -67,6 +67,10 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
|
||||
public textFieldShouldChangeCharactersInRangeReplacementString(textField: UITextField, range: NSRange, replacementString: string): boolean {
|
||||
const owner = this._owner.get();
|
||||
if (owner) {
|
||||
if (owner.maxLength > -1 && owner.maxLength <= textField.text.length && replacementString.length > range.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (owner.updateTextTrigger === "textChanged") {
|
||||
if (textField.secureTextEntry && this.firstEdit) {
|
||||
textProperty.nativeValueChange(owner, replacementString);
|
||||
@ -128,6 +132,7 @@ class UITextFieldImpl extends UITextField {
|
||||
export class TextField extends TextFieldBase {
|
||||
private _ios: UITextField;
|
||||
private _delegate: UITextFieldDelegateImpl;
|
||||
private _maxLength: number = -1;
|
||||
nativeView: UITextField;
|
||||
|
||||
constructor() {
|
||||
@ -234,4 +239,11 @@ export class TextField extends TextFieldBase {
|
||||
[paddingLeftProperty.setNative](value: Length) {
|
||||
// Padding is realized via UITextFieldImpl.textRectForBounds method
|
||||
}
|
||||
|
||||
[maxLengthProperty.getDefault](): number {
|
||||
return this._maxLength;
|
||||
}
|
||||
[maxLengthProperty.setNative](value: number) {
|
||||
this._maxLength = +value;
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
import {
|
||||
EditableTextBase, editableProperty, hintProperty, textProperty, colorProperty, placeholderColorProperty,
|
||||
borderTopWidthProperty, borderRightWidthProperty, borderBottomWidthProperty, borderLeftWidthProperty,
|
||||
paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty,
|
||||
paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty, maxLengthProperty,
|
||||
Length, _updateCharactersInRangeReplacementString, Color, layout
|
||||
} from "../editable-text-base";
|
||||
|
||||
@ -51,8 +51,14 @@ class UITextViewDelegateImpl extends NSObject implements UITextViewDelegate {
|
||||
|
||||
public textViewShouldChangeTextInRangeReplacementText(textView: UITextView, range: NSRange, replacementString: string): boolean {
|
||||
const owner = this._owner.get();
|
||||
if (owner && owner.formattedText) {
|
||||
_updateCharactersInRangeReplacementString(owner.formattedText, range.location, range.length, replacementString);
|
||||
if (owner) {
|
||||
if (owner.maxLength > -1 && owner.maxLength <= textView.text.length && replacementString.length > range.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (owner.formattedText) {
|
||||
_updateCharactersInRangeReplacementString(owner.formattedText, range.location, range.length, replacementString);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -63,6 +69,7 @@ export class TextView extends EditableTextBase implements TextViewDefinition {
|
||||
private _ios: UITextView;
|
||||
private _delegate: UITextViewDelegateImpl;
|
||||
private _isShowingHint: boolean;
|
||||
private _maxLength: number = -1;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
@ -268,6 +275,13 @@ export class TextView extends EditableTextBase implements TextViewDefinition {
|
||||
let left = layout.toDeviceIndependentPixels(this.effectivePaddingLeft + this.effectiveBorderLeftWidth);
|
||||
this.nativeView.textContainerInset = { top: inset.top, left: left, bottom: inset.bottom, right: inset.right };
|
||||
}
|
||||
|
||||
[maxLengthProperty.getDefault](): number {
|
||||
return this._maxLength;
|
||||
}
|
||||
[maxLengthProperty.setNative](value: number) {
|
||||
this._maxLength = +value;
|
||||
}
|
||||
}
|
||||
|
||||
// TextView.prototype.recycleNativeView = true;
|
Reference in New Issue
Block a user