generated from theodorusclarence/ts-nextjs-tailwind-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/NoorFatima01/team-management
- Loading branch information
Showing
3 changed files
with
196 additions
and
4 deletions.
There are no files selected for viewing
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,144 @@ | ||
'use client'; | ||
|
||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { useFieldArray, useForm } from 'react-hook-form'; | ||
import toast from 'react-hot-toast'; | ||
|
||
import { profileFormSchema, profileFormSchemaType } from '@/lib/schemas'; | ||
import { cn } from '@/lib/utils'; | ||
|
||
import { Button } from '@/components/ui/button'; | ||
import { | ||
Form, | ||
FormControl, | ||
FormDescription, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
} from '@/components/ui/form'; | ||
import { Input } from '@/components/ui/input'; | ||
import { Textarea } from '@/components/ui/textarea'; | ||
|
||
interface ProfileFormProps { | ||
defaultValues: Pick<profileFormSchemaType, 'username' | 'email'>; | ||
role: string; | ||
} | ||
|
||
// eslint-disable-next-line unused-imports/no-unused-vars | ||
export function ProfileForm({ defaultValues, role }: ProfileFormProps) { | ||
const form = useForm<profileFormSchemaType>({ | ||
resolver: zodResolver(profileFormSchema), | ||
defaultValues: { | ||
...defaultValues, | ||
bio: '', | ||
urls: [], | ||
}, | ||
mode: 'onChange', | ||
}); | ||
|
||
const { fields, append } = useFieldArray({ | ||
name: 'urls', | ||
control: form.control, | ||
}); | ||
|
||
// eslint-disable-next-line unused-imports/no-unused-vars | ||
function onSubmit(data: profileFormSchemaType) { | ||
toast.success('Profile updated successfully'); | ||
} | ||
|
||
return ( | ||
<Form {...form}> | ||
<form | ||
onSubmit={form.handleSubmit(onSubmit)} | ||
className='mx-auto grid w-full max-w-md gap-6 ' | ||
> | ||
<div className='flex justify-between gap-1'> | ||
<div className='flex-1'> | ||
<FormField | ||
control={form.control} | ||
name='username' | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Username</FormLabel> | ||
<FormControl> | ||
<Input placeholder='Your Username' {...field} /> | ||
</FormControl> | ||
|
||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
</div> | ||
<div className='flex-1'> | ||
<FormField | ||
control={form.control} | ||
name='email' | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Email</FormLabel> | ||
<FormControl> | ||
<Input placeholder='Email' type='email' {...field} /> | ||
</FormControl> | ||
|
||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
</div> | ||
</div> | ||
<FormField | ||
control={form.control} | ||
name='bio' | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Bio</FormLabel> | ||
<FormControl> | ||
<Textarea | ||
placeholder='Tell us a little bit about yourself' | ||
className='resize-none' | ||
{...field} | ||
/> | ||
</FormControl> | ||
|
||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<div> | ||
{fields.map((field, index) => ( | ||
<FormField | ||
control={form.control} | ||
key={field.id} | ||
name={`urls.${index}.value`} | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel className={cn(index !== 0 && 'sr-only')}> | ||
URLs | ||
</FormLabel> | ||
<FormDescription className={cn(index !== 0 && 'sr-only')}> | ||
Add links to your website, blog, or social media profiles. | ||
</FormDescription> | ||
<FormControl> | ||
<Input {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
))} | ||
<Button | ||
type='button' | ||
variant='outline' | ||
size='sm' | ||
className='mt-2' | ||
onClick={() => append({ value: '' })} | ||
> | ||
Add URL | ||
</Button> | ||
</div> | ||
<Button type='submit'>Update profile</Button> | ||
</form> | ||
</Form> | ||
); | ||
} |
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