diff --git a/pages/sample/index.tsx b/pages/sample/index.tsx new file mode 100644 index 0000000..366a224 --- /dev/null +++ b/pages/sample/index.tsx @@ -0,0 +1,289 @@ +import { WithDefaultLayout } from "@/components/DefautLayout"; +import { Title } from "@/components/Title"; +import { Page } from "@/types/Page"; +import { Button, Checkbox, DatePicker, Input, InputNumber, Radio, Rate, Select, Upload } from "antd"; +import { Controller, useForm } from "react-hook-form"; +import { z } from "zod"; +import { zodResolver } from "@hookform/resolvers/zod"; +import dayjs, { type Dayjs } from "dayjs"; +import { UploadOutlined } from '@ant-design/icons'; + +const foods = [ + { + label: 'Nasi Goreng', + value: 'Nasi Goreng' + }, + { + label: 'Nasi Padang', + value: 'Nasi Padang' + }, + { + label: 'Nasi Campur', + value: 'Nasi Campur' + } +]; + +const colors = [ + { + label: 'Red', + value: 'Red' + }, + { + label: 'Blue', + value: 'Blue' + }, + { + label: 'Green', + value: 'Green' + } +]; + +const MAX_FILE_SIZE = 500000; +const ACCEPTED_IMAGE_TYPES = ["image/jpeg", "image/jpg", "image/png", "image/webp"]; +const TestFormInputSchema = z.object({ + text: z.string({ + invalid_type_error: 'Please input the text', + required_error: 'Please input the text' + }) + .min(1, 'Please input the text') + .max(255, 'Cannot exceed 255 characters'), + amount: z.number({ + invalid_type_error: 'Please input the amount', + required_error: 'Please input the amount' + }) + .int('Amount must be a number') + .gte(1, 'Amount cannot be empty') + .lte(1000, 'Amount cannot exceed 1000'), + gender: z.enum(['male', 'female'], { + required_error: 'Please pick a gender' + }), + rating: z.number({ + required_error: 'Please give a rating' + }) + .gte(1, 'Please give a rating') + .lte(5), + food: z.string({ + required_error: 'Please select a food' + }), + color: z.string({ + required_error: 'Please tick your favorite colour(s)' + }).array(), + dob: z.instanceof(dayjs as unknown as typeof Dayjs) + .refine((dobValue) => (dobValue.isAfter(dayjs("1990-01-01")) && dobValue.isBefore(dayjs())) + , { + message: "invalid date" + }), + img: z.any() + .refine((uploaded) => uploaded?.fileList.length > 0 ,"Image is required") + .refine((uploaded) => ACCEPTED_IMAGE_TYPES.includes(uploaded?.fileList[0].type), "Must be Image") + .refine((uploaded) => uploaded?.fileList[0].size <= MAX_FILE_SIZE, "Image must not exceed 5MB") + +}); + +type TestFormInput = z.infer; + +const Sample: React.FC = () => { + const { control, handleSubmit, formState: { errors } } = useForm({ + resolver: zodResolver(TestFormInputSchema) + }); + const onSubmit = (data: TestFormInput) => { + alert(JSON.stringify(data)); + } + + return ( + +
+
+ ( + <> +
+ +
+
+ +
+ + )} + /> +
+ {errors.text && {errors.text.message}} +
+ + ( + <> +
+ +
+
+ +
+ + )} + /> +
+ {errors.amount && {errors.amount.message}} +
+ + ( + <> +
+ +
+
+ + Male + Female + +
+ + )} + /> +
+ {errors.gender && {errors.gender.message}} +
+ + ( + <> +
+ +
+
+ +
+ + )} + /> +
+ {errors.rating && {errors.rating.message}} +
+ + ( + <> +
+ +
+
+