From cd476ae82bd152ba9c6408273ce245bc69a38482 Mon Sep 17 00:00:00 2001 From: jbpenrath Date: Wed, 14 Feb 2024 00:41:41 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(demo)=20improve=20create=20form=20sub?= =?UTF-8?q?mit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Take in account more fields. Use a form instead a ref to retrieve values easily. --- apps/demo/src/Create.tsx | 43 ++++++++++++++++++++++++++-------------- apps/demo/src/Home.tsx | 15 ++++++++++++-- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/apps/demo/src/Create.tsx b/apps/demo/src/Create.tsx index 2b4cdd2..9e632bf 100644 --- a/apps/demo/src/Create.tsx +++ b/apps/demo/src/Create.tsx @@ -21,15 +21,22 @@ import { Page, PageProps } from "./App"; export const Create = ({ changePage }: PageProps) => { const { toast } = useToastProvider(); - const inputRef = React.useRef(null); - const submit = () => { + const submit: React.FormEventHandler = (e) => { + e.preventDefault(); + const formData = new FormData(e.target as HTMLFormElement); + const fakeDates = randomDates(); const character: Character = { id: faker.string.uuid(), - name: inputRef.current?.value || "", - sex: "male", - isGuest: false, - ...randomDates(), + name: (formData.get("name") as any) || "", + sex: (formData.get("sex") as any) || "", + isGuest: (formData.get("is_guest") as any) || "", + birthDate: + new Date(formData.get("birthdate") as any) || fakeDates.birthDate, + firstAppearanceDate: + new Date(formData.get("appearance_dates_start") as any) || + fakeDates.firstAppearanceDate, + lastAppearanceDate: fakeDates.lastAppearanceDate, }; database.unshift(character); @@ -38,7 +45,7 @@ export const Create = ({ changePage }: PageProps) => { }; return ( -
+

Add a character

General Information

@@ -46,30 +53,35 @@ export const Create = ({ changePage }: PageProps) => { You are about to add a new character to the collection