-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
346 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { Container, Stack, TextField, Typography } from '@mui/material'; | ||
import { useForm } from 'react-hook-form'; | ||
import { formSchema } from '@/_schemas/formSchema'; | ||
import type { Form} from '@/_schemas/formSchema'; | ||
|
||
export const CreateFormComponent = () => { | ||
const { | ||
control, | ||
handleSubmit, | ||
register, | ||
formState: { errors }, | ||
} = useForm<Form>({ | ||
mode: 'onSubmit', | ||
reValidateMode: 'onBlur', | ||
resolver: zodResolver(formSchema), | ||
}); | ||
|
||
const onSubmit = (data: Form) => { | ||
// todo: データの送信処理を書く | ||
}; | ||
|
||
return ( | ||
<Container component="form" onSubmit={handleSubmit(onSubmit)}> | ||
<Stack spacing={2}> | ||
<Typography variant="body1">フォームタイトル</Typography> | ||
<TextField {...register('title')} required /> | ||
<TextField | ||
{...register('description')} | ||
label="フォーム説明" | ||
variant="outlined" | ||
/> | ||
</Stack> | ||
</Container> | ||
); | ||
}; |
8 changes: 8 additions & 0 deletions
8
src/app/(authed)/admin/forms/create/_react-hook-form-components/RHFProps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { FieldValues, UseControllerProps } from 'react-hook-form'; | ||
|
||
export type RHFProps<T extends FieldValues = FieldValues> = Pick< | ||
UseControllerProps<T>, | ||
'name' | 'control' | ||
> & { | ||
label: string; | ||
}; |
35 changes: 35 additions & 0 deletions
35
src/app/(authed)/admin/forms/create/_react-hook-form-components/RHFTextField.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Stack, TextField, Typography } from '@mui/material'; | ||
import { useController } from 'react-hook-form'; | ||
import type { RHFProps } from './RHFProps'; | ||
import type { FieldValues} from 'react-hook-form'; | ||
|
||
export const RHFTextField = <T extends FieldValues>({ | ||
name, | ||
control, | ||
label, | ||
}: RHFProps<T>) => { | ||
const { | ||
field, | ||
formState: { errors }, | ||
} = useController({ name, control }); | ||
|
||
const errorMessage = errors?.[name]?.message as string; | ||
|
||
return ( | ||
<Stack direction="row" alignItems="center" m={2}> | ||
<Typography>{label}</Typography> | ||
<TextField | ||
value={field.value ?? ''} | ||
inputRef={field.ref} | ||
name={field.name} | ||
onChange={field.onChange} | ||
onBlur={field.onBlur} | ||
/> | ||
{errorMessage && ( | ||
<Typography ml={3} color="red"> | ||
{errorMessage} | ||
</Typography> | ||
)} | ||
</Stack> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.