UNPKG

11.8 kBJavaScriptView Raw
1import { TextFieldBase, secureProperty } from './text-field-common';
2import { textProperty } from '../text-base';
3import { hintProperty, placeholderColorProperty, _updateCharactersInRangeReplacementString } from '../editable-text-base';
4import { CoreTypes } from '../../core-types';
5import { Color } from '../../color';
6import { colorProperty, paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty } from '../styling/style-properties';
7import { layout, isEmoji } from '../../utils';
8export * from './text-field-common';
9var UITextFieldDelegateImpl = /** @class */ (function (_super) {
10 __extends(UITextFieldDelegateImpl, _super);
11 function UITextFieldDelegateImpl() {
12 return _super !== null && _super.apply(this, arguments) || this;
13 }
14 UITextFieldDelegateImpl.initWithOwner = function (owner) {
15 var delegate = UITextFieldDelegateImpl.new();
16 delegate._owner = owner;
17 return delegate;
18 };
19 UITextFieldDelegateImpl.prototype.textFieldShouldBeginEditing = function (textField) {
20 var _a;
21 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
22 if (owner) {
23 return owner.textFieldShouldBeginEditing(textField);
24 }
25 return true;
26 };
27 UITextFieldDelegateImpl.prototype.textFieldDidBeginEditing = function (textField) {
28 var _a;
29 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
30 if (owner) {
31 owner.textFieldDidBeginEditing(textField);
32 }
33 };
34 UITextFieldDelegateImpl.prototype.textFieldDidEndEditing = function (textField) {
35 var _a;
36 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
37 if (owner) {
38 owner.textFieldDidEndEditing(textField);
39 }
40 };
41 UITextFieldDelegateImpl.prototype.textFieldShouldClear = function (textField) {
42 var _a;
43 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
44 if (owner) {
45 return owner.textFieldShouldClear(textField);
46 }
47 return true;
48 };
49 UITextFieldDelegateImpl.prototype.textFieldShouldReturn = function (textField) {
50 var _a;
51 // Called when the user presses the return button.
52 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
53 if (owner) {
54 return owner.textFieldShouldReturn(textField);
55 }
56 return true;
57 };
58 UITextFieldDelegateImpl.prototype.textFieldShouldChangeCharactersInRangeReplacementString = function (textField, range, replacementString) {
59 var _a;
60 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
61 if (owner) {
62 return owner.textFieldShouldChangeCharactersInRangeReplacementString(textField, range, replacementString);
63 }
64 return true;
65 };
66 UITextFieldDelegateImpl.ObjCProtocols = [UITextFieldDelegate];
67 return UITextFieldDelegateImpl;
68}(NSObject));
69var UITextFieldImpl = /** @class */ (function (_super) {
70 __extends(UITextFieldImpl, _super);
71 function UITextFieldImpl() {
72 return _super !== null && _super.apply(this, arguments) || this;
73 }
74 UITextFieldImpl.initWithOwner = function (owner) {
75 var handler = UITextFieldImpl.new();
76 handler._owner = owner;
77 return handler;
78 };
79 UITextFieldImpl.prototype._getTextRectForBounds = function (bounds) {
80 var owner = this._owner ? this._owner.deref() : null;
81 if (!owner) {
82 return bounds;
83 }
84 var size = bounds.size;
85 var x = layout.toDeviceIndependentPixels(owner.effectiveBorderLeftWidth + owner.effectivePaddingLeft);
86 var y = layout.toDeviceIndependentPixels(owner.effectiveBorderTopWidth + owner.effectivePaddingTop);
87 var width = layout.toDeviceIndependentPixels(layout.toDevicePixels(size.width) - (owner.effectiveBorderLeftWidth + owner.effectivePaddingLeft + owner.effectivePaddingRight + owner.effectiveBorderRightWidth));
88 var height = layout.toDeviceIndependentPixels(layout.toDevicePixels(size.height) - (owner.effectiveBorderTopWidth + owner.effectivePaddingTop + owner.effectivePaddingBottom + owner.effectiveBorderBottomWidth));
89 return CGRectMake(x, y, width, height);
90 };
91 UITextFieldImpl.prototype.textRectForBounds = function (bounds) {
92 return this._getTextRectForBounds(bounds);
93 };
94 UITextFieldImpl.prototype.editingRectForBounds = function (bounds) {
95 return this._getTextRectForBounds(bounds);
96 };
97 return UITextFieldImpl;
98}(UITextField));
99export class TextField extends TextFieldBase {
100 createNativeView() {
101 return UITextFieldImpl.initWithOwner(new WeakRef(this));
102 }
103 initNativeView() {
104 super.initNativeView();
105 this._delegate = UITextFieldDelegateImpl.initWithOwner(new WeakRef(this));
106 this.nativeViewProtected.delegate = this._delegate;
107 }
108 disposeNativeView() {
109 this._delegate = null;
110 super.disposeNativeView();
111 }
112 // @ts-ignore
113 get ios() {
114 return this.nativeViewProtected;
115 }
116 textFieldShouldBeginEditing(textField) {
117 this.firstEdit = true;
118 return this.editable;
119 }
120 textFieldDidBeginEditing(textField) {
121 this.notify({ eventName: TextField.focusEvent, object: this });
122 }
123 textFieldDidEndEditing(textField) {
124 if (this.updateTextTrigger === 'focusLost') {
125 textProperty.nativeValueChange(this, textField.text);
126 }
127 this.dismissSoftInput();
128 }
129 textFieldShouldClear(textField) {
130 this.firstEdit = false;
131 textProperty.nativeValueChange(this, '');
132 return true;
133 }
134 textFieldShouldReturn(textField) {
135 // Called when the user presses the return button.
136 if (this.closeOnReturn) {
137 this.dismissSoftInput();
138 }
139 this.notify({ eventName: TextField.returnPressEvent, object: this });
140 return true;
141 }
142 textFieldShouldChangeCharactersInRangeReplacementString(textField, range, replacementString) {
143 if (this.secureWithoutAutofill && !textField.secureTextEntry) {
144 /**
145 * Helps avoid iOS 12+ autofill strong password suggestion prompt
146 * Discussed in several circles but for example:
147 * https://github.com/expo/expo/issues/2571#issuecomment-473347380
148 */
149 textField.secureTextEntry = true;
150 }
151 const delta = replacementString.length - range.length;
152 if (delta > 0) {
153 if (textField.text.length + delta > this.maxLength) {
154 return false;
155 }
156 }
157 if (this.updateTextTrigger === 'textChanged') {
158 if (this.valueFormatter) {
159 // format/replace
160 let currentValue = textField.text;
161 let nativeValueChange = `${textField.text}${replacementString}`;
162 if (replacementString === '') {
163 // clearing when empty
164 nativeValueChange = currentValue.slice(0, delta);
165 }
166 const formattedValue = this.valueFormatter(nativeValueChange);
167 textField.text = formattedValue;
168 textProperty.nativeValueChange(this, formattedValue);
169 return false;
170 }
171 else {
172 // 1. secureTextEntry with firstEdit should not replace
173 // 2. emoji's should not replace value
174 // 3. convenient keyboard shortcuts should not replace value (eg, '.com')
175 const shouldReplaceString = (textField.secureTextEntry && this.firstEdit) || (delta > 1 && !isEmoji(replacementString) && delta !== replacementString.length);
176 if (shouldReplaceString) {
177 textProperty.nativeValueChange(this, replacementString);
178 }
179 else {
180 if (range.location <= textField.text.length) {
181 const newText = NSString.stringWithString(textField.text).stringByReplacingCharactersInRangeWithString(range, replacementString);
182 textProperty.nativeValueChange(this, newText);
183 }
184 }
185 }
186 }
187 if (this.formattedText) {
188 _updateCharactersInRangeReplacementString(this.formattedText, range.location, range.length, replacementString);
189 }
190 if (this.width === 'auto') {
191 // if the textfield is in auto size we need to request a layout to take the new text width into account
192 this.requestLayout();
193 }
194 this.firstEdit = false;
195 return true;
196 }
197 [hintProperty.getDefault]() {
198 return this.nativeTextViewProtected.placeholder;
199 }
200 [hintProperty.setNative](value) {
201 this._updateAttributedPlaceholder();
202 }
203 [secureProperty.getDefault]() {
204 return this.nativeTextViewProtected.secureTextEntry;
205 }
206 [secureProperty.setNative](value) {
207 this.nativeTextViewProtected.secureTextEntry = value;
208 }
209 [colorProperty.getDefault]() {
210 return {
211 textColor: this.nativeTextViewProtected.textColor,
212 tintColor: this.nativeTextViewProtected.tintColor,
213 };
214 }
215 [colorProperty.setNative](value) {
216 if (value instanceof Color) {
217 const color = value instanceof Color ? value.ios : value;
218 this.nativeTextViewProtected.textColor = color;
219 this.nativeTextViewProtected.tintColor = color;
220 }
221 else {
222 this.nativeTextViewProtected.textColor = value.textColor;
223 this.nativeTextViewProtected.tintColor = value.tintColor;
224 }
225 }
226 [placeholderColorProperty.getDefault]() {
227 return null;
228 }
229 [placeholderColorProperty.setNative](value) {
230 this._updateAttributedPlaceholder();
231 }
232 _updateAttributedPlaceholder() {
233 let stringValue = this.hint;
234 if (stringValue === null || stringValue === void 0) {
235 stringValue = '';
236 }
237 else {
238 stringValue = stringValue + '';
239 }
240 if (stringValue === '') {
241 // we do not use empty string since initWithStringAttributes does not return proper value and
242 // nativeView.attributedPlaceholder will be null
243 stringValue = ' ';
244 }
245 const attributes = {};
246 if (this.style.placeholderColor) {
247 attributes[NSForegroundColorAttributeName] = this.style.placeholderColor.ios;
248 }
249 const attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(stringValue, attributes);
250 this.nativeTextViewProtected.attributedPlaceholder = attributedPlaceholder;
251 }
252 [paddingTopProperty.getDefault]() {
253 return CoreTypes.zeroLength;
254 }
255 [paddingTopProperty.setNative](value) {
256 // Padding is realized via UITextFieldImpl.textRectForBounds method
257 }
258 [paddingRightProperty.getDefault]() {
259 return CoreTypes.zeroLength;
260 }
261 [paddingRightProperty.setNative](value) {
262 // Padding is realized via UITextFieldImpl.textRectForBounds method
263 }
264 [paddingBottomProperty.getDefault]() {
265 return CoreTypes.zeroLength;
266 }
267 [paddingBottomProperty.setNative](value) {
268 // Padding is realized via UITextFieldImpl.textRectForBounds method
269 }
270 [paddingLeftProperty.getDefault]() {
271 return CoreTypes.zeroLength;
272 }
273 [paddingLeftProperty.setNative](value) {
274 // Padding is realized via UITextFieldImpl.textRectForBounds method
275 }
276}
277//# sourceMappingURL=index.ios.js.map
\No newline at end of file