Skip to content

Commit 8711b54

Browse files
Shorthand properties does not update if sub-property is changed (NativeScript#4607)
1 parent 4365979 commit 8711b54

File tree

2 files changed

+99
-17
lines changed

2 files changed

+99
-17
lines changed

tests/app/ui/view/view-tests-common.ts

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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";
33
import { topmost } from "tns-core-modules/ui/frame";
44
import { Page } from "tns-core-modules/ui/page";
55
import { Button } from "tns-core-modules/ui/button";
@@ -256,6 +256,43 @@ const customCssProperty = new CssProperty<Style, string>({ name: "customCssPrope
256256
const customCssAnimationProperty = new CssAnimationProperty<Style, string>({ name: "customCssAnimationProperty", cssName: "custom-css-animation-property" });
257257
const customViewProperty = new Property<TestView, string>({ name: "custom" });
258258

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+
259296
class TestView extends Layout {
260297
public inheritanceTest: number;
261298
public booleanInheritanceTest: boolean;
@@ -288,6 +325,28 @@ class TestView extends Layout {
288325
this.style["customCssAnimationProperty"] = value;
289326
}
290327

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+
291350
private _nativeView;
292351
constructor(public name: string) {
293352
super();
@@ -981,4 +1040,33 @@ export function test_background_image_doesnt_throw() {
9811040
helper.buildUIAndRunTest(btn, function (views: Array<View>) {
9821041
helper.waitUntilLayoutReady(btn);
9831042
});
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.");
9841072
}

tns-core-modules/ui/core/properties/properties.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -996,25 +996,19 @@ export class ShorthandProperty<T extends Style, P> implements definitions.Shorth
996996
const converter = options.converter;
997997

998998
function setLocalValue(this: T, value: string | P): void {
999-
if (this[key] !== value) {
1000-
this[key] = value;
1001-
this.view._batchUpdate(() => {
1002-
for (let [p, v] of converter(value)) {
1003-
this[p.name] = v;
1004-
}
1005-
});
1006-
}
999+
this.view._batchUpdate(() => {
1000+
for (let [p, v] of converter(value)) {
1001+
this[p.name] = v;
1002+
}
1003+
});
10071004
}
10081005

10091006
function setCssValue(this: T, value: string): void {
1010-
if (this[key] !== value) {
1011-
this[key] = value;
1012-
this.view._batchUpdate(() => {
1013-
for (let [p, v] of converter(value)) {
1014-
this[p.cssName] = v;
1015-
}
1016-
});
1017-
}
1007+
this.view._batchUpdate(() => {
1008+
for (let [p, v] of converter(value)) {
1009+
this[p.cssName] = v;
1010+
}
1011+
});
10181012
}
10191013

10201014
this.cssValueDescriptor = {

0 commit comments

Comments
 (0)