41 lines
844 B
TypeScript
41 lines
844 B
TypeScript
import { useState, ReactNode, cloneElement } from "react";
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
interface EditableTextProps {
|
|
children: ReactNode;
|
|
onChange: (newText: string) => void;
|
|
}
|
|
|
|
export default function EditableText({
|
|
children,
|
|
onChange,
|
|
}: EditableTextProps) {
|
|
const [isEditing, setIsEditing] = useState(false);
|
|
const child = Array.isArray(children) ? children[0] : children;
|
|
|
|
const text = child?.props.children || "";
|
|
|
|
const handleBlur = () => {
|
|
setIsEditing(false);
|
|
};
|
|
|
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
onChange(e.target.value);
|
|
};
|
|
|
|
return (
|
|
<div onClick={() => setIsEditing(true)}>
|
|
{isEditing ? (
|
|
<Input
|
|
autoFocus
|
|
value={text}
|
|
onChange={handleChange}
|
|
onBlur={handleBlur}
|
|
/>
|
|
) : (
|
|
cloneElement(child, {}, text)
|
|
)}
|
|
</div>
|
|
);
|
|
}
|