From 04c6576228bf76bf796a3d737a8b44ee507b0f02 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 13 Aug 2024 23:13:38 +0100 Subject: [PATCH] fix: ensure correct label colour when creating a new label --- src/views/card/components/NewLabelForm.tsx | 213 +++++++++++---------- 1 file changed, 111 insertions(+), 102 deletions(-) diff --git a/src/views/card/components/NewLabelForm.tsx b/src/views/card/components/NewLabelForm.tsx index 4d836858..1c3586bc 100644 --- a/src/views/card/components/NewLabelForm.tsx +++ b/src/views/card/components/NewLabelForm.tsx @@ -1,16 +1,13 @@ -import { Fragment, useState } from "react"; +import { Fragment } from "react"; import { Listbox, Transition } from "@headlessui/react"; import { api } from "~/utils/api"; - import { HiChevronUpDown, HiXMark } from "react-icons/hi2"; - import { useModal } from "~/providers/modal"; - -import { Formik, Form, Field } from "formik"; +import { useForm, Controller } from "react-hook-form"; interface FormValues { name: string; - colour: Colour | undefined; + colour: Colour; } interface Colour { @@ -18,7 +15,7 @@ interface Colour { code: string; } -interface cardPublicId { +interface CardPublicId { cardPublicId: string; } @@ -33,11 +30,17 @@ const colours = [ { name: "Pink", code: "#db2777" }, ]; -export function NewLabelForm({ cardPublicId }: cardPublicId) { - const [selected, setSelected] = useState(colours[0]); +export function NewLabelForm({ cardPublicId }: CardPublicId) { const utils = api.useUtils(); const { closeModal } = useModal(); + const { control, handleSubmit } = useForm({ + defaultValues: { + name: "", + colour: colours[0], + }, + }); + const refetchCard = () => utils.card.byId.refetch({ id: cardPublicId }); const createLabel = api.label.create.useMutation({ @@ -51,6 +54,16 @@ export function NewLabelForm({ cardPublicId }: cardPublicId) { }, }); + const onSubmit = (values: FormValues) => { + if (!values.colour?.code) return; + + createLabel.mutate({ + name: values.name, + cardPublicId, + colourCode: values.colour.code, + }); + }; + return (
@@ -63,101 +76,97 @@ export function NewLabelForm({ cardPublicId }: cardPublicId) {
- { - if (!values.colour?.code) return; - - createLabel.mutate({ - name: values.name, - cardPublicId, - colourCode: values.colour.code, - }); - }} - > -
- - - - {({ open }) => ( - <> -
- - - - - {selected?.name} + + + ( + + )} + /> + ( + + {({ open }) => ( + <> +
+ + + + + {field.value?.name} + - - - - + + + - - - {colours.map((colour, index) => ( - - {() => ( - <> -
-
- - )} -
- ))} -
-
-
- - )} -
-
- -
- - + + + {colours.map((colour, index) => ( + + {() => ( + <> +
+
+ + )} +
+ ))} +
+
+
+ + )} +
+ )} + /> +
+ +
+
); }