|
| 1 | +import React, { useState, useRef, useEffect, useCallback } from 'react'; |
| 2 | +import { toast } from 'react-toastify'; |
| 3 | +import { errorMessage } from '../../utils/conversionUtils'; |
| 4 | + |
| 5 | +interface InlineEditTextProps { |
| 6 | + value: string; |
| 7 | + onSave: (newValue: string) => Promise<void>; |
| 8 | + maxLength?: number; |
| 9 | + placeholder?: string; |
| 10 | + disabled?: boolean; |
| 11 | + className?: string; |
| 12 | + editClassName?: string; |
| 13 | + onEditStart?: () => void; |
| 14 | + onEditEnd?: () => void; |
| 15 | + allowEmpty?: boolean; |
| 16 | + singleClickEdit?: boolean; |
| 17 | +} |
| 18 | + |
| 19 | +export const InlineEditText: React.FC<InlineEditTextProps> = ({ |
| 20 | + value, |
| 21 | + onSave, |
| 22 | + maxLength = 200, |
| 23 | + placeholder = 'Enter text', |
| 24 | + disabled = false, |
| 25 | + className = '', |
| 26 | + editClassName = '', |
| 27 | + onEditStart, |
| 28 | + onEditEnd, |
| 29 | + allowEmpty = false, |
| 30 | + singleClickEdit = true, |
| 31 | +}) => { |
| 32 | + const [isEditing, setIsEditing] = useState(false); |
| 33 | + const [editValue, setEditValue] = useState(value); |
| 34 | + const [isSaving, setIsSaving] = useState(false); |
| 35 | + const inputRef = useRef<HTMLInputElement>(null); |
| 36 | + const originalValue = useRef(value); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + if (!isEditing) { |
| 40 | + setEditValue(value); |
| 41 | + originalValue.current = value; |
| 42 | + } |
| 43 | + }, [value, isEditing]); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + if (isEditing && inputRef.current) { |
| 47 | + inputRef.current.focus(); |
| 48 | + inputRef.current.select(); |
| 49 | + } |
| 50 | + }, [isEditing]); |
| 51 | + |
| 52 | + const handleStartEdit = useCallback(() => { |
| 53 | + if (disabled || isSaving) return; |
| 54 | + setIsEditing(true); |
| 55 | + setEditValue(value); |
| 56 | + onEditStart?.(); |
| 57 | + }, [disabled, isSaving, value, onEditStart]); |
| 58 | + |
| 59 | + const handleCancel = useCallback(() => { |
| 60 | + setIsEditing(false); |
| 61 | + setEditValue(originalValue.current); |
| 62 | + onEditEnd?.(); |
| 63 | + }, [onEditEnd]); |
| 64 | + |
| 65 | + const handleSave = useCallback(async () => { |
| 66 | + if (isSaving) return; |
| 67 | + |
| 68 | + const trimmedValue = editValue.trim(); |
| 69 | + |
| 70 | + // Check if value unchanged |
| 71 | + if (trimmedValue === originalValue.current) { |
| 72 | + handleCancel(); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // Check if empty when not allowed |
| 77 | + if (!allowEmpty && !trimmedValue) { |
| 78 | + handleCancel(); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + setIsSaving(true); |
| 83 | + try { |
| 84 | + await onSave(trimmedValue); |
| 85 | + originalValue.current = trimmedValue; |
| 86 | + setIsEditing(false); |
| 87 | + onEditEnd?.(); |
| 88 | + } catch (error) { |
| 89 | + const errMsg = errorMessage(error, 'Failed to save'); |
| 90 | + console.error('InlineEditText save error:', errMsg); |
| 91 | + toast.error(errMsg); |
| 92 | + setEditValue(originalValue.current); |
| 93 | + handleCancel(); |
| 94 | + } finally { |
| 95 | + setIsSaving(false); |
| 96 | + } |
| 97 | + }, [editValue, isSaving, allowEmpty, onSave, handleCancel, onEditEnd]); |
| 98 | + |
| 99 | + const handleKeyDown = useCallback( |
| 100 | + (e: React.KeyboardEvent<HTMLInputElement>) => { |
| 101 | + if (e.key === 'Enter' && !isSaving) { |
| 102 | + e.preventDefault(); |
| 103 | + handleSave(); |
| 104 | + } else if (e.key === 'Escape' && !isSaving) { |
| 105 | + e.preventDefault(); |
| 106 | + handleCancel(); |
| 107 | + } |
| 108 | + }, |
| 109 | + [handleSave, handleCancel, isSaving] |
| 110 | + ); |
| 111 | + |
| 112 | + const handleBlur = useCallback(() => { |
| 113 | + if (!isSaving) { |
| 114 | + handleSave(); |
| 115 | + } |
| 116 | + }, [handleSave, isSaving]); |
| 117 | + |
| 118 | + const handleChange = useCallback( |
| 119 | + (e: React.ChangeEvent<HTMLInputElement>) => { |
| 120 | + setEditValue(e.target.value); |
| 121 | + }, |
| 122 | + [] |
| 123 | + ); |
| 124 | + |
| 125 | + const handleClick = useCallback( |
| 126 | + (e: React.MouseEvent) => { |
| 127 | + if (singleClickEdit) { |
| 128 | + e.stopPropagation(); |
| 129 | + handleStartEdit(); |
| 130 | + } |
| 131 | + }, |
| 132 | + [singleClickEdit, handleStartEdit] |
| 133 | + ); |
| 134 | + |
| 135 | + const handleDoubleClick = useCallback( |
| 136 | + (e: React.MouseEvent) => { |
| 137 | + if (!singleClickEdit) { |
| 138 | + e.stopPropagation(); |
| 139 | + handleStartEdit(); |
| 140 | + } |
| 141 | + }, |
| 142 | + [singleClickEdit, handleStartEdit] |
| 143 | + ); |
| 144 | + |
| 145 | + if (isEditing) { |
| 146 | + return ( |
| 147 | + <input |
| 148 | + ref={inputRef} |
| 149 | + type="text" |
| 150 | + value={editValue} |
| 151 | + onChange={handleChange} |
| 152 | + onKeyDown={handleKeyDown} |
| 153 | + onBlur={handleBlur} |
| 154 | + maxLength={maxLength} |
| 155 | + placeholder={placeholder} |
| 156 | + disabled={isSaving} |
| 157 | + className={` |
| 158 | + w-full px-2 py-1 border rounded |
| 159 | + bg-background-default text-text-standard |
| 160 | + border-blue-500 ring-2 ring-blue-500/20 |
| 161 | + focus:outline-none focus:ring-2 focus:ring-blue-500/40 |
| 162 | + disabled:opacity-50 disabled:cursor-not-allowed |
| 163 | + ${editClassName} |
| 164 | + `} |
| 165 | + onClick={(e) => e.stopPropagation()} |
| 166 | + /> |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + return ( |
| 171 | + <div |
| 172 | + className={` |
| 173 | + cursor-pointer px-2 py-1 rounded |
| 174 | + hover:bg-background-hover |
| 175 | + transition-colors |
| 176 | + ${disabled ? 'opacity-50 cursor-not-allowed' : ''} |
| 177 | + ${className} |
| 178 | + `} |
| 179 | + onClick={handleClick} |
| 180 | + onDoubleClick={handleDoubleClick} |
| 181 | + title={disabled ? '' : singleClickEdit ? 'Click to edit' : 'Double-click to edit'} |
| 182 | + > |
| 183 | + {value || <span className="text-text-subtle italic">{placeholder}</span>} |
| 184 | + </div> |
| 185 | + ); |
| 186 | +}; |
0 commit comments