diff --git a/src/components/Renameable.tsx b/src/components/Renameable.tsx new file mode 100644 index 0000000..bda3e38 --- /dev/null +++ b/src/components/Renameable.tsx @@ -0,0 +1,49 @@ +import React from "react" +import { useEffect } from "react" +import { useRef } from "react" +import { useState } from "react" + +interface Props { + text: string + edit: boolean + onRename?: (newName: string)=>void + onCancleRename?: ()=>void +} + +const Renameable: React.FC = (props) => { + const [inputValue,setinputValue] = useState(props.text) + const inputRef = useRef(null) + + useEffect(()=>{ + if (props.edit){ + inputRef.current?.select() + }else{ + setinputValue(props.text) + } + },[props.edit]) + + return ( + <> + {props.edit &&
{ + e.preventDefault() + props.onRename?.(inputValue) + }}> + e.stopPropagation()} + onChange={(e)=>{setinputValue(e.target.value)}} + onBlur={props.onCancleRename} + />
+ + } + {!props.edit && +
{props.text}
+ } + + ) +} + +export default Renameable