Feature/amiriis add units

This commit is contained in:
AmirHossein Mahmoodi
2025-02-23 13:33:21 +00:00
parent 9efc8077da
commit 759bc621d5
13 changed files with 51 additions and 2 deletions

View File

@@ -0,0 +1,49 @@
import { useEffect } from 'react';
function usePersianInput(inputRef, onChange) {
useEffect(() => {
const input = inputRef.current;
if (!input) return;
const handleInput = (event) => {
const originalValue = event.target.value;
const filteredValue = originalValue.replace(/[A-Za-z]/g, '');
if (filteredValue !== originalValue) {
event.target.value = filteredValue;
if (onChange) {
onChange(filteredValue);
}
} else if (onChange) {
onChange(originalValue);
}
};
const handleKeyDown = (event) => {
const controlKeys = [
'Backspace',
'Delete',
'Tab',
'Escape',
'Enter',
'ArrowLeft',
'ArrowRight',
'ArrowUp',
'ArrowDown'
];
if (controlKeys.includes(event.key)) return;
if (/[A-Za-z]/.test(event.key)) {
event.preventDefault();
}
};
input.addEventListener('input', handleInput);
input.addEventListener('keydown', handleKeyDown);
return () => {
input.removeEventListener('input', handleInput);
input.removeEventListener('keydown', handleKeyDown);
};
}, [inputRef, onChange]);
}
export default usePersianInput;