Skip to content

Commit f771b48

Browse files
committed
Implement Renderer.setElementStyle.
Support style bindings: - [ngStyle]="{'background-color': 'red'}" - [style.backgroundColor]="'red'"
1 parent 5ecacd0 commit f771b48

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/nativescript-angular/view-util.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {ContentView} from 'ui/content-view';
55
import {LayoutBase} from 'ui/layouts/layout-base';
66
import {ViewClass, getViewClass, getViewMeta, isKnownView, ViewExtensions, NgView, ViewClassMeta} from './element-registry';
77
import {getSpecialPropertySetter} from "ui/builder/special-properties";
8+
import * as styleProperty from "ui/styling/style-property";
9+
import {StyleProperty, getPropertyByName, withStyleProperty} from "ui/styling/style-property";
10+
import {ValueSource} from "ui/core/dependency-observable";
811
import { ActionBar, ActionItem, NavigationButton } from "ui/action-bar";
912
import trace = require("trace");
1013
import {device, platformNames, Device} from "platform";
@@ -287,7 +290,38 @@ export class ViewUtil {
287290
view.cssClass = classValue;
288291
}
289292

293+
private resolveCssValue(styleValue: string): string {
294+
return styleValue;
295+
}
296+
297+
private setStyleValue(view: NgView, property: StyleProperty, value: any) {
298+
try {
299+
view.style._setValue(property, value, ValueSource.Local);
300+
} catch (ex) {
301+
trace.write("Error setting property: " + property.name + " view: " + view + " value: " + value + " " + ex, trace.categories.Style, trace.messageType.error);
302+
}
303+
}
304+
290305
public setStyleProperty(view: NgView, styleName: string, styleValue: string): void {
291-
throw new Error("Not implemented: setStyleProperty");
306+
traceLog("setStyleProperty: " + styleName + " = " + styleValue);
307+
308+
let name = styleName;
309+
let resolvedValue = this.resolveCssValue(styleValue);
310+
withStyleProperty(name, resolvedValue, (property, value) => {
311+
if (isString(property)) {
312+
//Fall back to resolving property by name.
313+
const propertyName = <string>property;
314+
const resolvedProperty = getPropertyByName(name);
315+
if (resolvedProperty) {
316+
this.setStyleValue(view, resolvedProperty, resolvedValue);
317+
} else {
318+
traceLog("Unknown style property: " + styleName);
319+
}
320+
} else {
321+
const resolvedProperty = <StyleProperty>property;
322+
this.setStyleValue(view, resolvedProperty, resolvedValue);
323+
}
324+
325+
});
292326
}
293327
}

tests/app/tests/style-properties.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//make sure you import mocha-config before @angular/core
2+
import {assert} from "./test-config";
3+
import {TextField} from "ui/text-field";
4+
import {Red, Lime} from "color/known-colors";
5+
import {NativeScriptRenderer, NativeScriptRootRenderer} from "nativescript-angular/renderer";
6+
import {device} from "platform";
7+
import {RenderComponentType} from '@angular/core/src/render/api';
8+
import {NgView} from "nativescript-angular/view-util";
9+
10+
describe("Setting style properties", () => {
11+
let renderer: NativeScriptRenderer = null;
12+
let element: NgView = null;
13+
14+
beforeEach(() => {
15+
const rootRenderer = new NativeScriptRootRenderer(null, device);
16+
const componentType = new RenderComponentType("id", "templateUrl", 0,
17+
null, []);
18+
renderer = new NativeScriptRenderer(rootRenderer, componentType);
19+
element = <NgView><any>new TextField();
20+
});
21+
22+
it("resolves hyphenated CSS names", () => {
23+
renderer.setElementStyle(element, "background-color", "red");
24+
assert.equal(Red, element.style.backgroundColor.hex);
25+
});
26+
27+
it("resolves camel-cased JavaScript names", () => {
28+
renderer.setElementStyle(element, "backgroundColor", "lime");
29+
assert.equal(Lime, element.style.backgroundColor.hex);
30+
});
31+
32+
it("resolves CSS shorthand properties", () => {
33+
renderer.setElementStyle(element, "font", "12");
34+
assert.equal(12, element.style.fontSize);
35+
});
36+
})

0 commit comments

Comments
 (0)