react-numeral-input
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -40,2 +40,3 @@ // Generated by CoffeeScript 1.9.3 | ||
displayName: 'NumeralInput', | ||
node: null, | ||
propTypes: { | ||
@@ -51,8 +52,13 @@ onChange: React.PropTypes.func, | ||
formatPos: function(val, index) { | ||
var dotCount, sub; | ||
var dotCount, pos, sub; | ||
val = numeral().unformat(val); | ||
val = numeral(val).format(this.props.fmt); | ||
sub = val.substr(0, index); | ||
dotCount = sub.split(',').length - 1; | ||
return index - dotCount; | ||
sub = val.substr(0, index - 1); | ||
dotCount = sub.split(',').length; | ||
pos = index - dotCount; | ||
if (pos > 0) { | ||
return pos; | ||
} else { | ||
return 0; | ||
} | ||
}, | ||
@@ -92,5 +98,11 @@ focusOnChar: function(val, index) { | ||
componentWillReceiveProps: function(nextProps) { | ||
var formatVal, node, val; | ||
node = this.getDOMNode(); | ||
var formatVal, pos, val; | ||
if (this.props.value === nextProps.value) { | ||
return; | ||
} | ||
val = nextProps.value; | ||
pos = this.state.pos; | ||
if (!re.test(val)) { | ||
formatVal = this.getNumeralValue(val); | ||
} | ||
formatVal = this.getNumeralValue(val); | ||
@@ -100,11 +112,30 @@ return this.setState({ | ||
}, (function(_this) { | ||
return function() {}; | ||
return function() { | ||
var node; | ||
node = _this.getDOMNode(); | ||
return setCaretPosition(node, _this.state.pos); | ||
}; | ||
})(this)); | ||
}, | ||
changeHandler: function() { | ||
var node, val; | ||
var node, oVal, pos, reTest, val; | ||
node = this.getDOMNode(); | ||
pos = getCaretPosition(node); | ||
val = node.value; | ||
pos = this.formatPos(this.state.value, pos); | ||
reTest = re.test(val); | ||
if (!reTest) { | ||
val = numeral(val).value(); | ||
oVal = numeral(this.state.val); | ||
if ((oVal + '').length < (val + '').length) { | ||
pos = this.focusOnChar(val, ++pos); | ||
} else if ((oVal + '').length > (val + '').length) { | ||
pos = this.focusOnChar(val, --pos); | ||
} else { | ||
pos = this.focusOnChar(val, pos); | ||
} | ||
} | ||
val = numeral(val).value(); | ||
return this.setState({ | ||
pos: pos, | ||
value: val | ||
@@ -111,0 +142,0 @@ }, (function(_this) { |
{ | ||
"name": "react-numeral-input", | ||
"description": "numeral input", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"author": "Bingo Yang <blackbing@gmail.com>", | ||
@@ -6,0 +6,0 @@ "browserify": { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
677499
18857