1
1
import * as TKUnit from "../../TKUnit" ;
2
- import { View , eachDescendant , getViewById , InheritedProperty , CssProperty , CssAnimationProperty , Property , Style } from "tns-core-modules/ui/core/view" ;
2
+ import { View , eachDescendant , getViewById , InheritedProperty , CssProperty , CssAnimationProperty , ShorthandProperty , Property , Style } from "tns-core-modules/ui/core/view" ;
3
3
import { topmost } from "tns-core-modules/ui/frame" ;
4
4
import { Page } from "tns-core-modules/ui/page" ;
5
5
import { Button } from "tns-core-modules/ui/button" ;
@@ -256,6 +256,43 @@ const customCssProperty = new CssProperty<Style, string>({ name: "customCssPrope
256
256
const customCssAnimationProperty = new CssAnimationProperty < Style , string > ( { name : "customCssAnimationProperty" , cssName : "custom-css-animation-property" } ) ;
257
257
const customViewProperty = new Property < TestView , string > ( { name : "custom" } ) ;
258
258
259
+ const customCssAProperty = new CssProperty < Style , string > ( {
260
+ name : "customCssA" ,
261
+ cssName : "custom-css-a" ,
262
+ valueChanged ( target , oldValue , newValue ) {
263
+ if ( ( < any > target ) . customCssAPropertyChanged ) {
264
+ ( < any > target ) . customCssAPropertyChanged ( oldValue , newValue ) ;
265
+ }
266
+ }
267
+ } ) ;
268
+ customCssAProperty . register ( Style ) ;
269
+ const customCssBProperty = new CssProperty < Style , string > ( {
270
+ name : "customCssB" ,
271
+ cssName : "custom-css-b" ,
272
+ valueChanged ( target , oldValue , newValue ) {
273
+ if ( ( < any > target ) . customCssBPropertyChanged ) {
274
+ ( < any > target ) . customCssBPropertyChanged ( oldValue , newValue ) ;
275
+ }
276
+ }
277
+ } ) ;
278
+ customCssBProperty . register ( Style ) ;
279
+ const customShortHandProperty = new ShorthandProperty < Style , string > ( {
280
+ name : "customShortHand" ,
281
+ cssName : "custom-short-hand" ,
282
+ converter ( value : string ) : [ CssProperty < any , any > , any ] [ ] {
283
+ console . log ( "Convert: " + value ) ;
284
+ const values = value . split ( "," ) ;
285
+ return [
286
+ [ customCssAProperty , values [ 0 ] ] ,
287
+ [ customCssBProperty , values [ 1 ] ]
288
+ ] ;
289
+ } ,
290
+ getter ( this : Style ) : string {
291
+ return `${ ( < any > this ) . customCssA } ,${ ( < any > this ) . customCssB } ` ;
292
+ }
293
+ } ) ;
294
+ customShortHandProperty . register ( Style ) ;
295
+
259
296
class TestView extends Layout {
260
297
public inheritanceTest : number ;
261
298
public booleanInheritanceTest : boolean ;
@@ -288,6 +325,28 @@ class TestView extends Layout {
288
325
this . style [ "customCssAnimationProperty" ] = value ;
289
326
}
290
327
328
+ get customCssA ( ) : string {
329
+ return ( < any > this . style ) . customCssA ;
330
+ }
331
+ set customCssA ( value : string ) {
332
+ ( < any > this . style ) . customCssA = value ;
333
+ }
334
+
335
+ get customCssB ( ) : string {
336
+ return ( < any > this . style ) . customCssB ;
337
+ }
338
+ set customCssB ( value : string ) {
339
+ ( < any > this . style ) . customCssB = value ;
340
+ }
341
+
342
+ get customShortHand ( ) : string {
343
+ return ( < any > this . style ) . customShortHand ;
344
+ }
345
+ set customShortHand ( value : string ) {
346
+ console . log ( "Setting style's customShortHand: " + value ) ;
347
+ ( < any > this . style ) . customShortHand = value ;
348
+ }
349
+
291
350
private _nativeView ;
292
351
constructor ( public name : string ) {
293
352
super ( ) ;
@@ -981,4 +1040,33 @@ export function test_background_image_doesnt_throw() {
981
1040
helper . buildUIAndRunTest ( btn , function ( views : Array < View > ) {
982
1041
helper . waitUntilLayoutReady ( btn ) ;
983
1042
} ) ;
1043
+ }
1044
+
1045
+ export function test_shorthand_property_sets_composite_properties ( ) {
1046
+ var view = new TestView ( "view1" ) ;
1047
+ view . customShortHand = "left,right" ;
1048
+ TKUnit . assertEqual ( view . customCssA , "left" , "Expected customCssAProperty to be 'left'." ) ;
1049
+ TKUnit . assertEqual ( view . customCssB , "right" , "Expected customCssAProperty to be 'right'." ) ;
1050
+ }
1051
+
1052
+ export function test_shorthand_property_is_set_by_composite_properties ( ) {
1053
+ var view = new TestView ( "view1" ) ;
1054
+ view . customCssA = "left" ;
1055
+ view . customCssB = "right" ;
1056
+ TKUnit . assertEqual ( view . customShortHand , "left,right" ) ;
1057
+ }
1058
+
1059
+ export function test_shorthand_property_doesnt_cache ( ) {
1060
+ var view = new TestView ( "view1" ) ;
1061
+ view . customShortHand = "left,right" ;
1062
+ TKUnit . assertEqual ( view . customCssA , "left" , "Expected customCssA to be 'left' the first time." ) ;
1063
+ TKUnit . assertEqual ( view . customCssB , "right" , "Expected customCssA to be 'right' the second time." ) ;
1064
+ view . customCssA = "top" ;
1065
+ view . customCssB = "bottom" ;
1066
+ TKUnit . assertEqual ( view . customShortHand , "top,bottom" , "Expected customShortHand to be 'top,bottom' calculated from the composite properties." ) ;
1067
+ // This used to fail in https://github.com/NativeScript/NativeScript/issues/4450 the customShortHand would cache "left,right" when initially set,
1068
+ // And won't run internal logic as the new value is "same" as the previous value, not taking into account that meanwhile the composite properties were set.
1069
+ view . customShortHand = "left,right" ;
1070
+ TKUnit . assertEqual ( view . customCssA , "left" , "Expected customCssA to be 'left' the second time." ) ;
1071
+ TKUnit . assertEqual ( view . customCssB , "right" , "Expected customCssA to be 'right' the second time." ) ;
984
1072
}
0 commit comments