UNPKG

7.09 kBJavaScriptView Raw
1import { Font } from '../styling/font';
2import { SearchBarBase, textProperty, hintProperty, textFieldHintColorProperty, textFieldBackgroundColorProperty } from './search-bar-common';
3import { isEnabledProperty } from '../core/view';
4import { Color } from '../../color';
5import { colorProperty, backgroundColorProperty, backgroundInternalProperty, fontInternalProperty } from '../styling/style-properties';
6import { iOSNativeHelper } from '../../utils';
7export * from './search-bar-common';
8const majorVersion = iOSNativeHelper.MajorVersion;
9var UISearchBarDelegateImpl = /** @class */ (function (_super) {
10 __extends(UISearchBarDelegateImpl, _super);
11 function UISearchBarDelegateImpl() {
12 return _super !== null && _super.apply(this, arguments) || this;
13 }
14 UISearchBarDelegateImpl.initWithOwner = function (owner) {
15 var delegate = UISearchBarDelegateImpl.new();
16 delegate._owner = owner;
17 return delegate;
18 };
19 UISearchBarDelegateImpl.prototype.searchBarTextDidChange = function (searchBar, searchText) {
20 var _a;
21 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
22 if (!owner) {
23 return;
24 }
25 textProperty.nativeValueChange(owner, searchText);
26 // This code is needed since sometimes searchBarCancelButtonClicked is not called!
27 if (searchText === '') {
28 owner._emit(SearchBarBase.clearEvent);
29 }
30 };
31 UISearchBarDelegateImpl.prototype.searchBarCancelButtonClicked = function (searchBar) {
32 var _a;
33 searchBar.resignFirstResponder();
34 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
35 if (!owner) {
36 return;
37 }
38 owner._emit(SearchBarBase.clearEvent);
39 };
40 UISearchBarDelegateImpl.prototype.searchBarSearchButtonClicked = function (searchBar) {
41 var _a;
42 searchBar.resignFirstResponder();
43 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
44 if (!owner) {
45 return;
46 }
47 owner._emit(SearchBarBase.submitEvent);
48 };
49 UISearchBarDelegateImpl.ObjCProtocols = [UISearchBarDelegate];
50 return UISearchBarDelegateImpl;
51}(NSObject));
52var UISearchBarImpl = /** @class */ (function (_super) {
53 __extends(UISearchBarImpl, _super);
54 function UISearchBarImpl() {
55 return _super !== null && _super.apply(this, arguments) || this;
56 }
57 UISearchBarImpl.prototype.sizeThatFits = function (size) {
58 // iOS11 SDK does not support passing sizeThatFits(...) non-finite width value;
59 // iOS layout system will take care to size the element properly when passed 0
60 if (majorVersion >= 11 && size.width === Number.POSITIVE_INFINITY) {
61 size.width = 0;
62 }
63 return _super.prototype.sizeThatFits.call(this, size);
64 };
65 return UISearchBarImpl;
66}(UISearchBar));
67export class SearchBar extends SearchBarBase {
68 createNativeView() {
69 return UISearchBarImpl.new();
70 }
71 initNativeView() {
72 super.initNativeView();
73 this._delegate = UISearchBarDelegateImpl.initWithOwner(new WeakRef(this));
74 this.nativeViewProtected.delegate = this._delegate;
75 }
76 disposeNativeView() {
77 this._delegate = null;
78 super.disposeNativeView();
79 }
80 dismissSoftInput() {
81 this.ios.resignFirstResponder();
82 }
83 // @ts-ignore
84 get ios() {
85 return this.nativeViewProtected;
86 }
87 get _textField() {
88 if (!this.__textField) {
89 this.__textField = this.ios.valueForKey('searchField');
90 }
91 return this.__textField;
92 }
93 [isEnabledProperty.setNative](value) {
94 const nativeView = this.nativeViewProtected;
95 if (nativeView instanceof UIControl) {
96 nativeView.enabled = value;
97 }
98 const textField = this._textField;
99 if (textField) {
100 textField.enabled = value;
101 }
102 }
103 [backgroundColorProperty.getDefault]() {
104 return this.ios.barTintColor;
105 }
106 [backgroundColorProperty.setNative](value) {
107 const color = value instanceof Color ? value.ios : value;
108 this.ios.barTintColor = color;
109 }
110 [colorProperty.getDefault]() {
111 const sf = this._textField;
112 if (sf) {
113 return sf.textColor;
114 }
115 return null;
116 }
117 [colorProperty.setNative](value) {
118 const sf = this._textField;
119 const color = value instanceof Color ? value.ios : value;
120 if (sf) {
121 sf.textColor = color;
122 sf.tintColor = color;
123 }
124 }
125 [fontInternalProperty.getDefault]() {
126 const sf = this._textField;
127 return sf ? sf.font : null;
128 }
129 [fontInternalProperty.setNative](value) {
130 const sf = this._textField;
131 if (sf) {
132 sf.font = value instanceof Font ? value.getUIFont(sf.font) : value;
133 }
134 }
135 [backgroundInternalProperty.getDefault]() {
136 return null;
137 }
138 [backgroundInternalProperty.setNative](value) {
139 //
140 }
141 [textProperty.getDefault]() {
142 return '';
143 }
144 [textProperty.setNative](value) {
145 const text = value === null || value === undefined ? '' : value.toString();
146 this.ios.text = text;
147 }
148 [hintProperty.getDefault]() {
149 return '';
150 }
151 [hintProperty.setNative](value) {
152 this._updateAttributedPlaceholder();
153 }
154 [textFieldBackgroundColorProperty.getDefault]() {
155 const textField = this._textField;
156 if (textField) {
157 return textField.backgroundColor;
158 }
159 return null;
160 }
161 [textFieldBackgroundColorProperty.setNative](value) {
162 const color = value instanceof Color ? value.ios : value;
163 const textField = this._textField;
164 if (textField) {
165 textField.backgroundColor = color;
166 }
167 }
168 [textFieldHintColorProperty.getDefault]() {
169 return null;
170 }
171 [textFieldHintColorProperty.setNative](value) {
172 this._updateAttributedPlaceholder();
173 }
174 // Very similar to text-field.ios.ts implementation. Maybe unify APIs and base classes?
175 _updateAttributedPlaceholder() {
176 let stringValue = this.hint;
177 if (stringValue === null || stringValue === void 0) {
178 stringValue = '';
179 }
180 else {
181 stringValue = stringValue + '';
182 }
183 if (stringValue === '') {
184 // we do not use empty string since initWithStringAttributes does not return proper value and
185 // nativeView.attributedPlaceholder will be null
186 stringValue = ' ';
187 }
188 const attributes = {};
189 if (this.textFieldHintColor) {
190 attributes[NSForegroundColorAttributeName] = this.textFieldHintColor.ios;
191 }
192 const attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(stringValue, attributes);
193 this._textField.attributedPlaceholder = attributedPlaceholder;
194 }
195}
196//# sourceMappingURL=index.ios.js.map
\No newline at end of file