(demo) improve create form submit

Take in account more fields. Use a form instead a ref to retrieve values
 easily.
This commit is contained in:
jbpenrath
2024-02-14 00:41:41 +01:00
committed by Jean-Baptiste PENRATH
parent 7320671589
commit cd476ae82b
2 changed files with 41 additions and 17 deletions

View File

@@ -21,15 +21,22 @@ import { Page, PageProps } from "./App";
export const Create = ({ changePage }: PageProps) => { export const Create = ({ changePage }: PageProps) => {
const { toast } = useToastProvider(); const { toast } = useToastProvider();
const inputRef = React.useRef<HTMLInputElement>(null);
const submit = () => { const submit: React.FormEventHandler<HTMLFormElement> = (e) => {
e.preventDefault();
const formData = new FormData(e.target as HTMLFormElement);
const fakeDates = randomDates();
const character: Character = { const character: Character = {
id: faker.string.uuid(), id: faker.string.uuid(),
name: inputRef.current?.value || "", name: (formData.get("name") as any) || "",
sex: "male", sex: (formData.get("sex") as any) || "",
isGuest: false, isGuest: (formData.get("is_guest") as any) || "",
...randomDates(), 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); database.unshift(character);
@@ -38,7 +45,7 @@ export const Create = ({ changePage }: PageProps) => {
}; };
return ( return (
<div className="page__create clr-greyscale-900"> <form className="page__create clr-greyscale-900" onSubmit={submit}>
<h1>Add a character</h1> <h1>Add a character</h1>
<div className="card"> <div className="card">
<h3 className="fw-bold fs-h3">General Information</h3> <h3 className="fw-bold fs-h3">General Information</h3>
@@ -46,30 +53,35 @@ export const Create = ({ changePage }: PageProps) => {
You are about to add a new character to the collection You are about to add a new character to the collection
</Alert> </Alert>
<Input <Input
ref={inputRef} name="name"
label="Name" label="Name"
text="Enter first and last name" text="Enter first and last name"
fullWidth={true} fullWidth={true}
required
/> />
<Select <Select
name="sex"
label="Sex" label="Sex"
fullWidth={true} fullWidth={true}
options={[ options={[
{ {
label: "Male", label: "Female",
value: "female",
}, },
{ {
label: "Female", label: "Male",
value: "male",
}, },
]} ]}
/> />
<DatePicker label="Birth Date" fullWidth={true} /> <DatePicker name="birthdate" label="Birth Date" fullWidth={true} />
<DateRangePicker <DateRangePicker
name="appearance_dates"
startLabel="First appearance" startLabel="First appearance"
endLabel="Last appearance" endLabel="Last appearance"
fullWidth={true} fullWidth={true}
/> />
<Checkbox label="This character is a guest" /> <Checkbox name="is_guest" label="This character is a guest" />
</div> </div>
<div className="card mt-l"> <div className="card mt-l">
<h3 className="fw-bold fs-h3">Bio</h3> <h3 className="fw-bold fs-h3">Bio</h3>
@@ -77,6 +89,7 @@ export const Create = ({ changePage }: PageProps) => {
Please be exhaustive, every detail counts! Please be exhaustive, every detail counts!
</Alert> </Alert>
<TextArea <TextArea
name="bio"
label="Biography" label="Biography"
text="Enter a detailed biography" text="Enter a detailed biography"
fullWidth={true} fullWidth={true}
@@ -104,7 +117,7 @@ export const Create = ({ changePage }: PageProps) => {
value="D" value="D"
/> />
</RadioGroup> </RadioGroup>
<Switch label="Make this character public" /> <Switch name="is_public" label="Make this character public" />
<div> <div>
<h4>Add pictures</h4> <h4>Add pictures</h4>
@@ -119,10 +132,10 @@ export const Create = ({ changePage }: PageProps) => {
</div> </div>
</div> </div>
<div className="mt-l"> <div className="mt-l">
<Button fullWidth={true} onClick={() => submit()}> <Button fullWidth={true} type="submit">
Add character Add character
</Button> </Button>
</div> </div>
</div> </form>
); );
}; };

View File

@@ -83,8 +83,13 @@ export const Home = ({ changePage }: PageProps) => {
headerName: "Name", headerName: "Name",
}, },
{ {
field: "sex", id: "sex",
headerName: "Sex", headerName: "Sex",
renderCell: (params) => {
return (
<span className="material-icons">{params.row.sex}</span>
);
},
}, },
{ {
id: "birthDate", id: "birthDate",
@@ -104,7 +109,13 @@ export const Home = ({ changePage }: PageProps) => {
id: "isGuest", id: "isGuest",
headerName: "Is Guest", headerName: "Is Guest",
renderCell: (params) => { renderCell: (params) => {
return params.row.isGuest ? "yes" : "no"; return params.row.isGuest ? (
<span className="material-icons ml-s">check_box</span>
) : (
<span className="material-icons ml-s">
check_box_outline_blank
</span>
);
}, },
}, },
{ {