mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-19 11:12:00 +10:00
178 lines
5.3 KiB
TypeScript
178 lines
5.3 KiB
TypeScript
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { t } from "@lingui/macro";
|
|
import { X } from "@phosphor-icons/react";
|
|
import { defaultSkill, skillSchema } from "@reactive-resume/schema";
|
|
import {
|
|
Badge,
|
|
BadgeInput,
|
|
FormControl,
|
|
FormDescription,
|
|
FormField,
|
|
FormItem,
|
|
FormLabel,
|
|
FormMessage,
|
|
Input,
|
|
Slider,
|
|
} from "@reactive-resume/ui";
|
|
import { AnimatePresence, motion } from "framer-motion";
|
|
import { useState } from "react";
|
|
import { useForm } from "react-hook-form";
|
|
import type { z } from "zod";
|
|
|
|
import { SectionDialog } from "../sections/shared/section-dialog";
|
|
|
|
const formSchema = skillSchema;
|
|
|
|
type FormValues = z.infer<typeof formSchema>;
|
|
|
|
const handleDragOver = (e: React.DragEvent) => {
|
|
e.preventDefault();
|
|
};
|
|
|
|
export const SkillsDialog = () => {
|
|
const form = useForm<FormValues>({
|
|
defaultValues: defaultSkill,
|
|
resolver: zodResolver(formSchema),
|
|
});
|
|
|
|
const [pendingKeyword, setPendingKeyword] = useState("");
|
|
const [draggedIndex, setDraggedIndex] = useState<number | null>(null);
|
|
|
|
const handleDrop = (
|
|
e: React.DragEvent,
|
|
dropIndex: number,
|
|
field: { value: string[]; onChange: (value: string[]) => void },
|
|
) => {
|
|
e.preventDefault();
|
|
if (draggedIndex === null) return;
|
|
|
|
const newKeywords = [...field.value];
|
|
const [draggedItem] = newKeywords.splice(draggedIndex, 1);
|
|
newKeywords.splice(dropIndex, 0, draggedItem);
|
|
|
|
field.onChange(newKeywords);
|
|
setDraggedIndex(null);
|
|
};
|
|
|
|
return (
|
|
<SectionDialog<FormValues>
|
|
id="skills"
|
|
form={form}
|
|
defaultValues={defaultSkill}
|
|
pendingKeyword={pendingKeyword}
|
|
>
|
|
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
|
<FormField
|
|
name="name"
|
|
control={form.control}
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{t`Name`}</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
name="description"
|
|
control={form.control}
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{t`Description`}</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
name="level"
|
|
control={form.control}
|
|
render={({ field }) => (
|
|
<FormItem className="sm:col-span-2">
|
|
<FormLabel>{t`Level`}</FormLabel>
|
|
<FormControl className="py-2">
|
|
<div className="flex items-center gap-x-4">
|
|
<Slider
|
|
{...field}
|
|
min={0}
|
|
max={5}
|
|
value={[field.value]}
|
|
orientation="horizontal"
|
|
onValueChange={(value) => {
|
|
field.onChange(value[0]);
|
|
}}
|
|
/>
|
|
|
|
{field.value > 0 ? (
|
|
<span className="text-base font-bold">{field.value}</span>
|
|
) : (
|
|
<span className="text-base font-bold">{t`Hidden`}</span>
|
|
)}
|
|
</div>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
name="keywords"
|
|
control={form.control}
|
|
render={({ field }) => (
|
|
<div className="space-y-3 sm:col-span-2">
|
|
<FormItem>
|
|
<FormLabel>{t`Keywords`}</FormLabel>
|
|
<FormControl>
|
|
<BadgeInput {...field} setPendingKeyword={setPendingKeyword} />
|
|
</FormControl>
|
|
<FormDescription>
|
|
{t`You can add multiple keywords by separating them with a comma or pressing enter.`}
|
|
</FormDescription>
|
|
<FormMessage />
|
|
</FormItem>
|
|
|
|
<div className="flex flex-wrap items-center gap-x-2 gap-y-3">
|
|
<AnimatePresence>
|
|
{field.value.map((item, index) => (
|
|
<motion.div
|
|
key={item}
|
|
layout
|
|
draggable
|
|
initial={{ opacity: 0, y: -50 }}
|
|
animate={{ opacity: 1, y: 0, transition: { delay: index * 0.1 } }}
|
|
exit={{ opacity: 0, x: -50 }}
|
|
onDragStart={() => {
|
|
setDraggedIndex(index);
|
|
}}
|
|
onDragOver={handleDragOver}
|
|
onDrop={(e) => {
|
|
handleDrop(e, index, field);
|
|
}}
|
|
>
|
|
<Badge
|
|
className="cursor-pointer"
|
|
onClick={() => {
|
|
field.onChange(field.value.filter((v) => item !== v));
|
|
}}
|
|
>
|
|
<span className="mr-1">{item}</span>
|
|
<X size={12} weight="bold" />
|
|
</Badge>
|
|
</motion.div>
|
|
))}
|
|
</AnimatePresence>
|
|
</div>
|
|
</div>
|
|
)}
|
|
/>
|
|
</div>
|
|
</SectionDialog>
|
|
);
|
|
};
|