this.state = {
selection: {start: 0, end: 0},
noCents: true,
firstFocus: true,
count: 0
};
setSelection = () => {
if (this.state.firstFocus) {
this.setState({selection: {start: 1, end: 1}});
this.setState({firstFocus: false})
}
};
doJob(amount) {
if (this.state.count === 2 && !this.state.noCents) {
this.setState({
count: 0,
noCent: true
})
}
if (amount.length !== 0 && this.state.noCents) {
this.setState({
selection: {
start: amount.length - 3,
end: amount.length - 3
}
});
} else if (amount.length !== 0 && !this.state.noCents) {
this.setState({count: this.state.count + 1});
if (this.state.count === 1) {
this.setState({
selection: {
start: amount.length - 2,
end: amount.length - 2
},
});
}
if (this.state.count === 2) {
this.setState({
selection: {
start: amount.length - 1,
end: amount.length - 1
},
});
}}
}
<TextInputMask
style={styles.input}
type={'money'}
options={{
precision: 2,
separator: ',',
delimiter: '.',
unit: '',
suffixUnit: '',
zeroCents: this.state.noCents
}}
value={this.props.amount}
onChangeText={amount => this.props.amountChanged({
props: 'amount',
value: amount
},
this.doJob(amount))}
onFocus={this.setSelection}
selection={this.state.selection}
onKeyPress={(e) => (e.nativeEvent.key === ',' ? this.setState({noCents: false}) : null)}
/>
selection: {start: 0, end: 0},
noCents: true,
firstFocus: true,
count: 0
};
setSelection = () => {
if (this.state.firstFocus) {
this.setState({selection: {start: 1, end: 1}});
this.setState({firstFocus: false})
}
};
doJob(amount) {
if (this.state.count === 2 && !this.state.noCents) {
this.setState({
count: 0,
noCent: true
})
}
if (amount.length !== 0 && this.state.noCents) {
this.setState({
selection: {
start: amount.length - 3,
end: amount.length - 3
}
});
} else if (amount.length !== 0 && !this.state.noCents) {
this.setState({count: this.state.count + 1});
if (this.state.count === 1) {
this.setState({
selection: {
start: amount.length - 2,
end: amount.length - 2
},
});
}
if (this.state.count === 2) {
this.setState({
selection: {
start: amount.length - 1,
end: amount.length - 1
},
});
}}
}
<TextInputMask
style={styles.input}
type={'money'}
options={{
precision: 2,
separator: ',',
delimiter: '.',
unit: '',
suffixUnit: '',
zeroCents: this.state.noCents
}}
value={this.props.amount}
onChangeText={amount => this.props.amountChanged({
props: 'amount',
value: amount
},
this.doJob(amount))}
onFocus={this.setSelection}
selection={this.state.selection}
onKeyPress={(e) => (e.nativeEvent.key === ',' ? this.setState({noCents: false}) : null)}
/>