Skip to content

Commit f83b9b9

Browse files
committed
Change API to mixin to allow better use with other components
1 parent 2084ebf commit f83b9b9

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

index.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
var React = require('react');
21
var Globalize = require('globalize');
2+
var mixins = {};
33

44
Object.getOwnPropertyNames(Globalize).forEach(function(fn) {
55
if (fn.indexOf("format") === 0) {
6-
var fnString = Globalize[fn].toString(),
7-
argString = fnString.substr(fnString.indexOf('(')+1, fnString.indexOf(')')-(fnString.indexOf('(')+1)).trim(),
8-
argArray = argString.split(', ');
6+
var fnString = Globalize[fn].toString();
7+
var argString = fnString.substr(fnString.indexOf('(')+1, fnString.indexOf(')')-(fnString.indexOf('(')+1)).trim();
8+
var argArray = argString.split(', ');
99

1010
(function(currentFn, currentArgs) {
11-
module.exports[currentFn.charAt(0).toUpperCase() + currentFn.slice(1)] = React.createClass({
12-
currentArgs: currentArgs,
13-
render: function() {
14-
var that = this;
15-
var propArgs = this.currentArgs.map(function(element) {
16-
return that.props[element.replace(/(\s\/\*|\*\/)/,'')];
17-
});
11+
var formatter = function(nextProps) {
12+
var componentProps = nextProps || this.props;
13+
var propArgs = currentArgs.map(function(element) {
14+
return componentProps[element.replace(/(\s\/\*|\*\/)/,'')];
15+
});
1816

19-
// set locale
20-
Globalize.locale( this.props.locale );
17+
Globalize.locale( componentProps["locale"] );
18+
this.setState({
19+
formattedValue: Globalize[currentFn].apply(Globalize, propArgs)
20+
});
21+
};
2122

22-
return (
23-
React.createElement("span", null, Globalize[currentFn].apply(Globalize, propArgs))
24-
);
25-
}
26-
});
23+
mixins[currentFn] = {
24+
componentWillMount: formatter,
25+
componentWillReceiveProps: formatter
26+
};
2727
})(fn, argArray);
2828
}
2929
});
30+
module.exports = mixins;

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"browserify": "~9.0.3"
2424
},
2525
"peerDependencies": {
26-
"globalize": "~1.0.0-alpha.17",
2726
"react": "~0.12.2"
2827
}
2928
}

0 commit comments

Comments
 (0)