I'm working on a React component to handle IPv4 addresses with a fixed mask format (___ . ___ . ___ . ___), allowing users to edit individual parts of the address. However, I'm encountering some issues with maintaining the mask and ensuring a smooth user experience.
Specifically, I'm facing the following challenges:
I'm seeking advice on how to address these issues and ensure that the mask remains visible while allowing smooth editing of individual parts of the IPv4 address.
Any insights or suggestions would be greatly appreciated! so far i ive tried
import React, { useState, useRef } from 'react'; const isIpv4 = (ip) => { const rgx = /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/; return rgx.test(ip); }; const IpAddressInput = () => { const [ value, setValue ] = useState('___ . ___ . ___ . ___'); const inputRef = useRef(null); const handleFocus = () => { if (value === '___ . ___ . ___ . ___') { setValue(''); } }; const handleBlur = () => { if (!isIpv4(value.replace(/_/g, '0'))) { setValue('___ . ___ . ___ . ___'); } }; const handleChange = (e) => { let newValue = e.target.value.toUpperCase(); newValue = newValue.replace(/[^0-9.]/g, ''); const parts = newValue.split('.').map((part) => part.padEnd(3, '_')).join('.'); setValue(parts.slice(0, 15)); }; return ( <input type="text" value={value} onFocus={handleFocus} onBlur={handleBlur} onChange={handleChange} ref={inputRef} maxLength={15} size={15} style={{ width: 'calc(15ch + 10px)', fontFamily: 'monospace' }} /> ); }; export default IpAddressInput;