-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Glen Miracle
authored and
Glen Miracle
committed
Jul 10, 2024
1 parent
04cd156
commit 779baf4
Showing
16 changed files
with
1,819 additions
and
84 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
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,184 @@ | ||
/* eslint-disable no-unused-vars */ | ||
import { E164Number } from "libphonenumber-js/core"; | ||
import ReactDatePicker from "react-datepicker"; | ||
import { Control } from "react-hook-form"; | ||
|
||
import { Checkbox } from "@/components/ui/checkbox"; | ||
import { | ||
FormControl, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
} from "@/components/ui/form"; | ||
import { Input } from "@/components/ui/input"; | ||
import { | ||
Select, | ||
SelectContent, | ||
SelectTrigger, | ||
SelectValue, | ||
} from "@/components/ui/select"; | ||
import { Textarea } from "@/components/ui/textarea"; | ||
import { CalendarIcon, LucideIcon } from "lucide-react"; | ||
import { Phone, BoxSelect, Keyboard } from "lucide-react"; | ||
import { PhoneInput } from "./atoms/phone-input"; | ||
import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover"; | ||
import { Button } from "../ui/button"; | ||
import { cn } from "@/lib/styles"; | ||
import { format } from "date-fns"; | ||
import { useState } from "react"; | ||
import { Calendar } from "../ui/calendar"; | ||
|
||
export enum FormFieldType { | ||
INPUT = "input", | ||
TEXTAREA = "textarea", | ||
PHONE_INPUT = "phoneInput", | ||
CHECKBOX = "checkbox", | ||
DATE_PICKER = "datePicker", | ||
SELECT = "select", | ||
SKELETON = "skeleton", | ||
} | ||
|
||
interface CustomProps { | ||
control: Control<any>; | ||
name: string; | ||
label?: string; | ||
placeholder?: string; | ||
icon?: LucideIcon; //TODO: change this to a lucide type | ||
iconAlt?: string; | ||
disabled?: boolean; | ||
dateFormat?: string; | ||
showTimeSelect?: boolean; | ||
children?: React.ReactNode; | ||
renderSkeleton?: (field: any) => React.ReactNode; | ||
fieldType: FormFieldType; | ||
} | ||
|
||
const RenderInput = ({ field, props }: { field: any; props: CustomProps }) => { | ||
const [date, setDate] = useState<Date | null>(null); | ||
switch (props.fieldType) { | ||
case FormFieldType.INPUT: | ||
return ( | ||
<div className="flex rounded-md border border-dark-500"> | ||
{props.icon && <props.icon className="ml-2 s-4" />} | ||
<FormControl> | ||
<Input | ||
placeholder={props.placeholder} | ||
{...field} | ||
className="shad-input border-0" | ||
/> | ||
</FormControl> | ||
</div> | ||
); | ||
case FormFieldType.TEXTAREA: | ||
return ( | ||
<FormControl> | ||
<Textarea | ||
placeholder={props.placeholder} | ||
{...field} | ||
className="shad-textArea" | ||
disabled={props.disabled} | ||
/> | ||
</FormControl> | ||
); | ||
case FormFieldType.PHONE_INPUT: | ||
return ( | ||
<FormControl> | ||
<PhoneInput value={field.value} onChange={field.onChange} /> | ||
</FormControl> | ||
); | ||
case FormFieldType.CHECKBOX: | ||
return ( | ||
<FormControl> | ||
<div className="flex items-center gap-4"> | ||
<Checkbox | ||
id={props.name} | ||
checked={field.value} | ||
onCheckedChange={field.onChange} | ||
/> | ||
<label htmlFor={props.name} className="checkbox-label"> | ||
{props.label} | ||
</label> | ||
</div> | ||
</FormControl> | ||
); | ||
case FormFieldType.DATE_PICKER: | ||
return ( | ||
<div className="flex rounded-md border border-dark-500 bg-dark-400"> | ||
{props.icon && <props.icon className="ml-2 h-4 w-4" />} | ||
<FormControl> | ||
<Popover> | ||
<PopoverTrigger asChild> | ||
<Button | ||
variant="outline" | ||
className={cn( | ||
"w-full justify-start text-left font-normal", | ||
!date && "text-muted-foreground", | ||
)} | ||
> | ||
<CalendarIcon className="mr-2 h-4 w-4" /> | ||
{date ? format(date, "PPP") : <span>Pick a date</span>} | ||
</Button> | ||
</PopoverTrigger> | ||
<PopoverContent className="w-auto p-0" align="start"> | ||
<Calendar | ||
mode="single" | ||
selected={field.value} | ||
captionLayout="dropdown-buttons" | ||
onSelect={(selectedDate) => { | ||
setDate(selectedDate); | ||
field.onChange(selectedDate); | ||
}} | ||
initialFocus | ||
fromYear={1999} | ||
toYear={2023} | ||
/> | ||
</PopoverContent> | ||
</Popover> | ||
</FormControl> | ||
</div> | ||
); | ||
case FormFieldType.SELECT: | ||
return ( | ||
<FormControl> | ||
<Select onValueChange={field.onChange} defaultValue={field.value}> | ||
<FormControl> | ||
<SelectTrigger className="shad-select-trigger"> | ||
<SelectValue placeholder={props.placeholder} /> | ||
</SelectTrigger> | ||
</FormControl> | ||
<SelectContent className="shad-select-content"> | ||
{props.children} | ||
</SelectContent> | ||
</Select> | ||
</FormControl> | ||
); | ||
case FormFieldType.SKELETON: | ||
return props.renderSkeleton ? props.renderSkeleton(field) : null; | ||
default: | ||
return null; | ||
} | ||
}; | ||
|
||
const CustomFormField = (props: CustomProps) => { | ||
const { control, name, label } = props; | ||
|
||
return ( | ||
<FormField | ||
control={control} | ||
name={name} | ||
render={({ field }) => ( | ||
<FormItem className="flex-1"> | ||
{props.fieldType !== FormFieldType.CHECKBOX && label && ( | ||
<FormLabel className="shad-input-label">{label}</FormLabel> | ||
)} | ||
<RenderInput field={field} props={props} /> | ||
|
||
<FormMessage className="shad-error" /> | ||
</FormItem> | ||
)} | ||
/> | ||
); | ||
}; | ||
|
||
export default CustomFormField; |
Oops, something went wrong.