-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: initial form input * feat: title and subtitle form
- Loading branch information
Showing
8 changed files
with
243 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useFormContext } from 'react-hook-form'; | ||
|
||
import { cn } from '@/utils/cn'; | ||
|
||
type FormInputProps = { | ||
name: string; | ||
label: string; | ||
placeholder: string; | ||
required?: boolean; | ||
}; | ||
|
||
const FormInput = ({ | ||
name, | ||
label, | ||
placeholder, | ||
required = false, | ||
}: FormInputProps) => { | ||
const { | ||
register, | ||
formState: { errors }, | ||
} = useFormContext(); | ||
|
||
const inputError = errors[name]; | ||
|
||
return ( | ||
<label htmlFor={name} className="form-control w-full max-w-xs"> | ||
<div className="label"> | ||
<span className={cn('label-text', inputError && 'text-error')}> | ||
{label} | ||
{required && <span className="ml-1 text-red-700">*</span>} | ||
</span> | ||
</div> | ||
<input | ||
type="text" | ||
id={name} | ||
{...register(name)} | ||
placeholder={placeholder} | ||
className={cn( | ||
'input input-sm input-bordered w-full max-w-xs', | ||
inputError && 'input-error', | ||
)} | ||
/> | ||
{inputError && ( | ||
<div className="label"> | ||
<span className="label-text-alt text-error"> | ||
{inputError?.message as string} | ||
</span> | ||
</div> | ||
)} | ||
</label> | ||
); | ||
}; | ||
|
||
export default FormInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { FormProvider, SubmitHandler, useForm } from 'react-hook-form'; | ||
import { yupResolver } from '@hookform/resolvers/yup'; | ||
|
||
import FormInput from '../FormInput/FormInput'; | ||
import { MenuFormInputs, menuFormSchema } from '@/schemas/menuForm'; | ||
|
||
const MenuForm = () => { | ||
const methods = useForm<MenuFormInputs>({ | ||
resolver: yupResolver(menuFormSchema), | ||
}); | ||
|
||
const { handleSubmit } = methods; | ||
|
||
const onFormSubmit: SubmitHandler<MenuFormInputs> = (values) => { | ||
alert(JSON.stringify(values, null, 2)); | ||
}; | ||
|
||
return ( | ||
<div className="basis-1/2"> | ||
<FormProvider {...methods}> | ||
<form | ||
className="flex flex-col items-center gap-4" | ||
onSubmit={handleSubmit(onFormSubmit)}> | ||
<div className="flex flex-col items-center gap-2 w-full"> | ||
<FormInput | ||
name="title" | ||
label="Menu Title" | ||
placeholder="Title" | ||
required | ||
/> | ||
<FormInput | ||
name="subtitle" | ||
label="Menu Subtitle" | ||
placeholder="Subtitle" | ||
/> | ||
</div> | ||
<div className="flex justify-end max-w-xs w-full"> | ||
<button className="btn btn-sm btn-primary">Submit</button> | ||
</div> | ||
</form> | ||
</FormProvider> | ||
</div> | ||
); | ||
}; | ||
|
||
export default MenuForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,31 @@ | ||
import { ReactElement, ReactNode } from 'react'; | ||
import { NextPage } from 'next'; | ||
import Head from 'next/head'; | ||
|
||
import '@/styles/globals.css'; | ||
|
||
import type { AppProps } from 'next/app'; | ||
|
||
const App = ({ Component, pageProps }: AppProps) => { | ||
return <Component {...pageProps} />; | ||
type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & { | ||
getLayout?: (page: ReactElement) => ReactNode; | ||
}; | ||
|
||
type AppPropsWithLayout<P = {}> = AppProps<P> & { | ||
Component: NextPageWithLayout<P>; | ||
}; | ||
|
||
const App = ({ Component, pageProps }: AppPropsWithLayout) => { | ||
const getLayout = Component.getLayout ?? ((page) => page); | ||
|
||
return ( | ||
<> | ||
<Head> | ||
<title>Easy-Menu</title> | ||
<meta content="width=device-width, initial-scale=1.0" name="viewport" /> | ||
</Head> | ||
{getLayout(<Component {...pageProps} />)} | ||
</> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as yup from 'yup'; | ||
|
||
const TitleMessages = { | ||
required: 'Menu Title is required!', | ||
}; | ||
|
||
export const menuFormSchema = yup | ||
.object({ | ||
title: yup.string().trim().required(TitleMessages.required), | ||
subtitle: yup.string().trim().optional(), | ||
}) | ||
.required(); | ||
|
||
export type MenuFormInputs = yup.InferType<typeof menuFormSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ClassValue, clsx } from 'clsx'; | ||
import { twMerge } from 'tailwind-merge'; | ||
|
||
export const cn = (...inputs: ClassValue[]) => { | ||
return twMerge(clsx(inputs)); | ||
}; |