@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
***************************************************************************** */
16
16
17
- import { TextField } from "ui/text-field " ;
17
+ import { Label } from "ui/label " ;
18
18
import { ListPicker } from "ui/list-picker" ;
19
19
import * as dependencyObservable from "ui/core/dependency-observable" ;
20
20
import { Observable , PropertyChangeData } from "data/observable" ;
@@ -24,12 +24,15 @@ import * as utils from "utils/utils";
24
24
import { Font } from "ui/styling/font" ;
25
25
import { Span } from "text/span" ;
26
26
import { FormattedString } from "text/formatted-string" ;
27
+ import { Color } from "color" ;
27
28
import * as enums from "ui/enums" ;
29
+ import * as types from "utils/types" ;
28
30
import { SelectedIndexChangedEventData } from "nativescript-drop-down" ;
29
31
30
32
global . moduleMerge ( common , exports ) ;
31
33
32
34
const TOOLBAR_HEIGHT = 44 ;
35
+ const HINT_COLOR = new Color ( "#3904041E" ) ;
33
36
34
37
export class DropDown extends common . DropDown {
35
38
private _toolbar : UIToolbar ;
@@ -38,19 +41,18 @@ export class DropDown extends common.DropDown {
38
41
private _doneTapDelegate : TapHandler ;
39
42
private _accessoryViewVisible : boolean ;
40
43
41
- public _textField : TextField ;
44
+ public _label : DropDownLabelWrapper ;
42
45
public _listPicker : ListPicker ;
43
46
44
47
constructor ( ) {
45
48
super ( ) ;
46
49
47
50
let applicationFrame = utils . ios . getter ( UIScreen , UIScreen . mainScreen ) . applicationFrame ;
48
51
49
- this . _textField = new TextField ( ) ;
52
+ this . _label = new DropDownLabelWrapper ( this ) ;
50
53
this . _listPicker = new ListPicker ( ) ;
51
54
52
- ( this . _listPicker as any ) . _delegate = DropDownListPickerDelegateImpl . initWithOwner ( this ) ;
53
- ( this . _textField as any ) . _delegate = DropDownTextFieldDelegateImpl . initWithOwner ( this ) ;
55
+ ( this . _listPicker as any ) . _delegate = DropDownListPickerDelegateImpl . initWithOwner ( this ) ;
54
56
this . _flexToolbarSpace = UIBarButtonItem . alloc ( ) . initWithBarButtonSystemItemTargetAction ( UIBarButtonSystemItem . FlexibleSpace , null , null ) ;
55
57
this . _doneTapDelegate = TapHandler . initWithOwner ( new WeakRef ( this ) ) ;
56
58
this . _doneButton = UIBarButtonItem . alloc ( ) . initWithBarButtonSystemItemTargetAction ( UIBarButtonSystemItem . Done , this . _doneTapDelegate , "tap" ) ;
@@ -65,8 +67,8 @@ export class DropDown extends common.DropDown {
65
67
this . _toolbar . setItemsAnimated ( nsArray , false ) ;
66
68
}
67
69
68
- get ios ( ) : UITextField {
69
- return this . _textField . ios ;
70
+ get ios ( ) : UILabel {
71
+ return this . _label . ios ;
70
72
}
71
73
72
74
get accessoryViewVisible ( ) : boolean {
@@ -84,7 +86,7 @@ export class DropDown extends common.DropDown {
84
86
public onLoaded ( ) {
85
87
super . onLoaded ( ) ;
86
88
87
- this . _textField . onLoaded ( ) ;
89
+ this . _label . onLoaded ( ) ;
88
90
this . _listPicker . onLoaded ( ) ;
89
91
this . _listPicker . on ( Observable . propertyChangeEvent ,
90
92
( data : PropertyChangeData ) => {
@@ -103,28 +105,28 @@ export class DropDown extends common.DropDown {
103
105
104
106
this . _listPicker . off ( Observable . propertyChangeEvent ) ;
105
107
106
- this . _textField . onUnloaded ( ) ;
108
+ this . _label . onUnloaded ( ) ;
107
109
this . _listPicker . onUnloaded ( ) ;
108
110
109
111
super . onUnloaded ( ) ;
110
112
}
111
113
112
114
public open ( ) {
113
- this . _textField . focus ( ) ;
115
+ this . _label . ios . becomeFirstResponder ( ) ;
114
116
}
115
117
116
118
public _onItemsPropertyChanged ( data : dependencyObservable . PropertyChangeData ) {
117
119
this . _listPicker . items = data . newValue ;
118
120
}
119
121
120
122
public _onHintPropertyChanged ( data : dependencyObservable . PropertyChangeData ) {
121
- this . _textField . hint = data . newValue ;
123
+ this . _label . hint = data . newValue ;
122
124
}
123
125
124
126
public _onSelectedIndexPropertyChanged ( data : dependencyObservable . PropertyChangeData ) {
125
127
super . _onSelectedIndexPropertyChanged ( data ) ;
126
128
this . _listPicker . selectedIndex = data . newValue ;
127
- this . _textField . text = ( this . items && this . items . getItem ? this . items . getItem ( data . newValue ) : this . items [ data . newValue ] ) ;
129
+ this . _label . text = ( this . items && this . items . getItem ? this . items . getItem ( data . newValue ) : this . items [ data . newValue ] ) ;
128
130
}
129
131
}
130
132
@@ -147,27 +149,6 @@ class TapHandler extends NSObject {
147
149
}
148
150
}
149
151
150
- class DropDownTextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
151
- public static ObjCProtocols = [ UITextFieldDelegate ] ;
152
-
153
- private _owner : WeakRef < DropDown > ;
154
-
155
- public static initWithOwner ( owner : DropDown ) : DropDownTextFieldDelegateImpl {
156
- let delegate = < DropDownTextFieldDelegateImpl > DropDownTextFieldDelegateImpl . new ( ) ;
157
- delegate . _owner = new WeakRef ( owner ) ;
158
- return delegate ;
159
- }
160
-
161
- public textFieldDidBeginEditing ( textField : UITextField ) {
162
- let owner = this . _owner . get ( ) ;
163
-
164
- owner . notify ( {
165
- eventName : common . DropDown . openedEvent ,
166
- object : owner
167
- } ) ;
168
- }
169
- }
170
-
171
152
class DropDownListPickerDelegateImpl extends NSObject implements UIPickerViewDelegate {
172
153
public static ObjCProtocols = [ UIPickerViewDelegate ] ;
173
154
@@ -221,6 +202,129 @@ class DropDownListPickerDelegateImpl extends NSObject implements UIPickerViewDel
221
202
}
222
203
}
223
204
205
+ class DropDownLabelWrapper extends Label {
206
+ private _ios : UILabel ;
207
+ private _hint : string = "" ;
208
+ private _hasText : boolean = true ;
209
+ private _internalColor : Color ;
210
+
211
+ constructor ( dropDown : DropDown ) {
212
+ super ( ) ;
213
+
214
+ this . _ios = DropDownLabel . initWithOwner ( dropDown ) ;
215
+ this . _ios . userInteractionEnabled = true ;
216
+ }
217
+
218
+ public onLoaded ( ) {
219
+ super . onLoaded ( ) ;
220
+ this . internalColor = this . color ;
221
+ }
222
+
223
+ get text ( ) : string {
224
+ return this . _ios . text ;
225
+ }
226
+ set text ( value : string ) {
227
+ let actualText = value || this . _hint || "" ;
228
+
229
+ this . _hasText = ! types . isNullOrUndefined ( value ) ;
230
+ this . _ios . text = ( actualText === "" ? " " : actualText ) ; // HACK: If empty use <space> so the label does not collapse
231
+
232
+ this . _refreshColor ( ) ;
233
+ }
234
+
235
+ get hint ( ) : string {
236
+ return this . _hint ;
237
+ }
238
+ set hint ( value : string ) {
239
+ this . _hint = value ;
240
+
241
+ if ( ! this . _hasText ) {
242
+ this . _ios . text = value ;
243
+ }
244
+ }
245
+
246
+ get internalColor ( ) : Color {
247
+ return this . _internalColor ;
248
+ }
249
+ set internalColor ( value : Color ) {
250
+ this . _internalColor = value ;
251
+ this . _refreshColor ( ) ;
252
+ }
253
+
254
+ private _refreshColor ( ) {
255
+ this . color = ( this . _hasText ? this . _internalColor : HINT_COLOR ) ;
256
+ }
257
+ }
258
+
259
+ class DropDownLabel extends TNSLabel {
260
+ private _inputView : UIView ;
261
+ private _inputAccessoryView : UIView ;
262
+ private _isInputViewOpened : boolean ;
263
+ private _owner : WeakRef < DropDown > ;
264
+
265
+ public static initWithOwner ( owner : DropDown ) : DropDownLabel {
266
+ let label = < DropDownLabel > DropDownLabel . new ( ) ;
267
+ label . _owner = new WeakRef ( owner ) ;
268
+ label . _isInputViewOpened = false ;
269
+ return label ;
270
+ }
271
+
272
+ get inputView ( ) : UIView {
273
+ return this . _inputView ;
274
+ }
275
+ set inputView ( value : UIView ) {
276
+ this . _inputView = value ;
277
+ }
278
+
279
+ get inputAccessoryView ( ) : UIView {
280
+ return this . _inputAccessoryView ;
281
+ }
282
+ set inputAccessoryView ( value : UIView ) {
283
+ this . _inputAccessoryView = value ;
284
+ }
285
+
286
+ get canBecomeFirstResponder ( ) : boolean {
287
+ return true ;
288
+ }
289
+
290
+ get canResignFirstResponder ( ) : boolean {
291
+ return true ;
292
+ }
293
+
294
+ public becomeFirstResponder ( ) : boolean {
295
+ let result = super . becomeFirstResponder ( ) ;
296
+
297
+ if ( result ) {
298
+ if ( ! this . _isInputViewOpened ) {
299
+ let owner = this . _owner . get ( ) ;
300
+
301
+ owner . notify ( {
302
+ eventName : common . DropDown . openedEvent ,
303
+ object : owner
304
+ } ) ;
305
+ }
306
+
307
+ this . _isInputViewOpened = true ;
308
+ }
309
+
310
+ return result ;
311
+ }
312
+
313
+ public resignFirstResponder ( ) : boolean {
314
+ let result = super . resignFirstResponder ( ) ;
315
+
316
+ if ( result ) {
317
+ this . _isInputViewOpened = false ;
318
+ }
319
+
320
+ return result ;
321
+ }
322
+
323
+ public touchesEndedWithEvent ( touches : NSSet < UITouch > , event : UIEvent ) {
324
+ this . becomeFirstResponder ( ) ;
325
+ }
326
+ }
327
+
224
328
//#region Styling
225
329
export class DropDownStyler implements style . Styler {
226
330
//#region Font
@@ -258,36 +362,36 @@ export class DropDownStyler implements style.Styler {
258
362
259
363
//#region Text Decoration
260
364
private static setTextDecorationProperty ( dropDown : DropDown , newValue : any ) {
261
- dropDown . _textField . style . textDecoration = newValue ;
262
- ( < any > dropDown . _textField . style ) . _updateTextDecoration ( ) ;
365
+ dropDown . _label . style . textDecoration = newValue ;
366
+ ( < any > dropDown . _label . style ) . _updateTextDecoration ( ) ;
263
367
}
264
368
265
369
private static resetTextDecorationProperty ( dropDown : DropDown , nativeValue : any ) {
266
- dropDown . _textField . style . textDecoration = enums . TextDecoration . none ;
267
- dropDown . _textField . style . _updateTextDecoration ( ) ;
370
+ dropDown . _label . style . textDecoration = enums . TextDecoration . none ;
371
+ dropDown . _label . style . _updateTextDecoration ( ) ;
268
372
}
269
373
//#endregion
270
374
271
375
//#region Color
272
376
private static setColorProperty ( dropDown : DropDown , newValue : any ) {
273
- let ios = dropDown . ios ;
274
- let pickerView = < UIPickerView > dropDown . _listPicker . ios ;
377
+ let dropDownLabel = dropDown . _label ,
378
+ pickerView = < UIPickerView > dropDown . _listPicker . ios ;
275
379
276
- ios . textColor = newValue ;
380
+ dropDownLabel . internalColor = utils . ios . getColor ( newValue ) ;
277
381
pickerView . reloadAllComponents ( ) ;
278
382
}
279
383
280
384
private static resetColorProperty ( dropDown : DropDown , nativeValue : any ) {
281
- let ios = dropDown . ios ;
282
- let pickerView = < UIPickerView > dropDown . _listPicker . ios ;
385
+ let dropDownLabel = dropDown . _label ,
386
+ pickerView = < UIPickerView > dropDown . _listPicker . ios ;
283
387
284
- ios . textColor = nativeValue ;
388
+ dropDownLabel . internalColor = utils . ios . getColor ( nativeValue ) ;
285
389
pickerView . reloadAllComponents ( ) ;
286
390
}
287
391
288
392
private static getNativeColorValue ( dropDown : DropDown ) : any {
289
- let ios = dropDown . ios ;
290
- return ios . textColor ;
393
+ let dropDownLabel = dropDown . _label ;
394
+ return dropDownLabel . internalColor ? dropDownLabel . internalColor . ios : dropDownLabel . ios . textColor ;
291
395
}
292
396
//#endregion
293
397
@@ -332,13 +436,13 @@ export class DropDownStyler implements style.Styler {
332
436
}
333
437
334
438
private static setPadding ( dropDown : DropDown , newValue : UIEdgeInsets ) {
335
- dropDown . _textField . style . paddingTop = newValue . top ;
336
- dropDown . _textField . style . paddingRight = newValue . right ;
337
- dropDown . _textField . style . paddingBottom = newValue . bottom ;
338
- dropDown . _textField . style . paddingLeft = newValue . left ;
439
+ dropDown . _label . style . paddingTop = newValue . top ;
440
+ dropDown . _label . style . paddingRight = newValue . right ;
441
+ dropDown . _label . style . paddingBottom = newValue . bottom ;
442
+ dropDown . _label . style . paddingLeft = newValue . left ;
339
443
}
340
444
//#endregion
341
-
445
+
342
446
public static registerHandlers ( ) {
343
447
style . registerHandler ( style . fontInternalProperty ,
344
448
new style . StylePropertyChangedHandler (
0 commit comments