11import React from "react" ;
22import Globalize from "globalize" ;
33
4- var commonProps = [ "elements" , "locale" ] ;
4+ var commonPropNames = [ "elements" , "locale" ] ;
55
66function capitalizeFirstLetter ( string ) {
77 return string . charAt ( 0 ) . toUpperCase ( ) + string . slice ( 1 ) ;
@@ -13,42 +13,43 @@ function omit(set) {
1313 } ;
1414}
1515
16- function generator ( fn , argArray , options ) {
17- var Fn = capitalizeFirstLetter ( fn ) ;
16+ function generator ( fn , localPropNames , options ) {
1817 options = options || { } ;
18+ var Fn = capitalizeFirstLetter ( fn ) ;
1919 var beforeFormat = options . beforeFormat || function ( ) { } ;
2020 var afterFormat = options . afterFormat || function ( formattedValue ) {
2121 return formattedValue ;
2222 } ;
23- return {
23+ var globalizePropNames = commonPropNames . concat ( localPropNames ) ;
24+
25+ return React . createClass ( {
2426 displayName : Fn ,
25- format : function ( ) {
26- return this . instance [ fn ] . apply ( this . instance , this . args ) ;
27+ componentWillMount : function ( ) {
28+ this . setup ( this . props ) ;
2729 } ,
28- render : function ( ) {
29- var props = this . props ;
30- this . instance = Globalize ;
31- this . args = argArray . map ( function ( element ) {
32- return props [ element ] ;
33- } ) ;
34-
35- // otherProps = this.props - argArray - commonProps.
36- var otherProps = Object . keys ( props ) . filter ( omit ( argArray ) ) . filter ( omit ( commonProps ) ) . reduce ( function ( memo , propKey ) {
30+ componentWillReceiveProps : function ( nextProps ) {
31+ this . setup ( nextProps ) ;
32+ } ,
33+ setup : function ( props ) {
34+ this . globalize = props . locale ? Globalize ( props . locale ) : Globalize ;
35+ this . domProps = Object . keys ( props ) . filter ( omit ( globalizePropNames ) ) . reduce ( function ( memo , propKey ) {
3736 memo [ propKey ] = props [ propKey ] ;
3837 return memo ;
3938 } , { } ) ;
4039
41- // Get value from this.props.children.
42- this . args [ 0 ] = props . children ;
43-
44- if ( props . locale ) {
45- this . instance = Globalize ( props . locale ) ;
46- }
40+ this . globalizePropValues = localPropNames . map ( function ( element ) {
41+ return props [ element ] ;
42+ } ) ;
43+ this . globalizePropValues [ 0 ] = props . children ;
4744
4845 beforeFormat . call ( this ) ;
49- return React . DOM . span ( otherProps , afterFormat . call ( this , this . format ( ) ) ) ;
46+ const formattedValue = this . globalize [ fn ] . apply ( this . globalize , this . globalizePropValues ) ;
47+ this . value = afterFormat . call ( this , formattedValue ) ;
48+ } ,
49+ render : function ( ) {
50+ return React . DOM . span ( this . domProps , this . value ) ;
5051 }
51- } ;
52+ } ) ;
5253}
5354
5455export default generator ;
0 commit comments