|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { Input } from '@comp/ui/input'; |
| 4 | +import { type InputHTMLAttributes, forwardRef, useCallback, useEffect, useState } from 'react'; |
| 5 | + |
| 6 | +interface WebsiteInputProps extends Omit<InputHTMLAttributes<HTMLInputElement>, 'type' | 'prefix'> { |
| 7 | + onValueChange?: (value: string) => void; |
| 8 | +} |
| 9 | + |
| 10 | +export const WebsiteInput = forwardRef<HTMLInputElement, WebsiteInputProps>( |
| 11 | + ({ value: propValue, onChange, onValueChange, onBlur, ...props }, ref) => { |
| 12 | + // Use local state for the display value |
| 13 | + const [displayValue, setDisplayValue] = useState(''); |
| 14 | + |
| 15 | + // Update display value when prop value changes |
| 16 | + useEffect(() => { |
| 17 | + if (typeof propValue === 'string') { |
| 18 | + setDisplayValue(propValue.replace(/^https?:\/\//, '').replace(/^www\./, '')); |
| 19 | + } |
| 20 | + }, [propValue]); |
| 21 | + |
| 22 | + const handleChange = useCallback( |
| 23 | + (e: React.ChangeEvent<HTMLInputElement>) => { |
| 24 | + let inputValue = e.target.value; |
| 25 | + |
| 26 | + // Clean up the input |
| 27 | + inputValue = inputValue.trim(); |
| 28 | + |
| 29 | + // Remove any protocol if pasted |
| 30 | + inputValue = inputValue.replace(/^https?:\/\//, ''); |
| 31 | + inputValue = inputValue.replace(/^ftp:\/\//, ''); |
| 32 | + inputValue = inputValue.replace(/^\/\//, ''); |
| 33 | + |
| 34 | + // Clean up multiple slashes (except for the protocol) |
| 35 | + inputValue = inputValue.replace(/([^:]\/)\/+/g, '$1'); |
| 36 | + |
| 37 | + // Update display value |
| 38 | + setDisplayValue(inputValue); |
| 39 | + |
| 40 | + // If empty, pass empty value |
| 41 | + if (!inputValue) { |
| 42 | + onChange?.({ |
| 43 | + ...e, |
| 44 | + target: { ...e.target, value: '', name: e.target.name }, |
| 45 | + } as React.ChangeEvent<HTMLInputElement>); |
| 46 | + onValueChange?.(''); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // Format the value with https:// |
| 51 | + const finalValue = `https://${inputValue}`; |
| 52 | + |
| 53 | + // Create a synthetic event with the formatted value |
| 54 | + const syntheticEvent = { |
| 55 | + ...e, |
| 56 | + target: { |
| 57 | + ...e.target, |
| 58 | + value: finalValue, |
| 59 | + name: e.target.name, |
| 60 | + }, |
| 61 | + } as React.ChangeEvent<HTMLInputElement>; |
| 62 | + |
| 63 | + // Call the original onChange if provided |
| 64 | + onChange?.(syntheticEvent); |
| 65 | + |
| 66 | + // Also call onValueChange if provided |
| 67 | + onValueChange?.(finalValue); |
| 68 | + }, |
| 69 | + [onChange, onValueChange], |
| 70 | + ); |
| 71 | + |
| 72 | + const handleBlur = useCallback( |
| 73 | + (e: React.FocusEvent<HTMLInputElement>) => { |
| 74 | + // On blur, ensure the value has a proper format |
| 75 | + if (displayValue && !displayValue.includes('.')) { |
| 76 | + // If there's no dot, add .com as a helpful default |
| 77 | + const finalValue = `https://${displayValue}.com`; |
| 78 | + const syntheticEvent = { |
| 79 | + ...e, |
| 80 | + target: { |
| 81 | + ...e.target, |
| 82 | + value: finalValue, |
| 83 | + name: e.target.name, |
| 84 | + }, |
| 85 | + } as React.FocusEvent<HTMLInputElement>; |
| 86 | + |
| 87 | + setDisplayValue(`${displayValue}.com`); |
| 88 | + onChange?.({ ...syntheticEvent, type: 'change' } as React.ChangeEvent<HTMLInputElement>); |
| 89 | + onValueChange?.(finalValue); |
| 90 | + } |
| 91 | + |
| 92 | + // Call original onBlur |
| 93 | + onBlur?.(e); |
| 94 | + }, |
| 95 | + [displayValue, onChange, onValueChange, onBlur], |
| 96 | + ); |
| 97 | + |
| 98 | + return ( |
| 99 | + <Input |
| 100 | + ref={ref} |
| 101 | + type="text" |
| 102 | + value={displayValue} |
| 103 | + onChange={handleChange} |
| 104 | + onBlur={handleBlur} |
| 105 | + prefix="https://" |
| 106 | + autoComplete="url" |
| 107 | + spellCheck={false} |
| 108 | + {...props} |
| 109 | + /> |
| 110 | + ); |
| 111 | + }, |
| 112 | +); |
| 113 | + |
| 114 | +WebsiteInput.displayName = 'WebsiteInput'; |
0 commit comments