12 lines
434 B
JavaScript
12 lines
434 B
JavaScript
export function handleDecimalInputKeyDown(e) {
|
|
const allowedKeys = ['Backspace', 'Tab', 'Delete', 'ArrowLeft', 'ArrowRight'];
|
|
const isDigit = /^[0-9]$/.test(e.key);
|
|
const isDot = e.key === '.';
|
|
|
|
const currentValue = e.target.value ?? '';
|
|
const alreadyHasDot = currentValue.includes('.');
|
|
|
|
if (allowedKeys.includes(e.key)) return;
|
|
if (isDot && !alreadyHasDot) return;
|
|
if (!isDigit) e.preventDefault();
|
|
} |