I made an input that show percentage like 1.1% (and not accepting 00 at the beginning) when typing but the cursor goes to the end while typing.
is there a away to keep it before % symbol
function formatPercent(amount) {
return amount + '%'
}
const [lendData, setLendData] = useState({
lendValue: '',
lendFee: '',
})
function handleChange(e) {
let { name, value } = e.target
if (name == 'lendValue') {
// prevent first character if it 0 and removing separator
value = value.replace(/^0+|[^0-9]/g, '')
setLendData((prev) => {
return {
...prev,
[name]: value,
}
})
} else if (name == 'lendFee') {
// prevent first character if it 0
value = value.replace(/^0+|[^0-9,.]/g, '')
console.log(value)
setLendData((prev) => {
return {
...prev,
[name]: value,
}
})
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<input
type='text'
inputMode='decimal'
name='lendFee'
id='lendFee'
placeholder='0%'
value={
lendData.lendFee ? formatPercent(lendData.lendFee) : ''
}
onChange={handleChange}
/>
2
Answers
You can try the below code:
Explanation of the code changes:
useRef
hook and created a ref for the input elementlendFeeInput
.const cursorPosition = e.target.selectionStart;
setTimeout
to ensure the input value has been updated.CODE DEMO
What about seeing it in a different perspective?
You’re trying to put
%
sign inside the string of numbers.I think making one more element for the percent sign and putting it next to each other would do the trick. Use flex to style it then.
Also, it would be helpful if you want to do something with the number value; for instance, you don’t need to do extra slicing.