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.

84 line
1.9KB

  1. import { yupResolver } from "@hookform/resolvers/yup";
  2. import { Box, Button, Stack, Typography } from "@mui/material";
  3. import { HasChildren } from "@types";
  4. import {
  5. FormProvider,
  6. RHFAutoComplete,
  7. RHFTextField,
  8. } from "components/hook-form";
  9. import {
  10. AutoCompleteOption,
  11. AutoCompleteOptionType,
  12. getValue,
  13. } from "components/hook-form/RHFAutoComplete";
  14. import { useState } from "react";
  15. import { useForm } from "react-hook-form";
  16. import * as Yup from "yup";
  17. type AreaBoxProps = {
  18. title: string;
  19. } & HasChildren;
  20. function AreaBox({ title, children }: AreaBoxProps) {
  21. return (
  22. <Box sx={{ maxWidth: 500 }}>
  23. <Typography variant="subtitle1">{title}</Typography>
  24. {children}
  25. </Box>
  26. );
  27. }
  28. type FormProps = {
  29. address: string;
  30. };
  31. type Props = {
  32. onNext?: VoidFunction;
  33. onPrev?: VoidFunction;
  34. };
  35. export default function useInputSMSSendAddress({ onNext, onPrev }: Props) {
  36. const form = useForm<FormProps>({
  37. defaultValues: {
  38. address: "",
  39. },
  40. resolver: yupResolver(
  41. Yup.object().shape({
  42. address: Yup.string()
  43. .required("必須項目です")
  44. .matches(/^[0-9]{11}$/, "正しい電話番号を入力してください"),
  45. })
  46. ),
  47. });
  48. const handleSubmit = () => {
  49. if (onNext) {
  50. onNext();
  51. }
  52. };
  53. const handlePrev = () => {
  54. if (onPrev) {
  55. onPrev();
  56. }
  57. };
  58. const element = (
  59. <FormProvider methods={form} onSubmit={form.handleSubmit(handleSubmit)}>
  60. <Stack spacing={2} sx={{ p: 1, m: 1 }}>
  61. <AreaBox title="SMS送信先">
  62. <RHFTextField name="address" size="small" />
  63. </AreaBox>
  64. <Stack direction="row" spacing={2}>
  65. <Button variant="text" onClick={handlePrev}>
  66. 戻る
  67. </Button>
  68. <Button variant="contained" type="submit">
  69. 次へ
  70. </Button>
  71. </Stack>
  72. </Stack>
  73. </FormProvider>
  74. );
  75. return { element, values: form.getValues };
  76. }