All files / src/shared/components DatePickerField.tsx

84.21% Statements 32/38
81.25% Branches 13/16
80% Functions 8/10
83.33% Lines 30/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127                      18x                             20x 20x 11x 11x     9x 9x 9x 9x 9x               326x 326x 326x     326x 326x         326x 326x 326x 69x 56x       326x               20x 20x 20x 20x 20x 20x   1x 3x                                                   3x                                          
import { enUS, es, ptBR, type Locale } from 'date-fns/locale'
import { useEffect, useRef, useState, type ComponentProps } from 'react'
import { useTranslation } from 'react-i18next'
 
import { Calendar } from '@/components/ui/calendar'
import { Input } from '@/components/ui/input'
import { Popover, PopoverAnchor, PopoverContent } from '@/components/ui/popover'
import { formatBrDateString, parseBrDateString, parseDateString, toDateString } from '@/lib/date'
import { cn } from '@/lib/utils'
import { CalendarIcon } from '@/shared/icons/calendar-icon'
 
const DATE_FNS_LOCALES: Record<string, Locale> = { 'pt-BR': ptBR, en: enUS, es }
 
interface DatePickerFieldProps extends Omit<ComponentProps<typeof Input>, 'value' | 'onChange' | 'type'> {
  value: string
  onValueChange: (value: string) => void
}
 
// Strips everything but digits and re-inserts the dd-MM-yyyy dashes as the user types, so typing
// "10032020" (or pasting it) becomes "10-03-2020" without the user having to type the dashes
// themselves. Deliberately simple (no cursor-position tracking) — good enough for append/backspace
// typing and full paste, which covers how this field is actually used.
//
// A pasted (or programmatically set) full ISO value is recognized as-is rather than run through
// the digit-reordering mask — otherwise "2026-08-05" would be misread as day="20" month="26".
function maskBrDateInput(rawValue: string): string {
  const isoMatch = /^(\d{4})-(\d{2})-(\d{2})$/.exec(rawValue.trim())
  if (isoMatch) {
    const [, year, month, day] = isoMatch
    return `${day}-${month}-${year}`
  }
 
  const digits = rawValue.replace(/\D/g, '').slice(0, 8)
  const day = digits.slice(0, 2)
  const month = digits.slice(2, 4)
  const year = digits.slice(4, 8)
  return [day, month, year].filter(Boolean).join('-')
}
 
// Displays and accepts typed input in the Brazilian dd-MM-yyyy standard, while the value passed
// to/from the form (and the API) stays ISO (yyyy-MM-dd) — kept as a real labeled input so it
// stays compatible with RHF/tests — paired with a calendar-icon trigger that opens the shared
// Calendar in a popover.
export function DatePickerField({ id, value, onValueChange, placeholder, disabled, className, ...inputProps }: DatePickerFieldProps) {
  const { t, i18n } = useTranslation()
  const [open, setOpen] = useState(false)
  const locale = DATE_FNS_LOCALES[i18n.language] ?? ptBR
  // RHF's field.value can be `undefined` before a form's defaultValues resolve, even though the
  // prop type says `string`.
  const safeValue = value ?? ''
  const selectedDate = parseDateString(safeValue)
 
  // Local text mirrors the ISO value formatted as dd-MM-yyyy, but only re-syncs when the ISO
  // value itself changes (calendar pick, form reset) — not on every keystroke — so an in-progress,
  // not-yet-complete date the user is typing isn't clobbered.
  const [text, setText] = useState(() => formatBrDateString(safeValue))
  const emittedValue = useRef<string | null>(null)
  useEffect(() => {
    if (emittedValue.current === safeValue) return
    setText(formatBrDateString(safeValue))
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [safeValue])
 
  return (
    <Popover open={open} onOpenChange={setOpen}>
      <PopoverAnchor>
        <div className="relative">
          <Input
            id={id}
            value={text}
            onChange={(event) => {
              const nextText = maskBrDateInput(event.target.value)
              setText(nextText)
              const parsed = parseBrDateString(nextText) ?? parseDateString(nextText)
              const nextValue = parsed ? toDateString(parsed) : ''
              emittedValue.current = nextValue
              onValueChange(nextValue)
            }}
            onClick={() => setOpen(true)}
            onFocus={() => setOpen(true)}
            placeholder={placeholder ?? t('common.datePicker.placeholder')}
            disabled={disabled}
            autoComplete="off"
            inputMode="numeric"
            maxLength={10}
            className={cn('pr-9', className)}
            {...inputProps}
          />
          <button
            type="button"
            disabled={disabled}
            aria-label={t('common.datePicker.openLabel')}
            onClick={() => setOpen(true)}
            className="absolute inset-y-0 right-0 flex w-8 items-center justify-center text-ink-muted transition-colors hover:text-ink disabled:pointer-events-none disabled:opacity-50"
          >
            <CalendarIcon className="h-4 w-4" />
          </button>
        </div>
      </PopoverAnchor>
      <PopoverContent
        align="start"
        className="w-auto p-0"
        // Radix focuses the first focusable element inside the content by default when it opens —
        // here that's a calendar day, which would steal focus away from the input the moment the
        // popover opens (via click/focus), breaking typing. Keep focus on the input instead.
        onOpenAutoFocus={(event) => event.preventDefault()}
      >
        <Calendar
          mode="single"
          locale={locale}
          captionLayout="dropdown"
          defaultMonth={selectedDate}
          selected={selectedDate}
          onSelect={(date) => {
            if (date) {
              emittedValue.current = null
              setText(formatBrDateString(toDateString(date)))
              onValueChange(toDateString(date))
              setOpen(false)
            }
          }}
        />
      </PopoverContent>
    </Popover>
  )
}