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/SignUpForm1.tsx | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/SignUpForm1.tsx (limited to 'src/SignUpForm1.tsx') 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}
+
+
+ +
+
+ + ); +} + -- cgit v1.2.3