rhf-month-field
shuip/rhf-month-fieldFreeComponent
shuip publishes no description for this item.
Install it
npx shadcn@latest add https://shuip.plvo.dev/r/rhf-month-field.jsonSource
1'use client';23import type { Lens } from '@hookform/lenses';4import type { Locale } from 'date-fns';5import { format } from 'date-fns';6import { CalendarIcon } from 'lucide-react';7import * as React from 'react';8import type { Matcher } from 'react-day-picker';9import { useController } from 'react-hook-form';10import { Button } from '@/components/ui/button';11import { Calendar } from '@/components/ui/calendar';12import { Field, FieldDescription, FieldError, FieldLabel } from '@/components/ui/field';13import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';14import { cn } from '@/lib/utils';1516export interface MonthFieldProps {17 lens: Lens<Date | undefined>;18 label?: string;19 description?: string;20 placeholder?: string;21 minDate?: Date;22 maxDate?: Date;23 locale?: Locale;24 disabled?: boolean;25 className?: string;26}2728const toFirstOfMonth = (date: Date): Date => new Date(date.getFullYear(), date.getMonth(), 1);29const toLastOfMonth = (date: Date): Date => new Date(date.getFullYear(), date.getMonth() + 1, 0);3031export function MonthField({32 lens,33 label,34 description,35 placeholder = 'Pick a month',36 minDate,37 maxDate,38 locale,39 disabled,40 className,41}: MonthFieldProps) {42 const { field, fieldState } = useController(lens.interop());43 const [open, setOpen] = React.useState(false);4445 const value: Date | undefined = field.value;4647 const handleSelect = (date: Date | undefined) => {48 if (!date) {49 field.onChange(undefined);50 return;51 }52 field.onChange(toFirstOfMonth(date));53 setOpen(false);54 };5556 const minMonth = minDate ? toFirstOfMonth(minDate) : undefined;57 const maxMonth = maxDate ? toLastOfMonth(maxDate) : undefined;5859 const disabledMatchers: Matcher[] = [];60 if (minMonth) disabledMatchers.push({ before: minMonth });61 if (maxMonth) disabledMatchers.push({ after: maxMonth });6263 return (64 <Field className='gap-2' data-invalid={fieldState.invalid}>65 {label && <FieldLabel htmlFor={field.name}>{label}</FieldLabel>}66 <Popover open={open} onOpenChange={setOpen}>67 <PopoverTrigger asChild>68 <Button69 id={field.name}70 type='button'71 variant='outline'72 disabled={disabled}73 onBlur={field.onBlur}74 aria-invalid={fieldState.invalid}75 className={cn('w-full justify-between font-normal', !value && 'text-muted-foreground', className)}76 >77// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from shuip
All 52 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| confirmation-dialogconfirmation-dialog | shuip | Components | Free | 1 dep | |
| copy-buttoncopy-button | shuip | Components | Free | 3 deps | |
| hover-revealhover-reveal | shuip | Components | Free | 1 dep | |
| rhf-address-fieldrhf-address-field | shuip | Components | Free | 5 deps · 2 files | |
| rhf-autocomplete-fieldrhf-autocomplete-field | shuip | Components | Free | 4 deps | |
| rhf-checkbox-fieldrhf-checkbox-field | shuip | Components | Free | 3 deps | |
| rhf-combobox-fieldrhf-combobox-field | shuip | Components | Free | 5 deps | |
| rhf-date-fieldrhf-date-field | shuip | Components | Free | 6 deps |
