From 13543838601de74c1750ee4bbb8029ca6d08ff2d Mon Sep 17 00:00:00 2001 From: Niklas Date: Sun, 29 Aug 2021 19:59:41 +0200 Subject: [PATCH] missed one --- src/components/Renameable.tsx | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/components/Renameable.tsx 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