From a9bac691572288817d8e1ba35e0cfdac822c782a Mon Sep 17 00:00:00 2001 From: marshennikovaolga Date: Wed, 11 Sep 2024 18:48:31 +0300 Subject: [PATCH] task input --- web/components/routes/task/TaskForm.tsx | 72 +++++++++++++++++++++---- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/web/components/routes/task/TaskForm.tsx b/web/components/routes/task/TaskForm.tsx index 567df5e8..8d1d15ad 100644 --- a/web/components/routes/task/TaskForm.tsx +++ b/web/components/routes/task/TaskForm.tsx @@ -1,8 +1,9 @@ "use client" - -import React, { useState } from "react" +import { useState, useEffect, useRef } from "react" +import { motion, AnimatePresence } from "framer-motion" import { Task } from "@/lib/schema/tasks" import { Input } from "@/components/ui/input" +import { Button } from "@/components/ui/button" import { useAccount } from "@/lib/providers/jazz-provider" import { LaIcon } from "@/components/custom/la-icon" @@ -12,7 +13,9 @@ interface TaskFormProps { export const TaskForm: React.FC = ({ onAddTask }) => { const [title, setTitle] = useState("") + const [inputVisible, setInputVisible] = useState(false) const { me } = useAccount() + const inputRef = useRef(null) const handleSubmit = (e: React.FormEvent) => { e.preventDefault() @@ -28,16 +31,67 @@ export const TaskForm: React.FC = ({ onAddTask }) => { { owner: me._owner } ) onAddTask(newTask) - setTitle("") + resetForm() } } + const resetForm = () => { + setTitle("") + setInputVisible(false) + } + + const handleKeyDown = (e: React.KeyboardEvent) => { + if (e.key === "Escape") { + resetForm() + } + } + + useEffect(() => { + if (inputVisible && inputRef.current) { + inputRef.current.focus() + } + }, [inputVisible]) + return ( -
- setTitle(e.target.value)} placeholder="Add new task" /> - -
+
+ + {!inputVisible ? ( + + + + ) : ( + + setTitle(e.target.value)} + onKeyDown={handleKeyDown} + placeholder="Enter task title" + /> + + + )} + +
) }