Update TextBase/Button text property when formattedText changes

Fixes #1794
This commit is contained in:
Rossen Hristov
2016-03-18 14:17:08 +02:00
parent 7458880f28
commit 6c68423ee9
5 changed files with 99 additions and 8 deletions

View File

@ -5,6 +5,8 @@ import pagesModule = require("ui/page");
import buttonTestsNative = require("./button-tests-native");
import colorModule = require("color");
import enums = require("ui/enums");
import formattedStringModule = require("text/formatted-string");
import spanModule = require("text/span");
// <snippet module="ui/button" title="button">
// # Button
@ -291,3 +293,37 @@ export var testNativeTextAlignmentFromLocal = function () {
TKUnit.assert(actualResult === expectedTextAlignment, "Actual: " + actualResult + "; Expected: " + expectedTextAlignment);
});
}
export var test_WhenFormattedTextPropertyChanges_TextIsUpdated_Button = function () {
var firstSpan = new spanModule.Span();
firstSpan.fontSize = 10;
firstSpan.text = "First";
var secondSpan = new spanModule.Span();
secondSpan.fontSize = 15;
secondSpan.text = "Second";
var thirdSpan = new spanModule.Span();
thirdSpan.fontSize = 20;
thirdSpan.text = "Third";
var formattedString1 = new formattedStringModule.FormattedString();
formattedString1.spans.push(firstSpan);
var formattedString2 = new formattedStringModule.FormattedString();
formattedString2.spans.push(secondSpan);
formattedString2.spans.push(thirdSpan);
var view = new buttonModule.Button();
helper.buildUIAndRunTest(view, function (views: Array<viewModule.View>) {
TKUnit.assertEqual(view.text, "");
view.formattedText = formattedString1;
TKUnit.assertEqual(view.text, "First");
view.formattedText = formattedString2;
TKUnit.assertEqual(view.text, "SecondThird");
formattedString2.spans[0].text = "Mecond";
TKUnit.assertEqual(view.text, "MecondThird");
view.formattedText = null;
TKUnit.assertEqual(view.text, "");
});
}

View File

@ -6,6 +6,8 @@ import textFieldTestsNative = require("./text-field-tests-native");
import colorModule = require("color");
import enums = require("ui/enums");
import platform = require("platform");
import formattedStringModule = require("text/formatted-string");
import spanModule = require("text/span");
// <snippet module="ui/text-field" title="TextField">
// # TextField
@ -468,3 +470,37 @@ export var testMemoryLeak = function (done) {
textFieldTestsNative.typeTextNatively(textField, "Hello, world!");
}, done);
}
export var test_WhenFormattedTextPropertyChanges_TextIsUpdated_TextBase = function () {
var firstSpan = new spanModule.Span();
firstSpan.fontSize = 10;
firstSpan.text = "First";
var secondSpan = new spanModule.Span();
secondSpan.fontSize = 15;
secondSpan.text = "Second";
var thirdSpan = new spanModule.Span();
thirdSpan.fontSize = 20;
thirdSpan.text = "Third";
var formattedString1 = new formattedStringModule.FormattedString();
formattedString1.spans.push(firstSpan);
var formattedString2 = new formattedStringModule.FormattedString();
formattedString2.spans.push(secondSpan);
formattedString2.spans.push(thirdSpan);
var view = new textFieldModule.TextField();
helper.buildUIAndRunTest(view, function (views: Array<viewModule.View>) {
TKUnit.assertEqual(view.text, "");
view.formattedText = formattedString1;
TKUnit.assertEqual(view.text, "First");
view.formattedText = formattedString2;
TKUnit.assertEqual(view.text, "SecondThird");
formattedString2.spans[0].text = "Mecond";
TKUnit.assertEqual(view.text, "MecondThird");
view.formattedText = null;
TKUnit.assertEqual(view.text, "");
});
}

View File

@ -740,6 +740,7 @@ export function test_parseSpansDirectlyOnLabel() {
var page = <Page>views[0];
var testLabel = <Label>page.getViewById("testLabel");
TKUnit.assertEqual(testLabel.formattedText + "", "We areAwesome", "Formatted string should be set");
TKUnit.assertEqual(testLabel.text + "", "We areAwesome", "Formatted string should be set");
}
helper.navigate(function () { return p; });
@ -757,6 +758,7 @@ export function test_parseSpansDirectlyOnButton() {
var page = <Page>views[0];
var testButton = <Button>page.getViewById("testButton");
TKUnit.assertEqual(testButton.formattedText + "", "We areAwesome", "Formatted string should be set");
TKUnit.assertEqual(testButton.text + "", "We areAwesome", "Formatted string should be set");
}
helper.navigate(function () { return p; });
@ -774,6 +776,7 @@ export function test_parseFormattedStringWithoutFormattedText() {
var page = <Page>views[0];
var testButton = <Button>page.getViewById("testButton");
TKUnit.assertEqual(testButton.formattedText + "", "author num_comments", "Formatted string should be set");
TKUnit.assertEqual(testButton.text + "", "author num_comments", "Formatted string should be set");
}
helper.navigate(function () { return p; });
@ -791,6 +794,7 @@ export function test_parseFormattedStringFullSyntax() {
var page = <Page>views[0];
var testButton = <Button>page.getViewById("testButton");
TKUnit.assertEqual(testButton.formattedText + "", "author num_comments", "Formatted string should be set");
TKUnit.assertEqual(testButton.text + "", "author num_comments", "Formatted string should be set");
}
helper.navigate(function () { return p; });
@ -808,6 +812,7 @@ export function test_parseSpansDirectlyToFormattedString() {
var page = <Page>views[0];
var testButton = <Button>page.getViewById("testButton");
TKUnit.assertEqual(testButton.formattedText + "", "author num_comments", "Formatted string should be set");
TKUnit.assertEqual(testButton.text + "", "author num_comments", "Formatted string should be set");
}
helper.navigate(function () { return p; });

View File

@ -99,7 +99,10 @@ export class Button extends view.View implements definition.Button {
}
private onFormattedTextChanged(eventData: observable.PropertyChangeData) {
this._setFormattedTextPropertyToNative(eventData.value);
var value = <formattedString.FormattedString>eventData.value;
this._setFormattedTextPropertyToNative(value);
this._onPropertyChangedFromNative(Button.textProperty, value.toString());
}
public _onTextPropertyChanged(data: dependencyObservable.PropertyChangeData) {
@ -111,10 +114,14 @@ export class Button extends view.View implements definition.Button {
}
public _onFormattedTextPropertyChanged(data: dependencyObservable.PropertyChangeData) {
if (data.newValue) {
(<formattedString.FormattedString>data.newValue).parent = this;
var newValue = <formattedString.FormattedString>data.newValue;
if (newValue) {
newValue.parent = this;
}
this._setFormattedTextPropertyToNative(data.newValue);
this._setFormattedTextPropertyToNative(newValue);
var newText = newValue ? newValue.toString() : "";
this._onPropertyChangedFromNative(Button.textProperty, newText);
}
public _addChildFromBuilder(name: string, value: any): void {

View File

@ -96,7 +96,10 @@ export class TextBase extends view.View implements definition.TextBase, formatte
}
private onFormattedTextChanged(eventData: observable.PropertyChangeData) {
this._setFormattedTextPropertyToNative(eventData.value);
var value = (<formattedString.FormattedString>eventData.value);
this._setFormattedTextPropertyToNative(value);
this._onPropertyChangedFromNative(TextBase.textProperty, value.toString());
}
public _onTextPropertyChanged(data: dependencyObservable.PropertyChangeData) {
@ -108,10 +111,14 @@ export class TextBase extends view.View implements definition.TextBase, formatte
}
public _onFormattedTextPropertyChanged(data: dependencyObservable.PropertyChangeData) {
if (data.newValue) {
(<formattedString.FormattedString>data.newValue).parent = this;
var newValue = (<formattedString.FormattedString>data.newValue);
if (newValue) {
newValue.parent = this;
}
this._setFormattedTextPropertyToNative(data.newValue);
this._setFormattedTextPropertyToNative(newValue);
var newText = newValue ? newValue.toString() : "";
this._onPropertyChangedFromNative(TextBase.textProperty, newText);
}
public _addChildFromBuilder(name: string, value: any): void {