diff --git a/bun.lockb b/bun.lockb index 6c1b1bfc..a36f9c4c 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 28f3746b..2ab13caa 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "react-beautiful-dnd": "^13.1.1", "react-contenteditable": "^3.3.7", "react-dom": "18.2.0", + "react-hook-form": "^7.51.1", "react-icons": "^4.11.0", "react-lottie-player": "^1.5.5", "superjson": "^1.13.1", diff --git a/src/app/boards/[...boardId]/components/NewCardForm.tsx b/src/app/boards/[...boardId]/components/NewCardForm.tsx index 22606d72..6a73b6e3 100644 --- a/src/app/boards/[...boardId]/components/NewCardForm.tsx +++ b/src/app/boards/[...boardId]/components/NewCardForm.tsx @@ -1,21 +1,22 @@ "use client"; -import { useState, useEffect } from "react"; +import { useEffect, useState } from "react"; +import { useForm } from "react-hook-form"; import { api } from "~/trpc/react"; import { HiXMark } from "react-icons/hi2"; import { Switch } from "@headlessui/react"; +import CheckboxDropdown from "~/app/components/CheckboxDropdown"; import { useBoard } from "~/app/providers/board"; import { useModal } from "~/app/providers/modal"; -import { Formik, Form, Field } from "formik"; - -interface FormValues { +interface FormData { title: string; + listPublicId: string; } -interface listPublicId { +interface NewCardFormProps { listPublicId: string; } @@ -23,12 +24,19 @@ function classNames(...classes: string[]): string { return classes.filter(Boolean).join(" "); } -export function NewCardForm({ listPublicId }: listPublicId) { +export function NewCardForm({ listPublicId }: NewCardFormProps) { const utils = api.useUtils(); const { boardData } = useBoard(); const { closeModal } = useModal(); const [isCreateAnotherEnabled, setIsCreateAnotherEnabled] = useState(false); + const { register, handleSubmit, reset, setValue, watch } = useForm({ + defaultValues: { + title: "", + listPublicId, + }, + }); + const refetchBoard = () => utils.board.byId.refetch({ id: boardData.publicId }); @@ -49,6 +57,27 @@ export function NewCardForm({ listPublicId }: listPublicId) { if (titleElement) titleElement.focus(); }, []); + const formattedLists = + boardData?.lists.map((list) => ({ + key: list.publicId, + value: list.name, + selected: list.publicId === watch("listPublicId"), + })) ?? []; + + const onSubmit = (data: FormData) => { + createCard.mutate({ + title: data.title, + listPublicId: data.listPublicId, + }); + reset(); + }; + + const handleSelectList = (listPublicId: string): void => { + setValue("listPublicId", listPublicId); + }; + + const selectedList = formattedLists.find((item) => item.selected); + return ( <>
@@ -61,61 +90,61 @@ export function NewCardForm({ listPublicId }: listPublicId) {
- { - createCard.mutate({ - title: values.title, - listPublicId, - }); - resetForm(); - }} - > -
+ +
- -
- Create more - +
+ handleSelectList(list.key)} + > +
+ {selectedList?.value} +
+
+
+
+ Create more + + Use setting + -
+ /> +
+
-
- -
- - +
+ +
+ ); } diff --git a/src/app/components/CheckboxDropdown.tsx b/src/app/components/CheckboxDropdown.tsx new file mode 100644 index 00000000..baec2857 --- /dev/null +++ b/src/app/components/CheckboxDropdown.tsx @@ -0,0 +1,74 @@ +"use client"; + +import { Fragment } from "react"; +import { Menu, Transition } from "@headlessui/react"; + +interface CheckboxDropdownProps { + children: React.ReactNode; + items: { + key: string; + value: string; + selected: boolean; + }[]; + handleSelect: (item: { key: string }) => void; +} + +export default function CheckboxDropdown({ + children, + items, + handleSelect, +}: CheckboxDropdownProps) { + return ( + <> + + {children} + + + +
+ {items?.map((item) => ( + +
{ + e.preventDefault(); + handleSelect({ key: item.key }); + }} + > + event.stopPropagation()} + onChange={() => handleSelect({ key: item.key })} + checked={item.selected} + /> + +
+
+ ))} +
+
+
+
+ + ); +}