2025-04-27 15:25:45 -03:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
2025-03-30 16:42:51 -03:00
|
|
|
import React from 'react';
|
|
|
|
import { useForm, Controller } from 'react-hook-form';
|
|
|
|
import { z } from 'zod';
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
|
|
|
|
|
|
const items = [
|
|
|
|
{ id: 'recents', label: 'Recents' },
|
|
|
|
{ id: 'home', label: 'Home' },
|
|
|
|
{ id: 'applications', label: 'Applications' },
|
|
|
|
{ id: 'desktop', label: 'Desktop' },
|
|
|
|
{ id: 'downloads', label: 'Downloads' },
|
|
|
|
{ id: 'documents', label: 'Documents' },
|
|
|
|
];
|
|
|
|
|
|
|
|
const displayFormSchema = z.object({
|
|
|
|
items: z.array(z.string()).refine((value) => value.some((item) => item), {
|
|
|
|
message: "You have to select at least one item.",
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
type DisplayFormValues = z.infer<typeof displayFormSchema>;
|
|
|
|
|
|
|
|
const defaultValues: Partial<DisplayFormValues> = {
|
|
|
|
items: ['recents', 'home'],
|
|
|
|
};
|
|
|
|
|
|
|
|
export function DisplayForm() {
|
|
|
|
const { control, handleSubmit } = useForm<DisplayFormValues>({
|
|
|
|
resolver: zodResolver(displayFormSchema),
|
|
|
|
defaultValues,
|
|
|
|
});
|
|
|
|
|
|
|
|
const onSubmit = (data: DisplayFormValues) => {
|
|
|
|
console.log(JSON.stringify(data, null, 2));
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2025-03-30 19:39:59 -03:00
|
|
|
<div>
|
|
|
|
<h3 className="text-sm font-medium mb-2">Sidebar</h3>
|
|
|
|
<p className="text-sm text-gray-500 mb-4">
|
2025-03-30 16:42:51 -03:00
|
|
|
Select the items you want to display in the sidebar.
|
2025-03-30 19:39:59 -03:00
|
|
|
</p>
|
2025-03-30 16:42:51 -03:00
|
|
|
{items.map((item) => (
|
|
|
|
<Controller
|
|
|
|
key={item.id}
|
|
|
|
control={control}
|
|
|
|
name="items"
|
|
|
|
render={({ field }) => (
|
2025-03-30 19:39:59 -03:00
|
|
|
<div className="flex items-center mb-3">
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
id={item.id}
|
|
|
|
checked={field.value?.includes(item.id)}
|
|
|
|
onChange={(e) => {
|
|
|
|
const updatedValue = e.target.checked
|
2025-03-30 16:42:51 -03:00
|
|
|
? [...field.value, item.id]
|
|
|
|
: field.value?.filter((value) => value !== item.id);
|
|
|
|
field.onChange(updatedValue);
|
|
|
|
}}
|
2025-03-30 19:39:59 -03:00
|
|
|
className="h-4 w-4 rounded border-gray-300 text-blue-600 focus:ring-blue-500"
|
2025-03-30 16:42:51 -03:00
|
|
|
/>
|
2025-03-30 19:39:59 -03:00
|
|
|
<label htmlFor={item.id} className="ml-2 text-sm">
|
|
|
|
{item.label}
|
|
|
|
</label>
|
|
|
|
</div>
|
2025-03-30 16:42:51 -03:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
))}
|
2025-03-30 19:39:59 -03:00
|
|
|
<button
|
|
|
|
className="mt-4 bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"
|
|
|
|
onClick={handleSubmit(onSubmit)}
|
2025-03-30 16:42:51 -03:00
|
|
|
>
|
2025-03-30 19:39:59 -03:00
|
|
|
Update display
|
|
|
|
</button>
|
|
|
|
</div>
|
2025-03-30 16:42:51 -03:00
|
|
|
);
|
|
|
|
}
|