Skip to content

Commit 1012dfc

Browse files
rxavierskborchers
authored andcommitted
Support react elements on properties
1 parent bd22af5 commit 1012dfc

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

examples/components/messages.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var FormatMessage = require('../react-globalize').FormatMessage;
2+
var FormatNumber = require('../react-globalize').FormatNumber;
23
var React = require('react');
3-
var Globalize = require('globalize');
44

55
module.exports = React.createClass({
66
getInitialState: function() {
@@ -46,7 +46,10 @@ module.exports = React.createClass({
4646
<br/>
4747
task count 0 - <FormatMessage locale={this.state.locale} path="task" variables={{count: 0}} />
4848
<br/>
49-
task count 1000 formatted - <FormatMessage ref="formattedTask" locale={this.state.locale} path="task" variables={{count: 1000, formattedCount: Globalize(this.state.locale).formatNumber(1000)}} />
49+
task count 1000 formatted - <FormatMessage ref="formattedTask" locale={this.state.locale} path="task" variables={{
50+
count: 1000,
51+
formattedCount: <FormatNumber locale={this.state.locale} value={1000} />
52+
}} />
5053
<br/>
5154
like count 0 with offset:1 - <FormatMessage locale={this.state.locale} path="likeIncludingMe" variables={{count: 0}} />
5255
<br/>

index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ function capitalizeFirstLetter(string) {
66
return string.charAt(0).toUpperCase() + string.slice(1);
77
}
88

9+
function isPlainObject(value) {
10+
return value && typeof value === "object" &&
11+
!(value.constructor &&
12+
!value.constructor.prototype.hasOwnProperty("isPrototypeOf")) &&
13+
!React.isValidElement(value);
14+
}
15+
16+
function supportReactElements(value) {
17+
if (isPlainObject(value)) {
18+
return Object.keys(value).reduce(function(sum, i) {
19+
sum[i] = supportReactElements(value[i]);
20+
return sum;
21+
}, {});
22+
} else if (React.isValidElement(value)) {
23+
value = new ReactGlobalize[value.type.displayName](value._store.props).render()._store.props.children;
24+
}
25+
return value;
26+
}
27+
928
Object.getOwnPropertyNames(Globalize).forEach(function(fn) {
1029
if (fn.indexOf("format") === 0) {
1130
var Fn = capitalizeFirstLetter(fn);
@@ -21,7 +40,7 @@ Object.getOwnPropertyNames(Globalize).forEach(function(fn) {
2140
var instance = Globalize;
2241
var propArgs = argArray.map(function(element) {
2342
return componentProps[element.replace(/(\s\/\*|\*\/)/,"").trim()];
24-
});
43+
}).map(supportReactElements);
2544

2645
if (componentProps["locale"]) {
2746
instance = Globalize(componentProps["locale"]);

0 commit comments

Comments
 (0)