From 1d3b5fcf0dc02557aa2eba02074ec84bd497a768 Mon Sep 17 00:00:00 2001 From: Masaya Tojo Date: Sun, 1 Sep 2024 20:21:06 +0900 Subject: Update tsx files --- src/SignUp.tsx | 85 ++++++++++++++--------------------------------------- src/SignUpForm1.tsx | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ src/SignUpForm2.tsx | 9 ++++++ src/Top.tsx | 4 ++- src/routes.tsx | 10 +++++-- 5 files changed, 118 insertions(+), 67 deletions(-) create mode 100644 src/SignUpForm1.tsx create mode 100644 src/SignUpForm2.tsx (limited to 'src') diff --git a/src/SignUp.tsx b/src/SignUp.tsx index 2a7f448..3103758 100644 --- a/src/SignUp.tsx +++ b/src/SignUp.tsx @@ -1,73 +1,32 @@ import './SignUp.css' -import { useState } from "react"; -import { useForm } from "react-hook-form"; -import { zodResolver } from "@hookform/resolvers/zod"; -import * as z from "zod" +import { Link } from 'react-router-dom'; -const schema = z.object({ - name: z.string(), - email: z.string().email("それメアドじゃないよ"), - birthday: z.coerce.date().max(new Date(), { message: "まだ生まれてないじゃん" }), - language: z.string(), -}) +/* import { useState } from "react"; + * import { useForm } from "react-hook-form"; + * import { zodResolver } from "@hookform/resolvers/zod"; */ +/* import * as z from "zod" */ +/* + * const schema = z.object({ + * name: z.string().min(1, { message: '必須項目です' }), + * email: z.string().min(1, { message: '必須項目です' }).email("メールアドレスを入力してください"), + * language: z.string(), + * }) */ -type Answer = { - name: string, - email: string, - birthday: Date, - language: string, -} +/* type Answer = { + * name: string, + * email: string, + * birthday: Date, + * language: string, + * } */ export const SignUp = () => { - const { register, handleSubmit, reset, formState: { errors } } = useForm({ - resolver: zodResolver(schema) - }); - const [answer, setAnswer] = useState(null); - - const yourAnswer = answer ? ( - <> -

あなたの情報です

-
-
おぬしの名前は…
-
{answer.name}
-
おぬしのメアドは…
-
{answer.email}
-
あなたの誕生日
-
{answer.birthday.getFullYear()}年{answer.birthday.getMonth()}月{answer.birthday.getDay()}日
-
おぬしの好きなプログラミング言語は…
-
{answer.language}
-
- - ) : (<>) - return ( <> -

会員登録

-
setAnswer(data))}> - - - {errors.name?.message} - - - {errors.email?.message &&

{errors.email?.message}

} - - - {errors.birthday?.message &&

{errors.birthday?.message}

} - - - {errors.language?.message &&

{errors.language?.message}

} - - -
- - {yourAnswer} +
+

会員登録

+

サービスの利用のためには会員登録が必要です。

+ 会員登録を始める +
); } diff --git a/src/SignUpForm1.tsx b/src/SignUpForm1.tsx new file mode 100644 index 0000000..fee515f --- /dev/null +++ b/src/SignUpForm1.tsx @@ -0,0 +1,77 @@ +import './SignUp.css' +import { useForm } from 'react-hook-form' +import { zodResolver } from "@hookform/resolvers/zod" +import * as z from "zod" +import parsePhoneNumber from 'libphonenumber-js' + +const kanaRegex = /^[ァ-ン]+$/ + +const schema = z.object({ + name: z.string().min(1, { message: '必須項目です' }), + kana: z.string().min(1, { message: '必須項目です' }).regex(kanaRegex, { message: 'カタカナを入力してください' }), + tel1: z.string(), + tel2: z.string(), + tel3: z.string(), +}).refine( + ({ tel1, tel2, tel3 }) => (tel1.length > 0 && tel2.length > 0 && tel3.length > 0), + { + message: '必須項目です', + path: ['tel3'], + } +).refine( + ({ tel1, tel2, tel3 }) => { + const phoneNumber = parsePhoneNumber('+81' + `${tel1}${tel2}${tel3}`) + console.log([`${tel1}${tel2}${tel3}`, phoneNumber?.isValid()]) + return phoneNumber?.isValid() + }, + { + message: '電話番号が不正です', + path: ['tel3'], + } +) + +type Form1 = { + name: string, + kana: string, + tel1: string, + tel2: string, + tel3: string, +} + +export const SignUpForm1 = () => { + const { register, handleSubmit, formState: { errors } } = useForm({ + resolver: zodResolver(schema), + }); + + const onsubmit = (data: Form1) => console.log(data); + const onerror = (err: any) => console.log(err); + + return ( + <> +

会員登録 フェーズ 1

+
+
+ + +
{errors.name?.message}
+
+
+ + +
{errors.kana?.message}
+
+
+ + - + - + +
{errors.tel3?.message}
+
+
+ +
+
+ + ); +} + diff --git a/src/SignUpForm2.tsx b/src/SignUpForm2.tsx new file mode 100644 index 0000000..1824fd6 --- /dev/null +++ b/src/SignUpForm2.tsx @@ -0,0 +1,9 @@ +import './SignUp.css' + +export const SignUpForm2 = () => { + return ( + <> +

会員登録 フェーズ 2

+ + ); +} diff --git a/src/Top.tsx b/src/Top.tsx index 7907ed5..9d88bc6 100644 --- a/src/Top.tsx +++ b/src/Top.tsx @@ -1,11 +1,13 @@ import './Top.css' +import { Link } from 'react-router-dom'; export const Top = () => { return ( <>

トップページ

); diff --git a/src/routes.tsx b/src/routes.tsx index 8cac244..cb2b37c 100644 --- a/src/routes.tsx +++ b/src/routes.tsx @@ -1,8 +1,12 @@ import { createBrowserRouter } from 'react-router-dom'; -import { Top } from './Top.tsx'; -import { SignUp } from './SignUp.tsx'; +import { Top } from './Top'; +import { SignUp } from './SignUp'; +import { SignUpForm1 } from './SignUpForm1'; +import { SignUpForm2 } from './SignUpForm2'; export const routes = createBrowserRouter([ { path: '/', element: }, - { path: '/sign-up', element: } + { path: '/sign-up', element: }, + { path: '/sign-up/form1', element: }, + { path: '/sign-up/form2', element: }, ]); -- cgit v1.2.3