You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.7KB

  1. import { yupResolver } from "@hookform/resolvers/yup";
  2. import { Box, Button, Divider, Stack, Typography } from "@mui/material";
  3. import { HasChildren } from "@types";
  4. import { FormProvider, RHFTextField } from "components/hook-form";
  5. import { useForm } from "react-hook-form";
  6. import * as Yup from "yup";
  7. type AreaBoxProps = {
  8. title: string;
  9. } & HasChildren;
  10. function AreaBox({ title, children }: AreaBoxProps) {
  11. return (
  12. <Box sx={{ maxWidth: 500 }}>
  13. <Typography variant="subtitle1">{title}</Typography>
  14. {children}
  15. </Box>
  16. );
  17. }
  18. type FormProps = {
  19. email: string;
  20. };
  21. type Props = {
  22. onNext?: VoidFunction;
  23. onPrev?: VoidFunction;
  24. };
  25. export default function useInputEmailStep({ onNext, onPrev }: Props) {
  26. const form = useForm<FormProps>({
  27. defaultValues: {
  28. email: "",
  29. },
  30. resolver: yupResolver(
  31. Yup.object().shape({
  32. email: Yup.string().required("必須項目です"),
  33. })
  34. ),
  35. });
  36. const handleSubmit = () => {
  37. if (onNext) {
  38. onNext();
  39. }
  40. };
  41. const handlePrev = () => {
  42. if (onPrev) {
  43. onPrev();
  44. }
  45. };
  46. const element = (
  47. <FormProvider methods={form} onSubmit={form.handleSubmit(handleSubmit)}>
  48. <Stack spacing={2} sx={{ p: 1, m: 1 }} textAlign="left">
  49. <AreaBox title="Email">
  50. <RHFTextField name="email" />
  51. </AreaBox>
  52. <Divider />
  53. <Stack direction="row" spacing={2}>
  54. <Button variant="text" onClick={handlePrev}>
  55. 戻る
  56. </Button>
  57. <Button variant="contained" type="submit">
  58. 次へ
  59. </Button>
  60. </Stack>
  61. </Stack>
  62. </FormProvider>
  63. );
  64. return { element, values: form.getValues, setValue: form.setValue };
  65. }