Skip to content

Commit f0149da

Browse files
authored
refactor for easy subclassing and new features (#8514)
1 parent 954a1b6 commit f0149da

1 file changed

Lines changed: 81 additions & 47 deletions

File tree

nativescript-core/ui/text-field/text-field.ios.ts

Lines changed: 81 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
2525
}
2626

2727
public textFieldShouldBeginEditing(textField: UITextField): boolean {
28-
this.firstEdit = true;
2928
const owner = this._owner.get();
3029
if (owner) {
31-
return owner.editable;
30+
return owner.textFieldShouldBeginEditing(textField);
3231
}
3332

3433
return true;
@@ -37,26 +36,21 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
3736
public textFieldDidBeginEditing(textField: UITextField): void {
3837
const owner = this._owner.get();
3938
if (owner) {
40-
owner.notify({ eventName: TextField.focusEvent, object: owner });
39+
owner.textFieldDidBeginEditing(textField);
4140
}
4241
}
4342

4443
public textFieldDidEndEditing(textField: UITextField) {
4544
const owner = this._owner.get();
4645
if (owner) {
47-
if (owner.updateTextTrigger === "focusLost") {
48-
textProperty.nativeValueChange(owner, textField.text);
49-
}
50-
51-
owner.dismissSoftInput();
46+
owner.textFieldDidEndEditing(textField);
5247
}
5348
}
5449

5550
public textFieldShouldClear(textField: UITextField) {
56-
this.firstEdit = false;
5751
const owner = this._owner.get();
5852
if (owner) {
59-
textProperty.nativeValueChange(owner, "");
53+
return owner.textFieldShouldClear(textField);
6054
}
6155

6256
return true;
@@ -66,10 +60,7 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
6660
// Called when the user presses the return button.
6761
const owner = this._owner.get();
6862
if (owner) {
69-
if (owner.closeOnReturn) {
70-
owner.dismissSoftInput();
71-
}
72-
owner.notify({ eventName: TextField.returnPressEvent, object: owner });
63+
return owner.textFieldShouldReturn(textField);
7364
}
7465

7566
return true;
@@ -78,40 +69,9 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
7869
public textFieldShouldChangeCharactersInRangeReplacementString(textField: UITextField, range: NSRange, replacementString: string): boolean {
7970
const owner = this._owner.get();
8071
if (owner) {
81-
if (owner.secureWithoutAutofill && !textField.secureTextEntry) {
82-
/**
83-
* Helps avoid iOS 12+ autofill strong password suggestion prompt
84-
* Discussed in several circles but for example:
85-
* https://github.com/expo/expo/issues/2571#issuecomment-473347380
86-
*/
87-
textField.secureTextEntry = true;
88-
}
89-
const delta = replacementString.length - range.length;
90-
if (delta > 0) {
91-
if (textField.text.length + delta > owner.maxLength) {
92-
return false;
93-
}
94-
}
95-
96-
if (owner.updateTextTrigger === "textChanged") {
97-
if (textField.secureTextEntry && this.firstEdit) {
98-
textProperty.nativeValueChange(owner, replacementString);
99-
}
100-
else {
101-
if (range.location <= textField.text.length) {
102-
const newText = NSString.stringWithString(textField.text).stringByReplacingCharactersInRangeWithString(range, replacementString);
103-
textProperty.nativeValueChange(owner, newText);
104-
}
105-
}
106-
}
107-
108-
if (owner.formattedText) {
109-
_updateCharactersInRangeReplacementString(owner.formattedText, range.location, range.length, replacementString);
110-
}
72+
return owner.textFieldShouldChangeCharactersInRangeReplacementString(textField, range, replacementString);
11173
}
112-
113-
this.firstEdit = false;
114-
74+
11575
return true;
11676
}
11777
}
@@ -186,6 +146,80 @@ export class TextField extends TextFieldBase {
186146
return this.nativeViewProtected;
187147
}
188148

149+
private firstEdit: boolean;
150+
151+
public textFieldShouldBeginEditing(textField: UITextField): boolean {
152+
this.firstEdit = true;
153+
154+
return this.editable;
155+
}
156+
157+
public textFieldDidBeginEditing(textField: UITextField): void {
158+
this.notify({ eventName: TextField.focusEvent, object: this });
159+
}
160+
161+
public textFieldDidEndEditing(textField: UITextField) {
162+
if (this.updateTextTrigger === "focusLost") {
163+
textProperty.nativeValueChange(this, textField.text);
164+
}
165+
166+
this.dismissSoftInput();
167+
}
168+
169+
public textFieldShouldClear(textField: UITextField) {
170+
this.firstEdit = false;
171+
textProperty.nativeValueChange(this, "");
172+
173+
return true;
174+
}
175+
176+
public textFieldShouldReturn(textField: UITextField): boolean {
177+
// Called when the user presses the return button.
178+
if (this.closeOnReturn) {
179+
this.dismissSoftInput();
180+
}
181+
this.notify({ eventName: TextField.returnPressEvent, object: this });
182+
183+
return true;
184+
}
185+
186+
public textFieldShouldChangeCharactersInRangeReplacementString(textField: UITextField, range: NSRange, replacementString: string): boolean {
187+
if (this.secureWithoutAutofill && !textField.secureTextEntry) {
188+
/**
189+
* Helps avoid iOS 12+ autofill strong password suggestion prompt
190+
* Discussed in several circles but for example:
191+
* https://github.com/expo/expo/issues/2571#issuecomment-473347380
192+
*/
193+
textField.secureTextEntry = true;
194+
}
195+
const delta = replacementString.length - range.length;
196+
if (delta > 0) {
197+
if (textField.text.length + delta > this.maxLength) {
198+
return false;
199+
}
200+
}
201+
202+
if (this.updateTextTrigger === "textChanged") {
203+
if (textField.secureTextEntry && this.firstEdit) {
204+
textProperty.nativeValueChange(this, replacementString);
205+
}
206+
else {
207+
if (range.location <= textField.text.length) {
208+
const newText = NSString.stringWithString(textField.text).stringByReplacingCharactersInRangeWithString(range, replacementString);
209+
textProperty.nativeValueChange(this, newText);
210+
}
211+
}
212+
}
213+
214+
if (this.formattedText) {
215+
_updateCharactersInRangeReplacementString(this.formattedText, range.location, range.length, replacementString);
216+
}
217+
218+
this.firstEdit = false;
219+
220+
return true;
221+
}
222+
189223
[hintProperty.getDefault](): string {
190224
return this.nativeTextViewProtected.placeholder;
191225
}

0 commit comments

Comments
 (0)