tsf-month-field
shuip/tsf-month-fieldFreeComponent
shuip publishes no description for this item.
Install it
npx shadcn@latest add https://shuip.plvo.dev/r/tsf-month-field.jsonSource
1'use client';23import type { Locale } from 'date-fns';4import { format } from 'date-fns';5import { CalendarIcon } from 'lucide-react';6import * as React from 'react';7import type { Matcher } from 'react-day-picker';8import { Button } from '@/components/ui/button';9import { Calendar } from '@/components/ui/calendar';10import { Field, FieldDescription, FieldError, FieldLabel } from '@/components/ui/field';11import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';12import { useFieldContext } from '@/components/ui/shuip/tanstack-form/form-context';13import { cn } from '@/lib/utils';1415export interface MonthFieldProps {16 label?: string;17 description?: string;18 placeholder?: string;19 minDate?: Date;20 maxDate?: Date;21 locale?: Locale;22 disabled?: boolean;23 fieldProps?: React.ComponentProps<typeof Field>;24 className?: string;25}2627const toFirstOfMonth = (date: Date): Date => new Date(date.getFullYear(), date.getMonth(), 1);28const toLastOfMonth = (date: Date): Date => new Date(date.getFullYear(), date.getMonth() + 1, 0);2930export function MonthField({31 label,32 description,33 placeholder = 'Pick a month',34 minDate,35 maxDate,36 locale,37 disabled,38 fieldProps,39 className,40}: MonthFieldProps) {41 const field = useFieldContext<Date | undefined>();42 const { isValid, errors } = field.state.meta;43 const [open, setOpen] = React.useState(false);4445 const value = field.state.value;4647 const handleSelect = (date: Date | undefined) => {48 if (!date) {49 field.handleChange(undefined);50 return;51 }52 field.handleChange(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={!isValid} {...fieldProps}>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.handleBlur}74 aria-invalid={!isValid}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 |
