Started working on the blogs
This commit is contained in:
40
frontend/components/editable-text.tsx
Normal file
40
frontend/components/editable-text.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
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>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user