dstpabuaran.com/resources/js/components/date-picker.tsx
Yoga Pangestu e7aa582572 feat: add cutting management functionality with CRUD operations
- Implemented CuttingIndex component for listing and managing cuttings.
- Added routes for cutting management in web.php.
- Created CuttingTest for testing cutting-related features including authorization, validation, and stock management.
- Updated roles create and edit pages to include necessary imports.
- Refactored settings and profile pages to streamline imports.
- Enhanced permissions checks for cutting management actions.
2026-08-04 02:24:11 +07:00

111 lines
2.7 KiB
TypeScript

import { format } from 'date-fns';
import { id } from 'date-fns/locale';
import { CalendarIcon } from 'lucide-react';
import * as React from 'react';
import { Button } from '@/components/ui/button';
import { Calendar } from '@/components/ui/calendar';
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover';
import { cn } from '@/lib/utils';
interface DatePickerProps {
value?: Date | string | null;
onChange?: (date: Date | undefined) => void;
placeholder?: string;
disabled?: boolean;
className?: string;
name?: string;
id?: string;
min?: Date;
max?: Date;
}
function DatePicker({
value,
onChange,
placeholder = 'Pilih tanggal',
disabled = false,
className,
name,
id,
min,
max,
}: DatePickerProps) {
const [open, setOpen] = React.useState(false);
const date = React.useMemo(() => {
if (!value) {
return undefined;
}
if (value instanceof Date) {
return value;
}
return new Date(value);
}, [value]);
const formattedDate = React.useMemo(() => {
if (!date) {
return '';
}
return format(date, 'dd MMM yyyy', { locale: id });
}, [date]);
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
id={id}
variant="outline"
disabled={disabled}
className={cn(
'w-full justify-start text-left font-normal',
!date && 'text-muted-foreground',
className,
)}
>
<CalendarIcon className="mr-2 h-4 w-4" />
{date ? formattedDate : placeholder}
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0" align="start">
<Calendar
mode="single"
selected={date}
onSelect={(selectedDate) => {
onChange?.(selectedDate);
setOpen(false);
}}
disabled={(date) => {
if (min && date < min) {
return true;
}
if (max && date > max) {
return true;
}
return false;
}}
initialFocus
/>
</PopoverContent>
{name && (
<input
type="hidden"
name={name}
value={date ? format(date, 'yyyy-MM-dd') : ''}
/>
)}
</Popover>
);
}
export { DatePicker };