我想使用Cleave js作为货币格式。我的Cleave标签看起来像这样:
<Cleave className="input-numeral"
name="loanAmount"
options={{ numeral: true,
numeralDecimalMark: ',',
delimiter: '.'
}}
value={this.props.loanAmount}
onChange={this.props.handleChange}
/>
假设输入是82456.56
我想要这样的输出: 82.456,56。如果我不在代码中使用“value”prop,代码就能正常工作。但是我需要使用value,因为我需要以格式化的方式从props中获得初始值。
在这种情况下,我应该怎么做才能最初从props中读取值,然后根据用户输入更改它?
发布于 2020-01-06 14:23:58
您可能想要尝试这样的操作:
class SampleComponent extends React.Component{
constructor(props){
super(props);
this.onChange = this.onChange.bind(this);
this.state = {
value : props.loanAmount
};
}
onChange(event) {
this.setState({
value: event.target.value
});
this.props.handleChange();
}
render() {
return <Cleave className="input-numeral"
name="loanAmount"
options={{ numeral: true,
numeralDecimalMark: ',',
delimiter: '.'
}}
value={this.state.value}
onChange={this.onChange}
/>
}
}
https://stackoverflow.com/questions/59613678
复制相似问题