Started working on the blogs

This commit is contained in:
cdricms
2025-02-21 17:49:42 +01:00
parent de828d4c13
commit 7a97961fef
9 changed files with 178 additions and 79 deletions

View 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>
);
}