update remove function for phone input
This commit is contained in:
Generated
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "no-copy-frontend",
|
"name": "no-copy-frontend",
|
||||||
"version": "0.2.0",
|
"version": "0.7.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "no-copy-frontend",
|
"name": "no-copy-frontend",
|
||||||
"version": "0.2.0",
|
"version": "0.7.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tailwindcss/postcss": "^4.1.17",
|
"@tailwindcss/postcss": "^4.1.17",
|
||||||
"@tanstack/match-sorter-utils": "^8.19.4",
|
"@tanstack/match-sorter-utils": "^8.19.4",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useState, ChangeEvent } from 'react';
|
import { useState, ChangeEvent, useEffect, useRef, KeyboardEvent } from 'react';
|
||||||
import styles from '@/app/styles/login.module.scss';
|
import styles from '@/app/styles/login.module.scss';
|
||||||
|
|
||||||
interface PhoneInputProps {
|
interface PhoneInputProps {
|
||||||
@@ -10,6 +10,7 @@ interface PhoneInputProps {
|
|||||||
|
|
||||||
export default function PhoneInput({ phoneState, validateHandler }: PhoneInputProps) {
|
export default function PhoneInput({ phoneState, validateHandler }: PhoneInputProps) {
|
||||||
const [phone, setPhone] = useState(phoneState ? phoneState : '');
|
const [phone, setPhone] = useState(phoneState ? phoneState : '');
|
||||||
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
const formatPhoneNumber = (value: string, isBackspace: boolean): string => {
|
const formatPhoneNumber = (value: string, isBackspace: boolean): string => {
|
||||||
if (value === '+') {
|
if (value === '+') {
|
||||||
@@ -60,17 +61,43 @@ export default function PhoneInput({ phoneState, validateHandler }: PhoneInputPr
|
|||||||
|
|
||||||
const handleChange = (e: ChangeEvent<HTMLInputElement>): void => {
|
const handleChange = (e: ChangeEvent<HTMLInputElement>): void => {
|
||||||
const input = e.target.value;
|
const input = e.target.value;
|
||||||
//@ts-ignore
|
const nativeEvent = e.nativeEvent as InputEvent;
|
||||||
const isBackspace = e.nativeEvent.inputType === 'deleteContentBackward';
|
const isBackspace = nativeEvent.inputType === 'deleteContentBackward';
|
||||||
const formatted = formatPhoneNumber(input, isBackspace);
|
const formatted = formatPhoneNumber(input, isBackspace);
|
||||||
setPhone(formatted);
|
setPhone(formatted);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const range = useRef<{
|
||||||
|
start: number | null,
|
||||||
|
key: string;
|
||||||
|
} | null>(null);
|
||||||
|
|
||||||
|
const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>): void => {
|
||||||
|
const input = e.currentTarget;
|
||||||
|
range.current = {
|
||||||
|
start: input.selectionStart,
|
||||||
|
key: e.key
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
console.log('use');
|
||||||
|
if (range.current?.key === 'Backspace' && inputRef.current) {
|
||||||
|
const startPos = range.current.start ?? 0;
|
||||||
|
const newPosition = Math.max(0, startPos - 1);
|
||||||
|
inputRef.current.setSelectionRange(newPosition, newPosition);
|
||||||
|
} else if (range.current?.key === 'Delete' && inputRef.current) {
|
||||||
|
inputRef.current.setSelectionRange(range.current.start, range.current.start);
|
||||||
|
}
|
||||||
|
|
||||||
|
}, [phone]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<input
|
<input
|
||||||
type="phone"
|
type="phone"
|
||||||
id="phone"
|
id="phone"
|
||||||
name="phone"
|
name="phone"
|
||||||
|
ref={inputRef}
|
||||||
className={`${styles['form-input']}`}
|
className={`${styles['form-input']}`}
|
||||||
placeholder="+7 (999) 123-45-67"
|
placeholder="+7 (999) 123-45-67"
|
||||||
value={phone}
|
value={phone}
|
||||||
@@ -78,6 +105,9 @@ export default function PhoneInput({ phoneState, validateHandler }: PhoneInputPr
|
|||||||
handleChange(e);
|
handleChange(e);
|
||||||
validateHandler(e);
|
validateHandler(e);
|
||||||
}}
|
}}
|
||||||
|
onKeyDown={(e) => {
|
||||||
|
handleKeyDown(e);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user