react-numeral-input
Advanced tools
Comparing version 0.0.8 to 0.0.9
// Generated by CoffeeScript 1.9.3 | ||
/** @jsx React.DOM */; | ||
var NumeralInput, React, numeral, re; | ||
var NumeralInput, React, getCaretPosition, numeral, re, setCaretPosition; | ||
@@ -11,2 +11,29 @@ React = require('react'); | ||
getCaretPosition = function(oField) { | ||
var iCaretPos, oSel; | ||
iCaretPos = 0; | ||
if (document.selection) { | ||
oField.focus(); | ||
oSel = document.selection.createRange(); | ||
oSel.moveStart('character', -oField.value.length); | ||
iCaretPos = oSel.text.length; | ||
} else if (oField.selectionStart || oField.selectionStart === '0') { | ||
iCaretPos = oField.selectionStart; | ||
} | ||
return iCaretPos; | ||
}; | ||
setCaretPosition = function(oField, index) { | ||
var range; | ||
if (oField.setSelectionRange) { | ||
return oField.setSelectionRange(index, index); | ||
} else { | ||
range = oField.createTextRange(); | ||
range.collapse(true); | ||
range.moveEnd('character', index); | ||
range.moveStart('character', index); | ||
return range.select(); | ||
} | ||
}; | ||
NumeralInput = React.createClass({ | ||
@@ -23,2 +50,21 @@ displayName: 'NumeralInput', | ||
}, | ||
focusOnChar: function(val, index) { | ||
var char, dotCount, finalIndex, formatVal, i; | ||
formatVal = numeral(val).format(this.props.fmt); | ||
dotCount = 0; | ||
i = 0; | ||
finalIndex = formatVal.length; | ||
while (i < formatVal.length) { | ||
char = formatVal[i]; | ||
if (char === ',') { | ||
dotCount++; | ||
} | ||
if (i === (index + dotCount)) { | ||
finalIndex = i; | ||
break; | ||
} | ||
i++; | ||
} | ||
return finalIndex; | ||
}, | ||
getInitialState: function() { | ||
@@ -35,19 +81,32 @@ return { | ||
componentWillReceiveProps: function(nextProps) { | ||
var val; | ||
var dot_sp, formatVal, node, val; | ||
node = this.getDOMNode(); | ||
val = nextProps.value; | ||
if (!re.test(val)) { | ||
val = this.getNumeralValue(val); | ||
formatVal = this.getNumeralValue(val); | ||
dot_sp = formatVal.split(','); | ||
} | ||
return this.setState({ | ||
value: val | ||
}); | ||
value: formatVal | ||
}, (function(_this) { | ||
return function() { | ||
return setCaretPosition(node, _this.state.pos); | ||
}; | ||
})(this)); | ||
}, | ||
changeHandler: function() { | ||
var reTest, val; | ||
val = this.getDOMNode().value; | ||
var node, pos, reTest, val; | ||
node = this.getDOMNode(); | ||
val = node.value; | ||
pos = getCaretPosition(node); | ||
reTest = re.test(val); | ||
if (!reTest) { | ||
val = numeral(val).value(); | ||
if ((this.state.value + '').length <= (val + '').length) { | ||
pos = this.focusOnChar(val, pos); | ||
pos++; | ||
} | ||
} | ||
return this.setState({ | ||
pos: pos, | ||
value: val | ||
@@ -54,0 +113,0 @@ }, (function(_this) { |
{ | ||
"name": "react-numeral-input", | ||
"description": "numeral input", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"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
674693
18773