{
  "name": "tsf-month-field",
  "type": "registry:ui",
  "dependencies": [
    "date-fns",
    "lucide-react",
    "react",
    "react-day-picker"
  ],
  "registryDependencies": [
    "button",
    "calendar",
    "field",
    "popover",
    "tsf-form-context"
  ],
  "files": [
    {
      "path": "./items/tanstack-form/month-field/component.tsx",
      "type": "registry:ui",
      "target": "./components/ui/shuip/tanstack-form/month-field.tsx",
      "content": "'use client';\n\nimport type { Locale } from 'date-fns';\nimport { format } from 'date-fns';\nimport { CalendarIcon } from 'lucide-react';\nimport * as React from 'react';\nimport type { Matcher } from 'react-day-picker';\nimport { Button } from '@/components/ui/button';\nimport { Calendar } from '@/components/ui/calendar';\nimport { Field, FieldDescription, FieldError, FieldLabel } from '@/components/ui/field';\nimport { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';\nimport { useFieldContext } from '@/components/ui/shuip/tanstack-form/form-context';\nimport { cn } from '@/lib/utils';\n\nexport interface MonthFieldProps {\n  label?: string;\n  description?: string;\n  placeholder?: string;\n  minDate?: Date;\n  maxDate?: Date;\n  locale?: Locale;\n  disabled?: boolean;\n  fieldProps?: React.ComponentProps<typeof Field>;\n  className?: string;\n}\n\nconst toFirstOfMonth = (date: Date): Date => new Date(date.getFullYear(), date.getMonth(), 1);\nconst toLastOfMonth = (date: Date): Date => new Date(date.getFullYear(), date.getMonth() + 1, 0);\n\nexport function MonthField({\n  label,\n  description,\n  placeholder = 'Pick a month',\n  minDate,\n  maxDate,\n  locale,\n  disabled,\n  fieldProps,\n  className,\n}: MonthFieldProps) {\n  const field = useFieldContext<Date | undefined>();\n  const { isValid, errors } = field.state.meta;\n  const [open, setOpen] = React.useState(false);\n\n  const value = field.state.value;\n\n  const handleSelect = (date: Date | undefined) => {\n    if (!date) {\n      field.handleChange(undefined);\n      return;\n    }\n    field.handleChange(toFirstOfMonth(date));\n    setOpen(false);\n  };\n\n  const minMonth = minDate ? toFirstOfMonth(minDate) : undefined;\n  const maxMonth = maxDate ? toLastOfMonth(maxDate) : undefined;\n\n  const disabledMatchers: Matcher[] = [];\n  if (minMonth) disabledMatchers.push({ before: minMonth });\n  if (maxMonth) disabledMatchers.push({ after: maxMonth });\n\n  return (\n    <Field className='gap-2' data-invalid={!isValid} {...fieldProps}>\n      {label && <FieldLabel htmlFor={field.name}>{label}</FieldLabel>}\n      <Popover open={open} onOpenChange={setOpen}>\n        <PopoverTrigger asChild>\n          <Button\n            id={field.name}\n            type='button'\n            variant='outline'\n            disabled={disabled}\n            onBlur={field.handleBlur}\n            aria-invalid={!isValid}\n            className={cn('w-full justify-between font-normal', !value && 'text-muted-foreground', className)}\n          >\n            {value ? format(value, 'MMMM yyyy', { locale }) : placeholder}\n            <CalendarIcon className='size-4 opacity-60' />\n          </Button>\n        </PopoverTrigger>\n        <PopoverContent className='w-auto p-0' align='start'>\n          <Calendar\n            mode='single'\n            captionLayout='dropdown'\n            selected={value}\n            onSelect={handleSelect}\n            disabled={disabledMatchers.length ? disabledMatchers : undefined}\n            startMonth={minMonth}\n            endMonth={maxMonth}\n            defaultMonth={value ?? minMonth}\n            locale={locale}\n          />\n        </PopoverContent>\n      </Popover>\n      {!isValid && (\n        <FieldError\n          className='text-xs text-left'\n          errors={errors.map((error) => ({ message: typeof error === 'string' ? error : error?.message }))}\n        />\n      )}\n      {description && <FieldDescription className='text-xs'>{description}</FieldDescription>}\n    </Field>\n  );\n}\n"
    }
  ],
  "$schema": "https://ui.shadcn.com/schema/registry-item.json"
}
