Skip to content

Commit

Permalink
refactor for easy subclassing and new features (#8514)
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug committed Apr 10, 2020
1 parent 954a1b6 commit f0149da
Showing 1 changed file with 81 additions and 47 deletions.
128 changes: 81 additions & 47 deletions nativescript-core/ui/text-field/text-field.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
}

public textFieldShouldBeginEditing(textField: UITextField): boolean {
this.firstEdit = true;
const owner = this._owner.get();
if (owner) {
return owner.editable;
return owner.textFieldShouldBeginEditing(textField);
}

return true;
Expand All @@ -37,26 +36,21 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
public textFieldDidBeginEditing(textField: UITextField): void {
const owner = this._owner.get();
if (owner) {
owner.notify({ eventName: TextField.focusEvent, object: owner });
owner.textFieldDidBeginEditing(textField);
}
}

public textFieldDidEndEditing(textField: UITextField) {
const owner = this._owner.get();
if (owner) {
if (owner.updateTextTrigger === "focusLost") {
textProperty.nativeValueChange(owner, textField.text);
}

owner.dismissSoftInput();
owner.textFieldDidEndEditing(textField);
}
}

public textFieldShouldClear(textField: UITextField) {
this.firstEdit = false;
const owner = this._owner.get();
if (owner) {
textProperty.nativeValueChange(owner, "");
return owner.textFieldShouldClear(textField);
}

return true;
Expand All @@ -66,10 +60,7 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
// Called when the user presses the return button.
const owner = this._owner.get();
if (owner) {
if (owner.closeOnReturn) {
owner.dismissSoftInput();
}
owner.notify({ eventName: TextField.returnPressEvent, object: owner });
return owner.textFieldShouldReturn(textField);
}

return true;
Expand All @@ -78,40 +69,9 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
public textFieldShouldChangeCharactersInRangeReplacementString(textField: UITextField, range: NSRange, replacementString: string): boolean {
const owner = this._owner.get();
if (owner) {
if (owner.secureWithoutAutofill && !textField.secureTextEntry) {
/**
* Helps avoid iOS 12+ autofill strong password suggestion prompt
* Discussed in several circles but for example:
* https://github.com/expo/expo/issues/2571#issuecomment-473347380
*/
textField.secureTextEntry = true;
}
const delta = replacementString.length - range.length;
if (delta > 0) {
if (textField.text.length + delta > owner.maxLength) {
return false;
}
}

if (owner.updateTextTrigger === "textChanged") {
if (textField.secureTextEntry && this.firstEdit) {
textProperty.nativeValueChange(owner, replacementString);
}
else {
if (range.location <= textField.text.length) {
const newText = NSString.stringWithString(textField.text).stringByReplacingCharactersInRangeWithString(range, replacementString);
textProperty.nativeValueChange(owner, newText);
}
}
}

if (owner.formattedText) {
_updateCharactersInRangeReplacementString(owner.formattedText, range.location, range.length, replacementString);
}
return owner.textFieldShouldChangeCharactersInRangeReplacementString(textField, range, replacementString);
}

this.firstEdit = false;


return true;
}
}
Expand Down Expand Up @@ -186,6 +146,80 @@ export class TextField extends TextFieldBase {
return this.nativeViewProtected;
}

private firstEdit: boolean;

public textFieldShouldBeginEditing(textField: UITextField): boolean {
this.firstEdit = true;

return this.editable;
}

public textFieldDidBeginEditing(textField: UITextField): void {
this.notify({ eventName: TextField.focusEvent, object: this });
}

public textFieldDidEndEditing(textField: UITextField) {
if (this.updateTextTrigger === "focusLost") {
textProperty.nativeValueChange(this, textField.text);
}

this.dismissSoftInput();
}

public textFieldShouldClear(textField: UITextField) {
this.firstEdit = false;
textProperty.nativeValueChange(this, "");

return true;
}

public textFieldShouldReturn(textField: UITextField): boolean {
// Called when the user presses the return button.
if (this.closeOnReturn) {
this.dismissSoftInput();
}
this.notify({ eventName: TextField.returnPressEvent, object: this });

return true;
}

public textFieldShouldChangeCharactersInRangeReplacementString(textField: UITextField, range: NSRange, replacementString: string): boolean {
if (this.secureWithoutAutofill && !textField.secureTextEntry) {
/**
* Helps avoid iOS 12+ autofill strong password suggestion prompt
* Discussed in several circles but for example:
* https://github.com/expo/expo/issues/2571#issuecomment-473347380
*/
textField.secureTextEntry = true;
}
const delta = replacementString.length - range.length;
if (delta > 0) {
if (textField.text.length + delta > this.maxLength) {
return false;
}
}

if (this.updateTextTrigger === "textChanged") {
if (textField.secureTextEntry && this.firstEdit) {
textProperty.nativeValueChange(this, replacementString);
}
else {
if (range.location <= textField.text.length) {
const newText = NSString.stringWithString(textField.text).stringByReplacingCharactersInRangeWithString(range, replacementString);
textProperty.nativeValueChange(this, newText);
}
}
}

if (this.formattedText) {
_updateCharactersInRangeReplacementString(this.formattedText, range.location, range.length, replacementString);
}

this.firstEdit = false;

return true;
}

[hintProperty.getDefault](): string {
return this.nativeTextViewProtected.placeholder;
}
Expand Down

0 comments on commit f0149da

Please sign in to comment.