<Input
disabled={this.state.disabled}
value={this.state.minRateAmountInput}
inputStyle={styles.input}
leftIcon={() => <Iconfont size={20} style={{paddingLeft: 10}} unicode={`\ue611`}
color={Colors.successColor}/>}
renderErrorMessage={false}
containerStyle={styles.inputContainerStyle}
keyboardType='decimal-pad'
onEndEditing={this.handleConcreteMinRateChange}
onChangeText={(value) => {
if (!value.endsWith(".")) {
this.setState({
minRateAmountInput: this.formatNumberThree(value.replaceAll(",", ""))
});
} else {
this.setState({
minRateAmountInput: value
});
}
}}/>
其中的formatter函数:
formatNumberThree(strNum): number {
if (!strNum) {
return strNum;
}
if (strNum.length <= 3) {
return strNum;
}
if (!/^(\+|-)?(\d+)(\.\d+)?$/.test(strNum)) {
return strNum;
}
var a = RegExp.$1,
b = RegExp.$2,
c = RegExp.$3;
var re = new RegExp();
re.compile("(\\d)(\\d{3})(,|$)");
while (re.test(b)) {
b = b.replace(re, "$1,$2$3");
}
return a + "" + b + "" + c;
}
在需要使用state中保存的input值只需要
this.state.minRateAmountInput.replaceAll(",", "")
即可
android平台对replaceAll支持可能会存在问题,可以使用replace的全局替换,即
this.state.minRateAmountInput.replace(/,/g, "")